Skip to content

Commit 84b50ba

Browse files
authored
Improve HTTP error messages (#485)
* Improve superagent errors * Update expected error response
1 parent 9dd99e9 commit 84b50ba

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/client/urlTokenBaseHTTPClient.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
8585
return res;
8686
}
8787

88+
/**
89+
* Make a superagent error more readable. For more info, see https://github.com/visionmedia/superagent/issues/1074
90+
*/
91+
private static formatSuperagentError(err: any): Error {
92+
if (err.response) {
93+
try {
94+
const decoded = JSON.parse(Buffer.from(err.response.body).toString());
95+
// eslint-disable-next-line no-param-reassign
96+
err.message = `Network request error. Received status ${err.response.status}: ${decoded.message}`;
97+
} catch (err2) {
98+
// ignore any error that happened while we are formatting the original error
99+
}
100+
}
101+
return err;
102+
}
103+
88104
async get(
89105
relativePath: string,
90106
query?: Query<string>,
@@ -98,8 +114,12 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
98114
.responseType('arraybuffer')
99115
.query(query);
100116

101-
const res = await r;
102-
return URLTokenBaseHTTPClient.superagentToHTTPClientResponse(res);
117+
try {
118+
const res = await r;
119+
return URLTokenBaseHTTPClient.superagentToHTTPClientResponse(res);
120+
} catch (err) {
121+
throw URLTokenBaseHTTPClient.formatSuperagentError(err);
122+
}
103123
}
104124

105125
async post(
@@ -118,8 +138,12 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
118138
.responseType('arraybuffer')
119139
.send(Buffer.from(data)); // Buffer.from necessary for superagent
120140

121-
const res = await r;
122-
return URLTokenBaseHTTPClient.superagentToHTTPClientResponse(res);
141+
try {
142+
const res = await r;
143+
return URLTokenBaseHTTPClient.superagentToHTTPClientResponse(res);
144+
} catch (err) {
145+
throw URLTokenBaseHTTPClient.formatSuperagentError(err);
146+
}
123147
}
124148

125149
async delete(
@@ -138,7 +162,11 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
138162
.responseType('arraybuffer')
139163
.send(Buffer.from(data)); // Buffer.from necessary for superagent
140164

141-
const res = await r;
142-
return URLTokenBaseHTTPClient.superagentToHTTPClientResponse(res);
165+
try {
166+
const res = await r;
167+
return URLTokenBaseHTTPClient.superagentToHTTPClientResponse(res);
168+
} catch (err) {
169+
throw URLTokenBaseHTTPClient.formatSuperagentError(err);
170+
}
143171
}
144172
}

tests/cucumber/steps/steps.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,7 @@ module.exports = function getSteps(options) {
18741874
throw err;
18751875
}
18761876
if (this.expectedMockResponseCode === 500) {
1877-
if (!err.toString().includes('Internal Server Error')) {
1877+
if (!err.toString().includes('Received status 500')) {
18781878
throw Error(
18791879
`expected response code 500 implies error Internal Server Error but instead had error: ${err}`
18801880
);

0 commit comments

Comments
 (0)