The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/plugins/extensions/errorHandling.ts

72 lines
2.2 KiB

import { isFunction, isObject } from 'lodash';
import type { CommandHandlerFunc, ConfigureFunc } from './types';
type Options = {
pluginId: string;
title: string;
logger: (msg: string, error?: unknown) => void;
};
export function handleErrorsInConfigure<T>(options: Options) {
const { pluginId, title, logger } = options;
return (configure: ConfigureFunc<T>): ConfigureFunc<T> => {
return function handleErrors(extension, context) {
try {
if (!isFunction(configure)) {
logger(`[Plugins] ${pluginId} provided invalid configuration function for extension '${title}'.`);
return;
}
const result = configure(extension, context);
if (result instanceof Promise) {
logger(
`[Plugins] ${pluginId} provided an unsupported async/promise-based configureation function for extension '${title}'.`
);
result.catch(() => {});
return;
}
if (!isObject(result) && typeof result !== 'undefined') {
logger(`[Plugins] ${pluginId} returned an inccorect object in configure function for extension '${title}'.`);
return;
}
return result;
} catch (error) {
logger(`[Plugins] ${pluginId} thow an error while configure extension '${title}'`, error);
return;
}
};
};
}
export function handleErrorsInHandler(options: Options) {
const { pluginId, title, logger } = options;
return (handler: CommandHandlerFunc): CommandHandlerFunc => {
return function handleErrors(context) {
try {
if (!isFunction(handler)) {
logger(`[Plugins] ${pluginId} provided invalid handler function for command extension '${title}'.`);
return;
}
const result = handler(context);
if (result instanceof Promise) {
logger(
`[Plugins] ${pluginId} provided an unsupported async/promise-based handler function for command extension '${title}'.`
);
result.catch(() => {});
return;
}
return result;
} catch (error) {
logger(`[Plugins] ${pluginId} thow an error while handling command extension '${title}'`, error);
return;
}
};
};
}