Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
feat: Add HttpError Class
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Dec 29, 2020
1 parent 1d2cb2e commit cd25ef0
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxify/http",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.2",
"description": "The Foxify HTTP module",
"author": {
"name": "Ardalan Amini",
Expand Down
51 changes: 51 additions & 0 deletions src/errors/HttpError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { STATUS_CODES } from "http";
import { STATUS, StatusT } from "../constants";

export default class HttpError extends Error {
public readonly statusCode: StatusT;

public readonly details: Record<string, unknown>;

constructor(status: StatusT);
constructor(message: string, status?: StatusT);
constructor(details: Record<string, unknown>, status?: StatusT);
constructor(
message: string,
details: Record<string, unknown>,
status?: StatusT,
);
constructor(
message: string | Record<string, unknown> | StatusT,
details: Record<string, unknown> | StatusT = {},
status: StatusT | Record<string, unknown> = STATUS.INTERNAL_SERVER_ERROR,
) {
switch (typeof message) {
case "string": {
if (typeof details === "number") {
status = details;
details = {};
}

break;
}
case "number": {
status = message;
message = STATUS_CODES[status] || `${status}`;

break;
}
default: {
status = details as StatusT;
details = message;
message = STATUS_CODES[status] || `${status}`;

break;
}
}

super(message);

this.statusCode = status as StatusT;
this.details = details as Record<string, unknown>;
}
}
9 changes: 9 additions & 0 deletions src/errors/MethodNotAllowed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import HttpError from "./HttpError";
import { STATUS } from "../constants";
import { STATUS_CODES } from "http";

export default class MethodNotAllowed extends HttpError {
constructor(message: string = STATUS_CODES[STATUS.METHOD_NOT_ALLOWED]!) {
super(message, STATUS.METHOD_NOT_ALLOWED);
}
}
9 changes: 9 additions & 0 deletions src/errors/NotFound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import HttpError from "./HttpError";
import { STATUS } from "../constants";
import { STATUS_CODES } from "http";

export default class NotFound extends HttpError {
constructor(message: string = STATUS_CODES[STATUS.NOT_FOUND]!) {
super(message, STATUS.NOT_FOUND);
}
}
3 changes: 3 additions & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as HttpError } from "./HttpError";
export { default as MethodNotAllowed } from "./MethodNotAllowed";
export { default as NotFound } from "./NotFound";
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./constants";
export * from "./errors";
export {
default as Request,
settings as requestSettings,
Expand Down

0 comments on commit cd25ef0

Please sign in to comment.