2025-06-18 21:51:25 +08:00
|
|
|
import { FetchError, isFetchError } from '@grafana/runtime';
|
2019-02-07 02:42:04 +08:00
|
|
|
|
2023-02-01 18:50:34 +08:00
|
|
|
export function getMessageFromError(err: unknown): string {
|
2023-03-02 20:09:58 +08:00
|
|
|
if (typeof err === 'string') {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 18:50:34 +08:00
|
|
|
if (err) {
|
2023-03-02 20:09:58 +08:00
|
|
|
if (err instanceof Error) {
|
2019-02-07 02:42:04 +08:00
|
|
|
return err.message;
|
2023-02-01 18:50:34 +08:00
|
|
|
} else if (isFetchError(err)) {
|
|
|
|
|
if (err.data && err.data.message) {
|
|
|
|
|
return err.data.message;
|
|
|
|
|
} else if (err.statusText) {
|
|
|
|
|
return err.statusText;
|
|
|
|
|
}
|
2025-01-27 22:54:10 +08:00
|
|
|
} else if (err.hasOwnProperty('message')) {
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
return err.message;
|
2019-02-07 02:42:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-02 20:09:58 +08:00
|
|
|
|
2023-02-01 18:50:34 +08:00
|
|
|
return JSON.stringify(err);
|
2019-02-07 02:42:04 +08:00
|
|
|
}
|
2025-01-27 22:54:10 +08:00
|
|
|
|
|
|
|
|
export function getStatusFromError(err: unknown): number | undefined {
|
|
|
|
|
if (typeof err === 'string') {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
if (err instanceof Error) {
|
|
|
|
|
return undefined;
|
|
|
|
|
} else if (isFetchError(err)) {
|
|
|
|
|
return err.status;
|
|
|
|
|
} else if (err.hasOwnProperty('status')) {
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
return err.status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getMessageIdFromError(err: unknown): string | undefined {
|
|
|
|
|
if (typeof err === 'string') {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
if (err instanceof Error) {
|
|
|
|
|
return undefined;
|
|
|
|
|
} else if (isFetchError(err)) {
|
|
|
|
|
return err.data?.messageId;
|
|
|
|
|
} else if (err.hasOwnProperty('messageId')) {
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
return err.messageId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2025-06-18 21:51:25 +08:00
|
|
|
|
|
|
|
|
export function getRequestConfigFromError(err: FetchError): string {
|
|
|
|
|
const method = err.config?.method ?? 'GET';
|
|
|
|
|
const url = err.config?.url;
|
|
|
|
|
|
|
|
|
|
return method && url ? `${method} ${url}` : 'request';
|
|
|
|
|
}
|