-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathserverErrors.ts
More file actions
52 lines (43 loc) · 1.54 KB
/
serverErrors.ts
File metadata and controls
52 lines (43 loc) · 1.54 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
40
41
42
43
44
45
46
47
48
49
50
51
52
import { isArray } from 'lodash';
import { FormFieldError, ResponseServerError } from './types';
export default function serverErrors(error: unknown): error is { data: { message: string } } {
return (
typeof error === 'object' &&
error !== null &&
'data' in error &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
typeof ((error as any).data as any)?.message === 'string'
);
}
export function isResponseServerFormFieldError(fieldError: unknown): fieldError is FormFieldError {
return (
typeof fieldError === 'object' &&
fieldError !== null &&
fieldError !== undefined &&
'loc' in fieldError &&
'msg' in fieldError &&
isArray(fieldError?.loc) &&
typeof fieldError?.msg === 'string'
);
}
export function isResponseServerError(formErrors: unknown): formErrors is ResponseServerError {
return (
typeof formErrors === 'object' &&
formErrors !== null &&
formErrors !== undefined &&
'detail' in formErrors &&
isArray(formErrors?.detail) &&
!!formErrors.detail.length &&
'msg' in formErrors.detail[0]
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getServerError(error: any): string {
let errorText = error?.error;
const errorData = error.data;
if (isResponseServerError(errorData)) {
const errorDetail = errorData.detail;
errorText = errorDetail.flatMap(({ msg }) => msg).join(', ');
}
return errorText;
}