-
Notifications
You must be signed in to change notification settings - Fork 0
/
getErrorFromCodes.js
109 lines (94 loc) · 3.39 KB
/
getErrorFromCodes.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {codes, messages} from './codesMessages';
/**
* SJWT Code - a code exported from `./codesMessages.js`
*
* @typedef {string} SjwtCode
*/
/**
* SJWT Code Item - an SJWT Code with additional contextual hints
*
* @typedef {object} SjwtCodeItem
* @property {SjwtCode} code - A code exported from `./codesMessages.js`
* @property {any} [value] - the value that caused the issue
* @property {string} [param] - the parameter that the value was a part of
* @property {string} [location] - where the parameter resided (e.g. `body`)
*/
/**
* SJWT Error Item - The SJWT Code, its associated message, and any additional
* contextual hints that were provided.
*
* @typedef {object} SjwtErrorItem
* @property {SjwtCode} code - A code exported from `./codesMessages.js`
* @property {string} message - A user facing message describing the problem
* @property {any} [value] - the value that caused the issue
* @property {string} [param] - the parameter that the value was a part of
* @property {string} [location] - where the parameter resided (e.g. `body`)
*/
/**
* SJWT Error object
*
* @typedef {object} SjwtError
* @property {SjwtCode} code - A code exported from `./codesMessages.js`
* @property {string} message - A user facing message describing the problem
* @property {object} context - Additional information that may describe the problem
* @property {Array.<SjwtErrorItem>} errors - All errors that occurred, including the code and message already exposed above. Guaranteed to be at least length 1.
*/
/**
* Checks to confirm the code has a corresponding message, and if not returns the
* code `UNKNOWN_ERROR`
*
* @param {string} code - The error code to transform
* @returns {SjwtCode}
*/
const getRealCode = (code) => (messages[code] ? code : codes.UNKNOWN_ERROR);
/**
* Transforms the given array into at least 1 `SjwtErrorItem`
*
* @param {Array.<SjwtCode|SjwtCodeItem>} items - an array of code strings or items
* @returns {Array.<SjwtErrorItem>}
*/
const transformItems = (items = []) => {
const transformedItems = items.map((item) => {
const realCode = typeof item === 'string'
? getRealCode(item)
: getRealCode(item?.code);
const transformedItem = {
code: realCode,
message: messages[realCode],
};
if (item?.value !== undefined) {
transformedItem.value = item.value;
}
if (item?.param !== undefined) {
transformedItem.param = item.param;
}
if (item?.location !== undefined) {
transformedItem.location = item.location;
}
return transformedItem;
});
return transformedItems.length > 0
? transformedItems
: [{
code: codes.UNKNOWN_ERROR,
message: messages[codes.UNKNOWN_ERROR],
}];
};
/**
* Get an `SjwtError` from an array of codes or items. The first code in the
* error is guaranteed to be the same as the first item in the array of errors.
*
* @param {Array.<SjwtCode|SjwtCodeItem>} items - an array of code strings or items
* @param {object} context - additional context to include with the error
* @returns {SjwtError}
*/
const getErrorFromCodes = (items = [], context = {}) => {
const errors = transformItems(items);
return {
code: errors[0].code,
message: errors[0].message,
errors,
context,
};
};
export default getErrorFromCodes;