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 | 'use strict'
|
34 |
|
35 | const _ = require('lodash')
|
36 |
|
37 | const MojaloopSDKError = require('@mojaloop/sdk-standard-components').Errors
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const MojaloopTypes = {
|
43 | GENERIC_COMMUNICATION_ERROR: {
|
44 | regex: '^10[0-9]{2}$',
|
45 | description: 'Generic Communication Error',
|
46 | httpStatusCode: 503
|
47 | },
|
48 | GENERIC_SERVER_ERROR: {
|
49 | regex: '^20[0-9]{2}$',
|
50 | description: 'Generic Server Error',
|
51 | httpStatusCode: 500
|
52 | },
|
53 | GENERIC_CLIENT_ERROR: {
|
54 | regex: '^30[0-9]{2}$',
|
55 | description: 'Generic Client Error',
|
56 | httpStatusCode: 400
|
57 | },
|
58 | CLIENT_VALIDATION_ERROR: {
|
59 | regex: '^31[0-9]{2}$',
|
60 | description: 'Client Validation Error',
|
61 | httpStatusCode: 400
|
62 | },
|
63 | IDENTIFIER_ERROR: {
|
64 | regex: '^32[0-9]{2}$',
|
65 | description: 'Identifier Error',
|
66 | httpStatusCode: 400
|
67 | },
|
68 | EXPIRED_ERROR: {
|
69 | regex: '^33[0-9]{2}$',
|
70 | description: 'Expired Error',
|
71 | httpStatusCode: 400
|
72 | },
|
73 | GENERIC_PAYER_ERROR: {
|
74 | regex: '^40[0-9]{2}$',
|
75 | description: 'Generic Payer Error',
|
76 | httpStatusCode: 400
|
77 | },
|
78 | PAYER_REJECTION_ERROR: {
|
79 | regex: '^41[0-9]{2}$',
|
80 | description: 'Payer Rejection Error',
|
81 | httpStatusCode: 400
|
82 | },
|
83 | PAYER_LIMIT_ERROR: {
|
84 | regex: '^42[0-9]{2}$',
|
85 | description: 'Payer Limit Error',
|
86 | httpStatusCode: 400
|
87 | },
|
88 | PAYER_PERMISSION_ERROR: {
|
89 | regex: '^43[0-9]{2}$',
|
90 | description: 'Payer Permission Error',
|
91 | httpStatusCode: 400
|
92 | },
|
93 | PAYER_BLOCKED_ERROR: {
|
94 | regex: '^44[0-9]{2}$',
|
95 | description: 'Payer Blocker Error',
|
96 | httpStatusCode: 400
|
97 | },
|
98 | GENERIC_PAYEE_ERROR: {
|
99 | regex: '^50[0-9]{2}$',
|
100 | description: 'Generic Payee Error',
|
101 | httpStatusCode: 400
|
102 | },
|
103 | PAYEE_REJECTION_ERROR: {
|
104 | regex: '^51[0-9]{2}$',
|
105 | description: 'Payee Rejection Error',
|
106 | httpStatusCode: 400
|
107 | },
|
108 | PAYEE_LIMIT_ERROR: {
|
109 | regex: '^52[0-9]{2}$',
|
110 | description: 'Payee Limit Error',
|
111 | httpStatusCode: 400
|
112 | },
|
113 | PAYEE_PERMISSION_ERROR: {
|
114 | regex: '^53[0-9]{2}$',
|
115 | description: 'Payee Permission Error',
|
116 | httpStatusCode: 400
|
117 | },
|
118 | PAYEE_BLOCKED_ERROR: {
|
119 | regex: '^54[0-9]{2}$',
|
120 | description: 'Payee Blocker Error',
|
121 | httpStatusCode: 400
|
122 | }
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | const MojaloopApiErrorCodesOverride = {
|
129 |
|
130 |
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | const populateOverrides = (errorCodes, override) => {
|
141 | const newErrorCodes = _.cloneDeep(errorCodes)
|
142 | for (const [overrideKey, overrideValue] of Object.entries(override)) {
|
143 | if (newErrorCodes[overrideKey]) {
|
144 | for (const [key, value] of Object.entries(overrideValue)) {
|
145 | _.set(newErrorCodes[overrideKey], key, value)
|
146 | }
|
147 | } else {
|
148 | _.set(newErrorCodes, overrideKey, overrideValue)
|
149 | }
|
150 | }
|
151 | return newErrorCodes
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | const populateErrorTypes = (errorCodes, errorTypes) => {
|
162 | const newErrorCodes = {}
|
163 | for (const [errorCodeKey, errorCodeValue] of Object.entries(errorCodes)) {
|
164 | for (const [errorTypeKey, errorTypeValue] of Object.entries(errorTypes)) {
|
165 | const regExp = new RegExp(errorTypeValue.regex)
|
166 | if (regExp.test(errorCodeValue.code)) {
|
167 | const newErrorCodeValue = _.cloneDeep(errorCodeValue)
|
168 | _.set(newErrorCodeValue, 'name', errorCodeKey)
|
169 | _.set(newErrorCodeValue, 'type', errorTypeValue)
|
170 | _.set(newErrorCodeValue, 'type.name', errorTypeKey)
|
171 | if (_.get(newErrorCodeValue, 'httpStatusCode') === undefined) {
|
172 | _.set(newErrorCodeValue, 'httpStatusCode', errorTypeValue.httpStatusCode)
|
173 | }
|
174 | _.set(newErrorCodes, errorCodeKey, newErrorCodeValue)
|
175 | }
|
176 | }
|
177 | }
|
178 | return newErrorCodes
|
179 | }
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | const errorCodesToMap = (errorCodes) => {
|
188 | const newErrorCodeMap = {}
|
189 | for (const [errorCodeKey, errorCodeValue] of Object.entries(errorCodes)) {
|
190 | const newErrorCodeValue = _.cloneDeep(errorCodeValue)
|
191 | _.set(newErrorCodeValue, 'name', errorCodeKey)
|
192 | _.set(newErrorCodeMap, errorCodeValue.code.toString(), newErrorCodeValue)
|
193 | }
|
194 | return newErrorCodeMap
|
195 | }
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | let FSPIOPErrorCodes = populateOverrides(MojaloopSDKError.MojaloopApiErrorCodes, MojaloopApiErrorCodesOverride)
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | FSPIOPErrorCodes = populateErrorTypes(FSPIOPErrorCodes, MojaloopTypes)
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | const FSPIOPErrorCodeMap = errorCodesToMap(FSPIOPErrorCodes)
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | const findFSPIOPErrorCode = (code) => {
|
219 | let ec
|
220 | if (code) {
|
221 | const stringCodeValue = code.toString()
|
222 | ec = FSPIOPErrorCodeMap[stringCodeValue]
|
223 | }
|
224 |
|
225 | if (ec) {
|
226 | return ec
|
227 | }
|
228 | return undefined
|
229 | }
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | const findErrorType = (errorCode) => {
|
238 | for (const [errorTypeKey, errorTypeValue] of Object.entries(MojaloopTypes)) {
|
239 | const regExp = new RegExp(errorTypeValue.regex)
|
240 | if (regExp.test(errorCode)) {
|
241 | const newErrorCodeType = _.cloneDeep(errorTypeValue)
|
242 | _.set(newErrorCodeType, 'name', errorTypeKey)
|
243 | return newErrorCodeType
|
244 | }
|
245 | }
|
246 | return undefined
|
247 | }
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | const MojaloopModelTypes = {
|
253 | ExtensionKey: {
|
254 | cardinality: 1,
|
255 | type: 'string',
|
256 | constraints: {
|
257 | min: 1,
|
258 | max: 32
|
259 | }
|
260 | },
|
261 | ExtensionValue: {
|
262 | cardinality: 1,
|
263 | type: 'string',
|
264 | constraints: {
|
265 | min: 1,
|
266 | max: 128
|
267 | }
|
268 | }
|
269 | }
|
270 |
|
271 | const Internal = {
|
272 | FSPIOPError: {
|
273 | ExtensionsKeys: {
|
274 | cause: 'cause',
|
275 | _cause: '_cause'
|
276 | }
|
277 | }
|
278 | }
|
279 |
|
280 | module.exports = {
|
281 | FSPIOPErrorCodes,
|
282 | FSPIOPErrorTypes: MojaloopTypes,
|
283 | FSPIOPErrorCodeMap,
|
284 | findFSPIOPErrorCode,
|
285 | findErrorType,
|
286 | MojaloopModelTypes,
|
287 | Internal,
|
288 | _populateOverrides: populateOverrides
|
289 | }
|