Files
grafana/public/app/api/utils.ts
Ashley Harrison 45092261ab Playlist: Move to generated api client (#103083)
* create playlist api client

* add more api client methods

* integrate new client. TODO: tests, integrate remaining util functions

* add error handling

* fix unit tests

* refactor to remove PlaylistUI type

* cleaner PlaylistStartPage

* handle generateName

* Fix startmodal name

* create generic error handling util

* prettier

* keep migration code for now

* better syntax
2025-04-16 13:27:52 +01:00

40 lines
1.5 KiB
TypeScript

import { config, isFetchError } from '@grafana/runtime';
import { notifyApp } from '../core/actions';
import { createErrorNotification } from '../core/copy/appNotification';
import { ThunkDispatch } from '../types';
export const getAPINamespace = () => config.namespace;
/**
* Get a base URL for a k8s API endpoint with parameterised namespace given it's group and version
* @param group the k8s group, e.g. dashboard.grafana.app
* @param version e.g. v0alpha1
* @returns
*/
export const getAPIBaseURL = (group: string, version: string) => {
return `/apis/${group}/${version}/namespaces/${getAPINamespace()}`;
};
/**
* Handle an error from a k8s API call
* @param e the raw error
* @param dispatch store dispatch function
* @param message error alert title. error details will also be surfaced
*/
export const handleError = (e: unknown, dispatch: ThunkDispatch, message: string) => {
if (!e) {
dispatch(notifyApp(createErrorNotification(message, new Error('Unknown error'))));
} else if (e instanceof Error) {
dispatch(notifyApp(createErrorNotification(message, e)));
} else if (typeof e === 'object' && 'error' in e) {
if (e.error instanceof Error) {
dispatch(notifyApp(createErrorNotification(message, e.error)));
} else if (isFetchError(e.error)) {
if (Array.isArray(e.error.data.errors) && e.error.data.errors.length) {
dispatch(notifyApp(createErrorNotification(message, e.error.data.errors.join('\n'))));
}
}
}
};