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 | 'use strict'
|
33 |
|
34 | const Factory = require('./factory')
|
35 | const Errors = require('./enums').FSPIOPErrorCodes
|
36 |
|
37 | const getReplyToFromRequestHeaders = (request) => {
|
38 | return (request.headers && request.headers['fspiop-source']) ? request.headers['fspiop-source'] : null
|
39 | }
|
40 |
|
41 | const createFSPIOPErrorFromErrorResponse = (request, response) => {
|
42 | const fspiopError = ((response, request) => {
|
43 | switch (response.output.statusCode) {
|
44 | case 400:
|
45 | return Errors.CLIENT_ERROR
|
46 | case 404:
|
47 | {
|
48 | const matches = findMatches(request)
|
49 |
|
50 | if (matches.length > 0) {
|
51 | const allowedHeaderValues = getAllowHeaders(matches)
|
52 | response.message = ''
|
53 | response.output.headers.Allow = allowedHeaderValues
|
54 | return Errors.METHOD_NOT_ALLOWED
|
55 | } else {
|
56 | return Errors.UNKNOWN_URI
|
57 | }
|
58 | }
|
59 | case 415:
|
60 | return Errors.MALFORMED_SYNTAX
|
61 |
|
62 | default:
|
63 | return Errors.INTERNAL_SERVER_ERROR
|
64 | }
|
65 | })(response, request)
|
66 |
|
67 | return Factory.createFSPIOPError(fspiopError, response.message, response.stack, getReplyToFromRequestHeaders(request))
|
68 | }
|
69 |
|
70 | const findMatches = (request) => {
|
71 | const server = request.server
|
72 | const path = request.path
|
73 | const matches = []
|
74 | const methods = ['get', 'post', 'put', 'patch', 'delete']
|
75 |
|
76 | for (const i in methods) {
|
77 | const match = server.match(methods[i], path)
|
78 | if (match != null) {
|
79 | const data = { method: match.method }
|
80 | matches.push(data)
|
81 | }
|
82 | }
|
83 |
|
84 | return matches
|
85 | }
|
86 |
|
87 | const getAllowHeaders = (matches) => {
|
88 | let headers = ''
|
89 |
|
90 | for (const i in matches) {
|
91 | const method = matches[i].method
|
92 |
|
93 | if (headers === '') {
|
94 | headers += method
|
95 | } else {
|
96 | headers += ',' + method
|
97 | }
|
98 | }
|
99 |
|
100 | return headers
|
101 | }
|
102 |
|
103 | const reformatError = (request, response) => {
|
104 | if (!response.output) {
|
105 | response.output = {}
|
106 | }
|
107 |
|
108 | let fspiopError
|
109 | if (response.isJoi) {
|
110 | const replyTo = getReplyToFromRequestHeaders(request)
|
111 | fspiopError = Factory.createFSPIOPErrorFromJoiError(request.response.details[0], response, replyTo)
|
112 | } else if (response.name === 'FSPIOPError') {
|
113 | fspiopError = response
|
114 | } else {
|
115 | fspiopError = createFSPIOPErrorFromErrorResponse(request, response)
|
116 | }
|
117 |
|
118 | response.output.payload = fspiopError.toApiErrorObject()
|
119 | if (fspiopError.httpStatusCode) {
|
120 | response.output.statusCode = fspiopError.httpStatusCode
|
121 | }
|
122 |
|
123 | if (!response.output.statusCode) {
|
124 | response.output.statusCode = 500
|
125 | }
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | const onPreResponse = (request, reply) => {
|
138 | const response = request.response
|
139 | if (response instanceof Error || response.isBoom) {
|
140 | reformatError(request, response)
|
141 | }
|
142 |
|
143 | return reply.continue
|
144 | }
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | const validateIncomingErrorCode = (request, h) => {
|
156 | const incomingErrorCode = request.payload.errorInformation.errorCode
|
157 | try {
|
158 | if (Factory.validateFSPIOPErrorCode(incomingErrorCode).code === incomingErrorCode) {
|
159 | return h.continue
|
160 | }
|
161 | } catch (err) {
|
162 | try {
|
163 | if (Factory.validateFSPIOPErrorGroups(incomingErrorCode)) {
|
164 | return h.continue
|
165 | }
|
166 | } catch (err) {
|
167 | const onPreHandlerApiErrorObject = Factory.createFSPIOPError(Errors.VALIDATION_ERROR, `The incoming error code: ${incomingErrorCode} is not a valid mojaloop specification error code`).toApiErrorObject()
|
168 | return h
|
169 | .response(onPreHandlerApiErrorObject)
|
170 | .code(400)
|
171 | .takeover()
|
172 | }
|
173 | }
|
174 | }
|
175 |
|
176 | module.exports = {
|
177 | validateIncomingErrorCode,
|
178 | onPreResponse,
|
179 | createFSPIOPErrorFromErrorResponse,
|
180 | getAllowHeaders,
|
181 | findMatches
|
182 | }
|