1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix failing unit tests about overriding too late (#7287)

The fix was to defer injection. The error was made possible in master by git-merging a PR which was
falsely green.

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2023-03-06 14:45:12 +02:00 committed by GitHub
parent c6799e1478
commit d3c9c6b5f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,63 +21,62 @@ import logErrorInjectable from "../../../common/log-error.injectable";
const telemetryDecoratorInjectable = getInjectable({ const telemetryDecoratorInjectable = getInjectable({
id: "telemetry-decorator", id: "telemetry-decorator",
instantiate: (diForDecorator) => { instantiate: (diForDecorator) => ({
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable); decorate:
const logError = diForDecorator.inject(logErrorInjectable); (instantiateToBeDecorated: any) =>
(di: DiContainerForInjection, instantiationParameter: any) => {
const instance = instantiateToBeDecorated(di, instantiationParameter);
const whiteList = diForDecorator.inject( if (isFunction(instance)) {
telemetryWhiteListForFunctionsInjectable, return (...args: any[]) => {
); const currentContext = di.context.at(-1);
const whiteListMap = getWhiteListMap(whiteList); assert(currentContext);
return { const emitTelemetry = diForDecorator.inject(
decorate: emitTelemetryInjectable,
(instantiateToBeDecorated: any) => );
(di: DiContainerForInjection, instantiationParameter: any) => {
const instance = instantiateToBeDecorated(di, instantiationParameter);
if (isFunction(instance)) { const logError = diForDecorator.inject(logErrorInjectable);
return (...args: any[]) => {
const currentContext = di.context.at(-1);
assert(currentContext); const whiteList = diForDecorator.inject(
telemetryWhiteListForFunctionsInjectable,
);
const whiteListed = whiteListMap.get( const whiteListMap = getWhiteListMap(whiteList);
currentContext.injectable.id,
);
if (whiteListed) { const whiteListed = whiteListMap.get(currentContext.injectable.id);
let params;
try { if (whiteListed) {
params = whiteListed.getParams(...args); let params;
} catch (e) {
params = {
error:
"Tried to produce params for telemetry, but getParams() threw an error",
};
logError( try {
`Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`, params = whiteListed.getParams(...args);
e, } catch (e) {
); params = {
} error:
"Tried to produce params for telemetry, but getParams() threw an error",
};
emitTelemetry({ logError(
action: currentContext.injectable.id, `Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`,
params, e,
}); );
} }
return instance(...args); emitTelemetry({
}; action: currentContext.injectable.id,
} params,
});
}
return instance; return instance(...args);
}, };
}; }
},
return instance;
},
}),
decorable: false, decorable: false,
// Todo: this is required because of imperfect typing in injectable. // Todo: this is required because of imperfect typing in injectable.