This repository was archived by the owner on Nov 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d2cb2e
commit cd25ef0
Showing
7 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters