Skip to content

Commit

Permalink
feat: add HTTP error
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysomallos committed Jul 19, 2024
1 parent a198eae commit aec1b05
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"object-hash": "^3.0.0"
},
"devDependencies": {
"@babel/eslint-parser": "^7.24.7",
"@babel/core": "^7.24.9",
"@babel/eslint-parser": "^7.24.8",
"@babel/plugin-syntax-import-assertions": "^7.24.7",
"@eslint/js": "^9.5.0",
"c8": "^10.1.2",
Expand Down
10 changes: 5 additions & 5 deletions test/request.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import quibble from 'quibble';
const expectedResponse = {success: true};

describe('[Utils] request', function () {
let request, buffer;
let request, buffer, HttpError;

beforeEach(async function () {
buffer = Buffer.from(JSON.stringify(expectedResponse));
Expand All @@ -17,7 +17,7 @@ describe('[Utils] request', function () {
response.headers = {
'content-type': 'text/json',
'content-length': `${response.statusCode === 200 ? buffer.length : 0}`,
connection: 'close'
connection: 'close',
};
callback(response);
return {
Expand All @@ -33,7 +33,7 @@ describe('[Utils] request', function () {
},
},
});
request = (await import('../utils/request.mjs')).default;
({default: request, HttpError} = await import('../utils/request.mjs'));
});

it('should make a successful HTTP GET request', async function () {
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('[Utils] request', function () {
method: 'GET',
};

await assert.rejects(request(options), Error);
await assert.rejects(request(options), HttpError);
});

it('should reject the promise on HTTP error for invalid json response', async function () {
Expand All @@ -75,7 +75,7 @@ describe('[Utils] request', function () {
method: 'GET',
};

await assert.rejects(request(options), Error);
await assert.rejects(request(options), SyntaxError);
});

afterEach(function () {
Expand Down
24 changes: 22 additions & 2 deletions utils/request.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import https from 'node:https';

/**
* Base class for HTTP errors.
* Extends the built-in Error class to include an HTTP status code.
*/
export class HttpError extends Error {
/**
* Constructs a new HttpError instance.
* @param {number} status The HTTP status code.
* @param {string} message The error message.
*/
constructor(status, message) {
super(message);
this.status = status;
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}

/**
* Makes an HTTP request with the provided options and body.
* @template T
Expand All @@ -10,8 +28,10 @@ import https from 'node:https';
export default async function request(options, body) {
return new Promise(function (resolve, reject) {
const request = https.request(options, response => {
const {statusCode} = response;
if (statusCode < 200 || statusCode >= 300) return reject(new Error(`HTTP Error with status ${statusCode}`));
const {statusCode, statusMessage} = response;
if (statusCode < 200 || statusCode >= 300) {
return reject(new HttpError(statusCode, statusMessage));
}
let chunks = [];
response.on('data', chunk => chunks.push(chunk));
response.on('end', () => {
Expand Down

0 comments on commit aec1b05

Please sign in to comment.