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-tursoandGabriel committed 2023-03-24 11:18:18 +02:00
1 parent b847bb71f4
commit 3359d7d77b
1 file changed
+26 -6
@@ -3,7 +3,6 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainerForInjection } from "@ogre-tools/injectable";
import {
getInjectable,
instantiationDecoratorToken,
@@ -24,6 +23,7 @@ import logErrorInjectable, {
} from "../../../common/log-error.injectable";
import type { AppEvent } from "../../../common/app-event-bus/event-bus";
import { getFailure, getSuccess } from "./call-result/call-result";
const telemetryDecoratorInjectable = getInjectable({
id: "telemetry-decorator",
@@ -85,17 +85,19 @@ const doTelemetryFor =
args: any[],
currentContext: any
) => {
try {
const params = whiteListed.getParams(...args);
const getParamsSafe = withNoThrownErrors(whiteListed.getParams);
const params = getParamsSafe(...args);
if (params.callWasSuccessful) {
emitTelemetry({
action: currentContext.injectable.id,
params,
params: params.response,
});
} catch (e: any) {
} else {
logError(
`Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`,
e
params.error
);
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;