mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: improve some types (#62363)
* improve some types * more tidy up * better error message handling + update tests * undo store changes
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { FetchError } from '@grafana/runtime';
|
||||
import { getMessageFromError } from 'app/core/utils/errors';
|
||||
|
||||
describe('errors functions', () => {
|
||||
@@ -15,7 +16,7 @@ describe('errors functions', () => {
|
||||
|
||||
describe('when getMessageFromError gets an error object with message field', () => {
|
||||
beforeEach(() => {
|
||||
message = getMessageFromError({ message: 'error string' } as Error);
|
||||
message = getMessageFromError(new Error('error string'));
|
||||
});
|
||||
|
||||
it('should return the message text', () => {
|
||||
@@ -25,7 +26,7 @@ describe('errors functions', () => {
|
||||
|
||||
describe('when getMessageFromError gets an error object with data.message field', () => {
|
||||
beforeEach(() => {
|
||||
message = getMessageFromError({ data: { message: 'error string' } } as any);
|
||||
message = getMessageFromError({ data: { message: 'error string' }, status: 500 } as FetchError);
|
||||
});
|
||||
|
||||
it('should return the message text', () => {
|
||||
@@ -35,7 +36,7 @@ describe('errors functions', () => {
|
||||
|
||||
describe('when getMessageFromError gets an error object with statusText field', () => {
|
||||
beforeEach(() => {
|
||||
message = getMessageFromError({ statusText: 'error string' } as any);
|
||||
message = getMessageFromError({ data: 'foo', statusText: 'error string', status: 500 } as FetchError);
|
||||
});
|
||||
|
||||
it('should return the statusText text', () => {
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
import { isString } from 'lodash';
|
||||
import { isFetchError } from '@grafana/runtime';
|
||||
|
||||
export function getMessageFromError(err: string | (Error & { data?: any; statusText?: string })): string {
|
||||
if (err && !isString(err)) {
|
||||
if (err.message) {
|
||||
export function getMessageFromError(err: unknown): string {
|
||||
if (err) {
|
||||
if (typeof err === 'string') {
|
||||
return err;
|
||||
} else if (err instanceof Error) {
|
||||
return err.message;
|
||||
} else if (err.data && err.data.message) {
|
||||
return err.data.message;
|
||||
} else if (err.statusText) {
|
||||
return err.statusText;
|
||||
} else {
|
||||
return JSON.stringify(err);
|
||||
} else if (isFetchError(err)) {
|
||||
if (err.data && err.data.message) {
|
||||
return err.data.message;
|
||||
} else if (err.statusText) {
|
||||
return err.statusText;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
return JSON.stringify(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user