mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Helper to resolve proxy from URL in any environment, or using extension-API (#5690)
* Permit modular directory structures for behaviours in spirit of "Screaming Architecture" Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Implement env-agnostic helper to resolve proxy from URL Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Implement way to resolve a proxy, that is not reliant on existing windows Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Restore reliancy on existing windows for resolving proxy for fears of Electron events misbehaving because of a temp-window used in case no windows are open Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Make resolving a proxy throw if no browser window is available Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Extract minimal abstraction for specifically logging error instead of also warn, info, etc. Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Make decorator for error logging not create orphan promise for easier controllability Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Add error logging to resolving of proxy Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Expose resolving of proxy in extension-API Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Add missing general override Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Add test to add documentation Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Disperse implementation of resolve-proxy back to conventional directory structure instead of an experimental one Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Revert "Permit modular directory structures for behaviours in spirit of "Screaming Architecture"" This reverts commit 75e1231b0e61b74d030d12365352226e7f1ce500. Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Rename a helper for less ambiguity Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com> * Add tsdoc for "resolveSystemProxy" exposed in Extension-API Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
1 parent
fa23b5cd3f
commit
71427da02d
15 files changed
+432
-27
No files matched your search
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import loggerInjectable from "./logger.injectable";
|
||||
|
||||
const logErrorInjectable = getInjectable({
|
||||
id: "log-error",
|
||||
instantiate: (di) => di.inject(loggerInjectable).error,
|
||||
});
|
||||
|
||||
export default logErrorInjectable;
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { RequestChannel } from "../channel/request-channel-injection-token";
|
||||
import { requestChannelInjectionToken } from "../channel/request-channel-injection-token";
|
||||
|
||||
export type ResolveSystemProxyChannel = RequestChannel<string, string>;
|
||||
|
||||
const resolveSystemProxyChannelInjectable = getInjectable({
|
||||
id: "resolve-system-proxy-channel",
|
||||
|
||||
instantiate: (): ResolveSystemProxyChannel => ({
|
||||
id: "resolve-system-proxy-channel",
|
||||
}),
|
||||
|
||||
injectionToken: requestChannelInjectionToken,
|
||||
});
|
||||
|
||||
export default resolveSystemProxyChannelInjectable;
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
|
||||
export type ResolveSystemProxy = (url: string) => Promise<string>;
|
||||
|
||||
export const resolveSystemProxyInjectionToken = getInjectionToken<ResolveSystemProxy>({
|
||||
id: "resolve-system-proxy",
|
||||
});
|
||||
@@ -3,7 +3,7 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import loggerInjectable from "../../logger.injectable";
|
||||
import logErrorInjectable from "../../log-error.injectable";
|
||||
import { isPromise } from "../is-promise/is-promise";
|
||||
|
||||
export type WithErrorLoggingFor = (
|
||||
@@ -16,30 +16,34 @@ const withErrorLoggingInjectable = getInjectable({
|
||||
id: "with-error-logging",
|
||||
|
||||
instantiate: (di): WithErrorLoggingFor => {
|
||||
const logger = di.inject(loggerInjectable);
|
||||
const logError = di.inject(logErrorInjectable);
|
||||
|
||||
return (getErrorMessage) =>
|
||||
(toBeDecorated) =>
|
||||
(...args) => {
|
||||
let returnValue: ReturnType<typeof toBeDecorated>;
|
||||
|
||||
try {
|
||||
const returnValue = toBeDecorated(...args);
|
||||
|
||||
if (isPromise(returnValue)) {
|
||||
returnValue.catch((e) => {
|
||||
const errorMessage = getErrorMessage(e);
|
||||
|
||||
logger.error(errorMessage, e);
|
||||
});
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
returnValue = toBeDecorated(...args);
|
||||
} catch (e) {
|
||||
const errorMessage = getErrorMessage(e);
|
||||
|
||||
logger.error(errorMessage, e);
|
||||
logError(errorMessage, e);
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
if (isPromise(returnValue)) {
|
||||
return returnValue.catch((e: unknown) => {
|
||||
const errorMessage = getErrorMessage(e);
|
||||
|
||||
logError(errorMessage, e);
|
||||
|
||||
throw e;
|
||||
});
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -163,23 +163,24 @@ describe("with-error-logging", () => {
|
||||
});
|
||||
|
||||
describe("when call rejects with error instance", () => {
|
||||
let error: Error;
|
||||
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await toBeDecorated.reject(new Error("some-error"));
|
||||
await returnValuePromise;
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
beforeEach(() => {
|
||||
toBeDecorated.reject(new Error("some-error"));
|
||||
});
|
||||
|
||||
it("logs the error", () => {
|
||||
it("logs the error", async () => {
|
||||
let error: unknown;
|
||||
|
||||
try {
|
||||
await returnValuePromise;
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(loggerStub.error).toHaveBeenCalledWith("some-error-message-for-some-error", error);
|
||||
});
|
||||
|
||||
it("rejects", () => {
|
||||
return expect(() => returnValuePromise).rejects.toThrow("some-error");
|
||||
return expect(returnValuePromise).rejects.toThrow("some-error");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -187,8 +188,9 @@ describe("with-error-logging", () => {
|
||||
let error: unknown;
|
||||
|
||||
beforeEach(async () => {
|
||||
toBeDecorated.reject({ someProperty: "some-rejection" });
|
||||
|
||||
try {
|
||||
await toBeDecorated.reject({ someProperty: "some-rejection" });
|
||||
await returnValuePromise;
|
||||
} catch (e) {
|
||||
error = e;
|
||||
@@ -203,7 +205,7 @@ describe("with-error-logging", () => {
|
||||
});
|
||||
|
||||
it("rejects", () => {
|
||||
return expect(() => returnValuePromise).rejects.toEqual({ someProperty: "some-rejection" });
|
||||
return expect(returnValuePromise).rejects.toEqual({ someProperty: "some-rejection" });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user