Skip to content

Commit 865518a

Browse files
committed
create http error exception type and error
1 parent 21ec79a commit 865518a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+399
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HttpException } from "./http-exception";
2+
import { HttpError } from "../types/http-error";
3+
4+
export class BadGatewayException extends HttpException {
5+
constructor(message?: string | object | Error) {
6+
super(HttpError.BAD_GATEWAY, message);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HttpException } from "./http-exception";
2+
import { HttpError } from "../types/http-error";
3+
4+
export class BadRequestException extends HttpException {
5+
constructor(message?: string | object | Error) {
6+
super(HttpError.BAD_REQUEST, message);
7+
}
8+
}

src/exceptions/conflict-exception.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HttpException } from "./http-exception";
2+
import { HttpError } from "../types/http-error";
3+
4+
export class ConflictException extends HttpException {
5+
constructor(message?: string | object | Error) {
6+
super(HttpError.CONFLICT, message);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HttpException } from "./http-exception";
2+
import { HttpError } from "../types/http-error";
3+
4+
export class ExpectationFailedException extends HttpException {
5+
constructor(message?: string | object | Error) {
6+
super(HttpError.EXPECTATION_FAILED, message);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HttpException } from "./http-exception";
2+
import { HttpError } from "../types/http-error";
3+
4+
export class FailedDependencyException extends HttpException {
5+
constructor(message?: string | object | Error) {
6+
super(HttpError.FAILED_DEPENDENCY, message);
7+
}
8+
}

src/exceptions/forbidden-exception.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HttpException } from "./http-exception";
2+
import { HttpError } from "../types/http-error";
3+
4+
export class ForbiddenException extends HttpException {
5+
constructor(message?: string | object | Error) {
6+
super(HttpError.FORBIDDEN, message);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HttpException } from "./http-exception";
2+
import { HttpError } from "../types/http-error";
3+
4+
export class GatewayTimeoutException extends HttpException {
5+
constructor(message?: string | object | Error) {
6+
super(HttpError.GATEWAY_TIMEOUT, message);
7+
}
8+
}

src/exceptions/gone-exception.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HttpException } from "./http-exception";
2+
import { HttpError } from "../types/http-error";
3+
4+
export class GoneException extends HttpException {
5+
constructor(message?: string | object | Error) {
6+
super(HttpError.GONE, message);
7+
}
8+
}

src/exceptions/http-exception.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { HttpError } from '../types/http-error';
2+
3+
type Payload = { statusCode: number; code: string; message: string };
4+
5+
export class HttpException extends Error {
6+
public readonly statusCode: number;
7+
public readonly code: string;
8+
public readonly data?: unknown;
9+
public readonly isHttpException = true;
10+
11+
constructor(httpError: HttpError, message?: string | object | Error) {
12+
const { statusCode, code, message: defaultMsg } = JSON.parse(httpError) as Payload;
13+
14+
const finalMsg =
15+
message instanceof Error
16+
? message.message
17+
: typeof message === 'string'
18+
? message
19+
: defaultMsg;
20+
21+
super(finalMsg);
22+
this.name = new.target.name;
23+
24+
this.statusCode = statusCode; // number
25+
this.code = code; // string e.g. "INTERNAL_SERVER_ERROR"
26+
27+
if (message && typeof message === 'object' && !(message instanceof Error)) {
28+
this.data = message; // custom object dikembalikan apa adanya di toBody()
29+
}
30+
31+
// buat enumerable agar ikut saat JSON.stringify(err)
32+
Object.defineProperties(this, {
33+
statusCode: { enumerable: true },
34+
code: { enumerable: true },
35+
message: { enumerable: true },
36+
name: { enumerable: true },
37+
isHttpException: { enumerable: true },
38+
data: { enumerable: true },
39+
});
40+
}
41+
42+
toBody(): unknown {
43+
if (this.data && typeof this.data === 'object') return this.data;
44+
return { statusCode: this.statusCode, message: this.message };
45+
}
46+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { HttpException } from "./http-exception";
2+
import { HttpError } from "../types/http-error";
3+
4+
export class HttpVersionNotSupportedException extends HttpException {
5+
constructor(message?: string | object | Error) {
6+
super(HttpError.HTTP_VERSION_NOT_SUPPORTED, message);
7+
}
8+
}

0 commit comments

Comments
 (0)