-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathhttp.js
98 lines (86 loc) · 3.76 KB
/
http.js
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
// (C)2018 ModusBox Inc.
/*****
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.
Initial contribution
--------------------
The initial functionality and code base was donated by the Mowali project working in conjunction with MTN and Orange as service provides.
* Project: Mowali
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
- Georgi Georgiev <georgi.georgiev@modusbox.com>
- Henk Kodde <henk.kodde@modusbox.com>
- Matt Kingston <matt.kingston@modusbox.com>
- Vassilis Barzokas <vassilis.barzokas@modusbox.com>
--------------
******/
const http = require('node:http')
const util = require('node:util')
const axios = require('axios')
const ErrorHandler = require('@mojaloop/central-services-error-handling')
const { logger } = require('../lib')
const { getStackOrInspect } = require('../lib/util')
axios.defaults.httpAgent = new http.Agent({ keepAlive: true })
axios.defaults.httpAgent.toJSON = () => ({})
/**
* Encapsulates making an HTTP request and translating any error response into a domain-specific
* error type.
*
* @param {Object} opts
* @param {String} fspiopSource
* @returns {Promise<void>}
*/
async function httpRequest (opts, fspiopSource) {
// Network errors lob an exception. Bear in mind 3xx 4xx and 5xx are not network errors so we
// need to wrap the request below in a `try catch` to handle network errors
let res
let body
const log = logger.child({ context: 'httpRequest', fspiopSource, opts })
log.debug('httpRequest is started...')
try {
res = await axios.request(opts)
body = await res.data
log.debug('httpRequest is finished', { body })
} catch (e) {
log.error('httpRequest is failed due to error:', e)
const [fspiopErrorType, fspiopErrorDescr] = e.response && e.response.status === 404
? [ErrorHandler.Enums.FSPIOPErrorCodes.CLIENT_ERROR, 'Not found']
: [ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_COMMUNICATION_ERROR, 'Network error']
throw ErrorHandler.CreateFSPIOPError(fspiopErrorType, fspiopErrorDescr,
`${getStackOrInspect(e)}. Opts: ${util.inspect(opts)}`,
fspiopSource)
}
// handle non network related errors below
if (res.status < 200 || res.status >= 300) {
const errObj = {
opts,
status: res.status,
statusText: res.statusText,
body
}
log.warn('httpRequest returned non-success status code', errObj)
throw ErrorHandler.CreateFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_COMMUNICATION_ERROR,
'Non-success response in HTTP request',
`${errObj}`,
fspiopSource)
}
return body
}
module.exports = {
httpRequest
}