UNPKG

8.95 kBJavaScriptView Raw
1/*****
2 License
3 --------------
4 Copyright © 2017 Bill & Melinda Gates Foundation
5 The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10
11 Contributors
12 --------------
13 This is the official list of the Mojaloop project contributors for this file.
14 Names of the original copyright holders (individuals or organizations)
15 should be listed with a '*' in the first column. People who have
16 contributed from an organization can be listed under the organization
17 that actually holds the copyright for their contributions (see the
18 Gates Foundation organization for an example). Those individuals should have
19 their names indented and be marked with a '-'. Email address can be added
20 optionally within square brackets <email>.
21
22 * Gates Foundation
23 - Name Surname <name.surname@gatesfoundation.com>
24
25 * ModusBox
26 - Neal Donnan <neal.donnan@modusbox.com>
27 - Juan Correa <juan.correa@modusbox.com>
28 - Miguel de Barros <miguel.debarros@modusbox.com>
29
30 --------------
31 ******/
32
33'use strict'
34
35const _ = require('lodash')
36
37const MojaloopSDKError = require('@mojaloop/sdk-standard-components').Errors
38
39/**
40 * Mojaloop API spec error type enums
41 */
42const 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 * Mojaloop API Error Codes Override
127 */
128const MojaloopApiErrorCodesOverride = {
129 // INTERNAL_SERVER_ERROR: { httpStatusCode: 500 }, // Example of overriding default or undefined values for INTERNAL_SERVER_ERROR error
130 // CUSTOM_ERROR: { code: '3241', description: 'Error text' } // Example of adding new CUSTOM_ERROR with default MojaloopErrorType.httpStatusCode
131}
132
133/**
134 * Returns an object representing a Mojaloop API spec error code combined with overrides
135 *
136 * @param errorCodes {object} - Mojaloop API spec error code enums
137 * @param override {object} - Override enum
138 * @returns {object} - Object representing the Mojaloop API spec combined with overrides
139 */
140const 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 * Returns an object representing a Mojaloop API spec error code combined with error types enums
156 *
157 * @param errorCodes {object} - Mojaloop API spec error code enums
158 * @param errorTypes {object} - Mojaloop API spec error type enums
159 * @returns {object} - Object representing the Mojaloop API spec error enums with associated types
160 */
161const 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 * Returns an object representing a Mojaloop API spec error code map using the error code as the key
183 *
184 * @param errorCodes {object} - Mojaloop API spec error code enums
185 * @returns {object} - Object representing a Mojaloop API Error map using the error code as the key with each record having the associated name
186 */
187const 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 * Mojaloop API spec error enums with merged overrides
199 */
200let FSPIOPErrorCodes = populateOverrides(MojaloopSDKError.MojaloopApiErrorCodes, MojaloopApiErrorCodesOverride)
201
202/**
203 * Mojaloop API spec error enums with associated type enums
204 */
205FSPIOPErrorCodes = populateErrorTypes(FSPIOPErrorCodes, MojaloopTypes)
206
207/**
208 * Mojaloop API spec error map enums with associated type enums by error code as the key map
209 */
210const FSPIOPErrorCodeMap = errorCodesToMap(FSPIOPErrorCodes)
211
212/**
213 * Returns an object representing a Mojaloop API spec error object given its error code using super fast hash lookup
214 *
215 * @param code {number/string} - Mojaloop API spec error code (four digit integer as number or string)
216 * @returns {object} - Object representing the Mojaloop API spec error
217 */
218const 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 * Returns an object representing a Mojaloop API spec error code combined with error types enums
233 *
234 * @param errorCode {string/number} - Mojaloop API spec error code enums
235 * @returns {object} - Object representing the Mojaloop API spec error enums with associated types
236 */
237const 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 * Mojaloop API spec Model Types related to ErrorInformation
251 */
252const 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
271const Internal = {
272 FSPIOPError: {
273 ExtensionsKeys: {
274 cause: 'cause',
275 _cause: '_cause'
276 }
277 }
278}
279
280module.exports = {
281 FSPIOPErrorCodes,
282 FSPIOPErrorTypes: MojaloopTypes,
283 FSPIOPErrorCodeMap,
284 findFSPIOPErrorCode,
285 findErrorType,
286 MojaloopModelTypes,
287 Internal,
288 _populateOverrides: populateOverrides
289}