Skip to content

Commit 65e3d9b

Browse files
committed
Use Studio Fail
1 parent ddbd041 commit 65e3d9b

File tree

4 files changed

+54
-28
lines changed

4 files changed

+54
-28
lines changed

lib/request.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const url = require('url');
99
const http = require('http');
1010
const https = require('https');
1111
const logger = require('@studio/log');
12+
const { failure, E_FAILED } = require('@studio/fail');
1213

1314
const logger_name = 'Request';
1415
const default_log = logger(logger_name);
@@ -19,12 +20,11 @@ const PROTOCOLS = {
1920
};
2021

2122
function expectError(expect, status) {
22-
const err = new Error(
23-
`Expected response statusCode to be ${expect}, but was ${status}`
23+
return failure(
24+
`Expected response statusCode to be ${expect}, but was ${status}`,
25+
'E_EXPECT',
26+
{ statusCode: status }
2427
);
25-
err.code = 'E_EXPECT';
26-
err.statusCode = status;
27-
return err;
2828
}
2929

3030
function copy(obj) {
@@ -39,7 +39,7 @@ module.exports = function fetch(options, data, callback) {
3939
const protocol = options.protocol || 'https:';
4040
const httpx = PROTOCOLS[protocol];
4141
if (!httpx) {
42-
throw new Error(`Unsupported protocol "${protocol}"`);
42+
throw failure(`Unsupported protocol "${protocol}"`);
4343
}
4444

4545
const opts = copy(options);
@@ -151,9 +151,7 @@ module.exports = function fetch(options, data, callback) {
151151
return;
152152
}
153153
res.on('error', (res_err) => {
154-
const e = new Error('Response failure');
155-
e.code = 'E_ERROR';
156-
e.cause = res_err;
154+
const e = failure('Response failure', res_err, E_FAILED);
157155
const ms_body = Date.now() - ts_head;
158156
log.error({ request, ms_head, ms_body, response }, res_err);
159157
callback(e);
@@ -181,10 +179,14 @@ module.exports = function fetch(options, data, callback) {
181179
try {
182180
json = JSON.parse(body);
183181
} catch (e) {
184-
e.code = 'E_JSON';
182+
const json_err = failure(
183+
'Failed to parse response body',
184+
/** @type {Error} */ (e),
185+
'E_JSON'
186+
);
185187
response.body = body;
186188
log.error({ request, ms_head, ms_body, response });
187-
callback(e, body, res);
189+
callback(json_err, body, res);
188190
return;
189191
}
190192
response.json = json;
@@ -197,8 +199,7 @@ module.exports = function fetch(options, data, callback) {
197199
if (timeout) {
198200
timer = setTimeout(() => {
199201
req.destroy();
200-
const err = new Error('Request timeout');
201-
err.code = 'E_TIMEOUT';
202+
const err = failure('Request timeout', 'E_TIMEOUT');
202203
log.warn({ ms: Date.now() - ts_start, request });
203204
callback(err);
204205
callback = null;
@@ -216,9 +217,7 @@ module.exports = function fetch(options, data, callback) {
216217
}
217218

218219
req.on('error', (req_err) => {
219-
const e = new Error('Request failure');
220-
e.code = 'E_ERROR';
221-
e.cause = req_err;
220+
const e = failure('Request failure', req_err, E_FAILED);
222221
if (timer) {
223222
clearTimeout(timer);
224223
}

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"extends": "@studio"
2929
},
3030
"dependencies": {
31+
"@studio/fail": "^1.8.0",
3132
"@studio/log": "^2.1.2"
3233
},
3334
"devDependencies": {

test/request-test.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,11 @@ describe('request', () => {
182182

183183
assert.calledWith(fake, match({
184184
message: 'Expected response statusCode to be 2xx, but was 199',
185-
code: 'E_EXPECT'
185+
code: 'E_EXPECT',
186+
properties: {
187+
statusCode: 199
188+
}
186189
}), null, res);
187-
assert.equals(fake.firstCall.args[0].statusCode, 199);
188190
});
189191

190192
it('fails the request if `statusCode` is > 299', () => {
@@ -197,9 +199,11 @@ describe('request', () => {
197199

198200
assert.calledOnceWith(fake, match({
199201
message: 'Expected response statusCode to be 2xx, but was 300',
200-
code: 'E_EXPECT'
202+
code: 'E_EXPECT',
203+
properties: {
204+
statusCode: 300
205+
}
201206
}), null, res);
202-
assert.equals(fake.firstCall.args[0].statusCode, 300);
203207
});
204208

205209
it('does not fail the request if `statusCode` is 201', () => {
@@ -223,12 +227,14 @@ describe('request', () => {
223227

224228
assert.calledOnceWith(fake, match({
225229
message: 'Expected response statusCode to be 200, but was 201',
226-
code: 'E_EXPECT'
230+
code: 'E_EXPECT',
231+
properties: {
232+
statusCode: 201
233+
}
227234
}), null, res);
228235
refute.calledWith(https.request, match({
229236
expect: 200
230237
}));
231-
assert.equals(fake.firstCall.args[0].statusCode, 201);
232238
});
233239

234240
it('does not fail request if `statusCode` equals `expect`', () => {
@@ -254,9 +260,11 @@ describe('request', () => {
254260
assert.calledOnceWith(fake, match({
255261
message: 'Expected response statusCode to be one of [200, 201], '
256262
+ 'but was 202',
257-
code: 'E_EXPECT'
263+
code: 'E_EXPECT',
264+
properties: {
265+
statusCode: 202
266+
}
258267
}), null, res);
259-
assert.equals(fake.firstCall.args[0].statusCode, 202);
260268
});
261269

262270
it('does not fail request if `statusCode` is in `expect` array', () => {
@@ -280,9 +288,13 @@ describe('request', () => {
280288
res.on.withArgs('end').yield();
281289

282290
assert.calledOnceWith(fake, match({
283-
name: 'SyntaxError',
284-
message: sinon.match('Unexpected token'),
285-
code: 'E_JSON'
291+
name: 'Error',
292+
message: 'Failed to parse response body',
293+
code: 'E_JSON',
294+
cause: match({
295+
name: 'SyntaxError',
296+
message: sinon.match('Unexpected token')
297+
})
286298
}), '<html/>', res);
287299
});
288300

@@ -354,7 +366,7 @@ describe('request', () => {
354366
clock.tick(5000);
355367
assert.calledOnceWithMatch(fake, {
356368
message: 'Request failure',
357-
code: 'E_ERROR',
369+
code: 'E_FAILED',
358370
cause: error
359371
});
360372
});

0 commit comments

Comments
 (0)