-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy patherror.ts
More file actions
39 lines (36 loc) · 1.1 KB
/
error.ts
File metadata and controls
39 lines (36 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { TFunction } from 'react-i18next';
export type ErrorType = {
message?: string;
moreInfo?: string;
response?: Response;
};
type FetchError = {
json?: {
detail?: string | { response?: string; cause?: string };
message?: string;
};
message?: string;
response?: Response;
};
// Extracts the error message from a Fetch error
export const getFetchErrorMessage = (error: FetchError, t: TFunction): ErrorType => {
// For OpenShift Lightspeed API errors, the `detail` field will either be a single string or
// an object containing `response` and `cause` strings
const detail = error.json?.detail;
if (detail && typeof detail === 'string') {
return { message: detail };
}
if (
detail &&
typeof detail === 'object' &&
typeof detail.response === 'string' &&
typeof detail.cause === 'string'
) {
return { message: detail.response, moreInfo: detail.cause };
}
return {
message: t('If this error persists, please contact an administrator. Error details: {{e}}', {
e: error.json?.message || error.message || error.response?.statusText,
}),
};
};