-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathldsUtils.js
More file actions
81 lines (80 loc) · 2.99 KB
/
ldsUtils.js
File metadata and controls
81 lines (80 loc) · 2.99 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Reduces one or more LDS errors into a string[] of error messages.
* @param {FetchResponse|FetchResponse[]} errors
* @return {String[]} Error messages
*/
export function reduceErrors(errors) {
if (!Array.isArray(errors)) {
errors = [errors];
}
return (
errors
// Remove null/undefined items
.filter((error) => !!error)
// Extract an error message
.map((error) => {
// UI API read errors
if (Array.isArray(error.body)) {
return error.body.map((e) => e.message);
}
// Page level errors
else if (
error?.body?.pageErrors &&
error.body.pageErrors.length > 0
) {
return error.body.pageErrors.map((e) => e.message);
}
// Field level errors
else if (
error?.body?.fieldErrors &&
Object.keys(error.body.fieldErrors).length > 0
) {
const fieldErrors = [];
Object.values(error.body.fieldErrors).forEach(
(errorArray) => {
fieldErrors.push(
...errorArray.map((e) => e.message)
);
}
);
return fieldErrors;
}
// UI API DML page level errors
else if (
error?.body?.output?.errors &&
error.body.output.errors.length > 0
) {
return error.body.output.errors.map((e) => e.message);
}
// UI API DML field level errors
else if (
error?.body?.output?.fieldErrors &&
Object.keys(error.body.output.fieldErrors).length > 0
) {
const fieldErrors = [];
Object.values(error.body.output.fieldErrors).forEach(
(errorArray) => {
fieldErrors.push(
...errorArray.map((e) => e.message)
);
}
);
return fieldErrors;
}
// UI API DML, Apex and network errors
else if (error.body && typeof error.body.message === 'string') {
return error.body.message;
}
// JS errors
else if (typeof error.message === 'string') {
return error.message;
}
// Unknown error shape so try HTTP status text
return error.statusText;
})
// Flatten
.reduce((prev, curr) => prev.concat(curr), [])
// Remove empty strings
.filter((message) => !!message)
);
}