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 Enums = require('./enums')
|
36 | const _ = require('lodash')
|
37 | const MojaloopFSPIOPError = require('@mojaloop/sdk-standard-components').Errors.MojaloopFSPIOPError
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | class FSPIOPError extends MojaloopFSPIOPError {
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | constructor (cause, message, replyTo, apiErrorCode, extensions, useMessageAsDescription = false) {
|
59 |
|
60 |
|
61 | if (extensions && !((Array.isArray(extensions)) || (extensions.extension && Array.isArray(extensions.extension)))) throw new Error('FSPIOPError Parameter Validation Failure - extensions is not a list or does not contain an extension list.')
|
62 | if (!apiErrorCode.code && !apiErrorCode.message) throw new Error('FSPIOPError Parameter Validation Failure - apiErrorCode is not valid error code enum.')
|
63 |
|
64 |
|
65 | const clonedExtensions = _.cloneDeep(extensions)
|
66 | super(cause, message, replyTo, apiErrorCode, clonedExtensions)
|
67 | this._setStackFromCause(cause)
|
68 | this.useMessageAsDescription = useMessageAsDescription
|
69 | }
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | _setStackFromCause (cause) {
|
78 | let stringifiedCause
|
79 | if (typeof this.cause === 'string' || this.cause instanceof String) {
|
80 | stringifiedCause = this.cause
|
81 | } else if (this.cause instanceof Error) {
|
82 | stringifiedCause = this.cause.stack
|
83 | } else {
|
84 | stringifiedCause = JSON.stringify(this.cause)
|
85 | }
|
86 | if (stringifiedCause) this.stack = `${this.stack}\n${stringifiedCause}`
|
87 | }
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | toApiErrorObject ({ includeCauseExtension = false, truncateExtensions = true } = {}) {
|
99 | let errorDescription = this.apiErrorCode.message
|
100 |
|
101 |
|
102 | if ((this.message && this.message !== 'null' && this.message.length > 0) && !this.useMessageAsDescription) {
|
103 | errorDescription = `${errorDescription} - ${this.message}`
|
104 | } else if (this.useMessageAsDescription) {
|
105 | errorDescription = `${this.message}`
|
106 | }
|
107 |
|
108 | const e = {
|
109 | errorInformation: {
|
110 | errorCode: this.apiErrorCode.code,
|
111 | errorDescription
|
112 | }
|
113 | }
|
114 |
|
115 | if (this.extensions) {
|
116 | e.errorInformation.extensionList = {}
|
117 |
|
118 | if (Array.isArray(this.extensions)) {
|
119 |
|
120 |
|
121 | e.errorInformation.extensionList.extension = _.cloneDeep(this.extensions)
|
122 | } else if (this.extensions.extension && Array.isArray(this.extensions.extension)) {
|
123 | e.errorInformation.extensionList.extension = _.cloneDeep(this.extensions.extension)
|
124 | }
|
125 |
|
126 | if (includeCauseExtension === true) {
|
127 | const causeKeyValueFromExtensions = e.errorInformation.extensionList.extension.find(keyValue => keyValue.key === Enums.Internal.FSPIOPError.ExtensionsKeys.cause)
|
128 | if (causeKeyValueFromExtensions) {
|
129 | causeKeyValueFromExtensions.value = `${this.stack}\n${causeKeyValueFromExtensions.value}`
|
130 | } else {
|
131 | const causeKeyValue = {
|
132 | key: Enums.Internal.FSPIOPError.ExtensionsKeys.cause,
|
133 | value: this.stack
|
134 | }
|
135 | e.errorInformation.extensionList.extension.push(causeKeyValue)
|
136 | }
|
137 | } else if (e.errorInformation.extensionList.extension && Array.isArray(e.errorInformation.extensionList.extension)) {
|
138 | _.remove(e.errorInformation.extensionList.extension, (extensionKeyValue) => {
|
139 | return [Enums.Internal.FSPIOPError.ExtensionsKeys.cause, Enums.Internal.FSPIOPError.ExtensionsKeys._cause].includes(extensionKeyValue.key)
|
140 | })
|
141 | if (e.errorInformation.extensionList.extension.length === 0) {
|
142 | delete e.errorInformation.extensionList
|
143 | }
|
144 | }
|
145 | } else {
|
146 | if (includeCauseExtension === true) {
|
147 | e.errorInformation.extensionList = {
|
148 | extension: [{
|
149 | key: Enums.Internal.FSPIOPError.ExtensionsKeys.cause,
|
150 | value: this.stack
|
151 | }]
|
152 | }
|
153 | }
|
154 | }
|
155 | const hasExtension = e.errorInformation.extensionList && e.errorInformation.extensionList.extension && e.errorInformation.extensionList.extension.length
|
156 | if (truncateExtensions && hasExtension) {
|
157 | for (const i in e.errorInformation.extensionList.extension) {
|
158 | if (e.errorInformation.extensionList.extension[i].value) {
|
159 | e.errorInformation.extensionList.extension[i].value = e.errorInformation.extensionList.extension[i].value.toString().substr(0, Enums.MojaloopModelTypes.ExtensionValue.constraints.max)
|
160 | }
|
161 | }
|
162 | }
|
163 |
|
164 | return e
|
165 | }
|
166 |
|
167 | toString () {
|
168 | return JSON.stringify(this.toFullErrorObject())
|
169 | }
|
170 | }
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | const createFSPIOPError = (apiErrorCode, message, cause, replyTo, extensions, useDescriptionAsMessage = false) => {
|
184 | if (apiErrorCode && apiErrorCode.code && apiErrorCode.message) {
|
185 | const newApiError = Object.assign({}, apiErrorCode)
|
186 | let match = Enums.findFSPIOPErrorCode(apiErrorCode.code)
|
187 | if (!match) {
|
188 | match = Enums.findErrorType(apiErrorCode.code)
|
189 | if (!match) {
|
190 | throw new FSPIOPError(cause, `Factory function createFSPIOPError failed due to apiErrorCode being invalid - ${JSON.stringify(apiErrorCode)}.`, replyTo, Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR, extensions)
|
191 | }
|
192 | if (!newApiError.httpStatusCode) {
|
193 | newApiError.httpStatusCode = match.httpStatusCode
|
194 | }
|
195 | } else if (!newApiError.httpStatusCode) {
|
196 | newApiError.httpStatusCode = match.httpStatusCode
|
197 | }
|
198 | return new FSPIOPError(cause, message, replyTo, newApiError, extensions, useDescriptionAsMessage)
|
199 | } else {
|
200 | throw new FSPIOPError(cause, `Factory function createFSPIOPError failed due to apiErrorCode being invalid - ${JSON.stringify(apiErrorCode)}.`, replyTo, Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR, extensions)
|
201 | }
|
202 | }
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | const createFSPIOPErrorFromJoiError = (error, cause, replyTo) => {
|
213 | const fspiopError = ((type) => {
|
214 | switch (type) {
|
215 | case 'any.required':
|
216 | case 'any.empty':
|
217 | return Enums.FSPIOPErrorCodes.MISSING_ELEMENT
|
218 | case 'object.allowUnknown':
|
219 | return Enums.FSPIOPErrorCodes.TOO_MANY_ELEMENTS
|
220 |
|
221 | case (type.match(/^string\./) || {}).input:
|
222 | case 'date.format':
|
223 | case 'number.integer':
|
224 | case 'any.allowOnly':
|
225 | return Enums.FSPIOPErrorCodes.MALFORMED_SYNTAX
|
226 | default:
|
227 | return Enums.FSPIOPErrorCodes.VALIDATION_ERROR
|
228 | }
|
229 | })(error.type)
|
230 |
|
231 | const stackTrace = (cause && cause.stack)
|
232 | ? cause.stack
|
233 | : cause
|
234 |
|
235 | const source = (
|
236 | cause &&
|
237 | cause.output &&
|
238 | cause.output.payload &&
|
239 | cause.output.payload.validation
|
240 | )
|
241 | ? cause.output.payload.validation.source
|
242 | : undefined
|
243 |
|
244 | const messages = {
|
245 | header: `'${error.context.label}' HTTP header`,
|
246 | params: `'${error.context.label}' URI path parameter`
|
247 | }
|
248 |
|
249 |
|
250 |
|
251 | const msg = (source && messages[source])
|
252 | ? messages[source]
|
253 | : error.message
|
254 |
|
255 | return createFSPIOPError(fspiopError, msg, stackTrace, replyTo)
|
256 | }
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | const createFSPIOPErrorFromOpenapiError = (error, replyTo) => {
|
266 | const fspiopError = ((type) => {
|
267 | switch (type) {
|
268 | case 'required':
|
269 | return Enums.FSPIOPErrorCodes.MISSING_ELEMENT
|
270 | case 'additionalProperties':
|
271 | return Enums.FSPIOPErrorCodes.TOO_MANY_ELEMENTS
|
272 | case 'type':
|
273 | return Enums.FSPIOPErrorCodes.MALFORMED_SYNTAX
|
274 | case 'notFound':
|
275 | return Enums.FSPIOPErrorCodes.UNKNOWN_URI
|
276 | case 'methodNotAllowed':
|
277 | return Enums.FSPIOPErrorCodes.METHOD_NOT_ALLOWED
|
278 | default:
|
279 | return Enums.FSPIOPErrorCodes.VALIDATION_ERROR
|
280 | }
|
281 | })(error.keyword)
|
282 | let message
|
283 | if (error.message) {
|
284 | message = error.dataPath + ' ' + error.message
|
285 | } else {
|
286 | message = error.dataPath
|
287 | }
|
288 | return createFSPIOPError(fspiopError, message, replyTo)
|
289 | }
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | const createInternalServerFSPIOPError = (message, cause, replyTo, extensions) => {
|
301 | return createFSPIOPError(Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR, message, cause, replyTo, extensions)
|
302 | }
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | const reformatFSPIOPError = (error, apiErrorCode = Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR, replyTo, extensions) => {
|
317 | if (error.constructor && error.constructor.name === FSPIOPError.name) {
|
318 | return error
|
319 | } else {
|
320 | return createFSPIOPError(apiErrorCode, error.message, error.stack, replyTo, extensions)
|
321 | }
|
322 | }
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 | const createFSPIOPErrorFromErrorInformation = (errorInformation, cause, replyTo) => {
|
333 | const errorCode = {
|
334 | code: errorInformation.errorCode,
|
335 | message: errorInformation.errorDescription
|
336 | }
|
337 | return createFSPIOPError(errorCode, errorInformation.errorDescription, cause, replyTo, errorInformation.extensionList, true)
|
338 | }
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | const createFSPIOPErrorFromErrorCode = (code, message, cause, replyTo, extensions) => {
|
351 | const errorCode = validateFSPIOPErrorCode(code)
|
352 | return createFSPIOPError(errorCode, message, cause, replyTo, extensions)
|
353 | }
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 | const validateFSPIOPErrorCode = (code) => {
|
364 | const errorMessage = 'Validation failed due to error code being invalid'
|
365 | let codeToValidate
|
366 | if (typeof code === 'number' || typeof code === 'string') {
|
367 | codeToValidate = code
|
368 | } else if (typeof code === 'object' && code.code) {
|
369 | codeToValidate = code.code
|
370 | }
|
371 |
|
372 | const result = Enums.findFSPIOPErrorCode(codeToValidate)
|
373 | if (result) {
|
374 | return result
|
375 | } else {
|
376 | throw createInternalServerFSPIOPError(`${errorMessage} - ${JSON.stringify(code)}.`)
|
377 | }
|
378 | }
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 | const validateFSPIOPErrorGroups = (code) => {
|
389 | const errorMessage = 'Validation failed due to error code being invalid'
|
390 | let codeToValidate
|
391 | if (typeof code === 'number' || typeof code === 'string') {
|
392 | codeToValidate = code
|
393 | } else if (typeof code === 'object' && code.code) {
|
394 | codeToValidate = code.code
|
395 | }
|
396 |
|
397 | const regex = /^(10|20|3[0-4]|4[0-4]|5[0-4])[0-9]{2}$/
|
398 | if (regex.test(codeToValidate)) {
|
399 | return true
|
400 | } else {
|
401 | throw createInternalServerFSPIOPError(`${errorMessage} - ${JSON.stringify(code)}.`)
|
402 | }
|
403 | }
|
404 |
|
405 | module.exports = {
|
406 | FSPIOPError,
|
407 | createFSPIOPError,
|
408 | createFSPIOPErrorFromJoiError,
|
409 | createFSPIOPErrorFromOpenapiError,
|
410 | createInternalServerFSPIOPError,
|
411 | createFSPIOPErrorFromErrorInformation,
|
412 | createFSPIOPErrorFromErrorCode,
|
413 | reformatFSPIOPError,
|
414 | validateFSPIOPErrorCode,
|
415 | validateFSPIOPErrorGroups
|
416 | }
|