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

Simplify code for telemetry more by leveraging call-result from previous commit

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

Co-authored-by: Gabriel <gaccettola@mirantis.com>
This commit is contained in:
Iku-turso 2023-03-23 17:28:13 +02:00
parent b847bb71f4
commit 3359d7d77b

View File

@ -3,7 +3,6 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import type { DiContainerForInjection } from "@ogre-tools/injectable"; import type { DiContainerForInjection } from "@ogre-tools/injectable";
import { import {
getInjectable, getInjectable,
instantiationDecoratorToken, instantiationDecoratorToken,
@ -24,6 +23,7 @@ import logErrorInjectable, {
} from "../../../common/log-error.injectable"; } from "../../../common/log-error.injectable";
import type { AppEvent } from "../../../common/app-event-bus/event-bus"; import type { AppEvent } from "../../../common/app-event-bus/event-bus";
import { getFailure, getSuccess } from "./call-result/call-result";
const telemetryDecoratorInjectable = getInjectable({ const telemetryDecoratorInjectable = getInjectable({
id: "telemetry-decorator", id: "telemetry-decorator",
@ -85,17 +85,19 @@ const doTelemetryFor =
args: any[], args: any[],
currentContext: any currentContext: any
) => { ) => {
try { const getParamsSafe = withNoThrownErrors(whiteListed.getParams);
const params = whiteListed.getParams(...args);
const params = getParamsSafe(...args);
if (params.callWasSuccessful) {
emitTelemetry({ emitTelemetry({
action: currentContext.injectable.id, action: currentContext.injectable.id,
params, params: params.response,
}); });
} catch (e: any) { } else {
logError( logError(
`Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`, `Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`,
e params.error
); );
emitTelemetry({ emitTelemetry({
@ -128,4 +130,22 @@ const getWhiteListMap = (whiteList: WhiteListItem[]) =>
) )
); );
type ToBeDecorated<TValue, TArgs extends unknown[]> = (
...args: TArgs
) => TValue;
const withNoThrownErrors =
<TValue, TArgs extends unknown[]>(
toBeDecorated: ToBeDecorated<TValue, TArgs>
) =>
(...args: TArgs) => {
try {
const result = toBeDecorated(...args);
return getSuccess(result);
} catch (error) {
return getFailure(error);
}
};
export default telemetryDecoratorInjectable; export default telemetryDecoratorInjectable;