forked from mojaloop/central-services-error-handling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
126 lines (105 loc) · 4.35 KB
/
Copy pathhandler.js
File metadata and controls
126 lines (105 loc) · 4.35 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*****
License
--------------
Copyright © 2017 Bill & Melinda Gates Foundation
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
http://www.apache.org/licenses/LICENSE-2.0
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.
Contributors
--------------
This is the official list of the Mojaloop project contributors for this file.
Names of the original copyright holders (individuals or organizations)
should be listed with a '*' in the first column. People who have
contributed from an organization can be listed under the organization
that actually holds the copyright for their contributions (see the
Gates Foundation organization for an example). Those individuals should have
their names indented and be marked with a '-'. Email address can be added
optionally within square brackets <email>.
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>
* ModusBox
- Neal Donnan <neal.donnan@modusbox.com>
- Rajiv Mothilal <rajiv.mothilal@modusbox.com>
--------------
******/
'use strict'
const Factory = require('./factory')
const Errors = require('./enums').FSPIOPErrorCodes
const getReplyToFromRequestHeaders = (request) => {
return (request.headers && request.headers['fspiop-source']) ? request.headers['fspiop-source'] : null
}
const createFSPIOPErrorFromErrorResponse = (request, response) => {
const fspiopError = ((httpStatusCode) => {
switch (httpStatusCode) {
case 400:
return Errors.CLIENT_ERROR
case 404:
return Errors.UNKNOWN_URI
case 415:
return Errors.MALFORMED_SYNTAX
default:
return Errors.INTERNAL_SERVER_ERROR
}
})(response.output.statusCode)
return Factory.createFSPIOPError(fspiopError, response.message, response.stack, getReplyToFromRequestHeaders(request))
}
const reformatError = (request, response) => {
if (!response.output) {
response.output = {}
}
let fspiopError
if (response.isJoi) {
const replyTo = getReplyToFromRequestHeaders(request)
fspiopError = Factory.createFSPIOPErrorFromJoiError(request.response.details[0], response, replyTo)
} else if (response.name === 'FSPIOPError') {
fspiopError = response
} else {
fspiopError = createFSPIOPErrorFromErrorResponse(request, response)
}
response.output.payload = fspiopError.toApiErrorObject()
if (fspiopError.httpStatusCode) {
response.output.statusCode = fspiopError.httpStatusCode
}
if (!response.output.statusCode) {
response.output.statusCode = 500
}
}
/**
* Function to be used to handle the 'onPreResponse' Hapi server extension.
* This reformats the standard Boom and Joi error output to a compliant error
* format as per section 7.6 of "API Definition v1.0.docx".
*
* @param request the http request
* @param reply
* @returns {boolean|reply.continue|continue|((key?: IDBValidKey) => void)}
*/
exports.onPreResponse = function (request, reply) {
const response = request.response
if (response instanceof Error || response.isBoom) {
reformatError(request, response)
}
return reply.continue
}
/**
* Function to be used to handle the 'validateIncomingErrorCode' Hapi server extension.
* This validates the error is a FSPIOPError and that it contains a valid error code
* format as per section 7.6 of "API Definition v1.0.docx".
*
* @param request the http request
* @param h
* @returns {boolean|h.continue|continue|((key?: IDBValidKey) => void)}
*/
exports.validateIncomingErrorCode = function (request, h) {
const incomingErrorCode = request.payload.errorInformation.errorCode
try {
if (Factory.validateFSPIOPErrorCode(incomingErrorCode).code === incomingErrorCode) {
return h.continue
}
} catch (err) {
const onPreHandlerApiErrorObject = Factory.createFSPIOPError(Errors.VALIDATION_ERROR, `The incoming error code: ${incomingErrorCode} is not a valid mojaloop specification error code`).toApiErrorObject()
return h
.response(onPreHandlerApiErrorObject)
.code(400)
.takeover()
}
}