Skip to content

Commit

Permalink
common: update http error message
Browse files Browse the repository at this point in the history
  • Loading branch information
josephjclark committed Jan 11, 2024
1 parent 1b72f07 commit 623aa4c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
7 changes: 4 additions & 3 deletions packages/common/src/util/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ const assertOK = (response, errorMap, fullUrl, method, startTime) => {
: errMapMessage || response.statusCode >= 400;

if (isError) {
const defaultErrorMesssage = `${method} ${fullUrl} - ${response.statusCode} ${statusText}`;
const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;

const duration = startTime - Date.now();
const duration = Date.now() - startTime;

const errMessage =
typeof errMapMessage === 'function'
? errMapMessage(response)
: errMapMessage || defaultErrorMesssage;

const error = new Error(errMessage);
error.code = response.statusCode;
error.statusCode = response.statusCode;
error.statusMessage = statusText;
error.url = fullUrl;
error.duration = duration;
error.method = method;
Expand Down
20 changes: 10 additions & 10 deletions packages/common/test/util/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,14 @@ describe('options', () => {
it('should use errorMap with function', async () => {
client
.intercept({
path: '/api/noAccess',
path: '/api/no-access',
method: 'GET',
})
.reply(404, { error: 'Not Found' });

let error = null;
try {
await request('GET', 'https://www.example.com/api/noAccess', {
await request('GET', 'https://www.example.com/api/no-access', {
errors: {
404: response => (response.context ? 'No Access' : 'Not Found'),
},
Expand All @@ -427,20 +427,20 @@ describe('options', () => {
it('should throw by default if a 404', async () => {
client
.intercept({
path: '/api/noAccess',
path: '/api/no-access',
method: 'GET',
})
.reply(404, { error: 'Not Found' });

let error = null;
try {
await request('GET', 'https://www.example.com/api/noAccess');
await request('GET', 'https://www.example.com/api/no-access');
} catch (err) {
error = err;
}

expect(error.message).to.eql(
'GET https://www.example.com/api/noAccess - 404 Not Found'
'GET to https://www.example.com/api/no-access returned 404: Not Found'
);
});

Expand Down Expand Up @@ -470,7 +470,7 @@ describe('options', () => {
it('should throw and use errorMap string value', async () => {
client
.intercept({
path: '/api/noAccess',
path: '/api/no-access',
method: 'GET',
})
.reply(
Expand All @@ -483,7 +483,7 @@ describe('options', () => {

let error = null;
try {
await request('GET', 'https://www.example.com/api/noAccess', {
await request('GET', 'https://www.example.com/api/no-access', {
errors: {
404: 'No Access',
},
Expand All @@ -498,7 +498,7 @@ describe('options', () => {
it('should throw and use default values', async () => {
client
.intercept({
path: '/api/noAccess',
path: '/api/no-access',
method: 'GET',
})
.reply(
Expand All @@ -511,12 +511,12 @@ describe('options', () => {

let error = null;
try {
await await request('GET', 'https://www.example.com/api/noAccess');
await await request('GET', 'https://www.example.com/api/no-access');
} catch (err) {
error = err;
}
expect(error.message).to.eql(
'GET https://www.example.com/api/noAccess - 405 Method Not Allowed'
'GET to https://www.example.com/api/no-access returned 405: Method Not Allowed'
);
});
});
Expand Down

0 comments on commit 623aa4c

Please sign in to comment.