Skip to content

Commit

Permalink
feat: add exception code
Browse files Browse the repository at this point in the history
  • Loading branch information
bingtsingw committed Jul 2, 2023
1 parent 3fb58f2 commit 9acfbca
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/exception/400.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from './base';

export class BadRequestException extends BaseException {
public constructor(messages?: unknown, code?: string) {
public constructor(messages?: unknown, code = 'BadRequestException') {
super(400, messages, code);
}
}
2 changes: 1 addition & 1 deletion src/exception/401.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from './base';

export class UnauthorizedException extends BaseException {
public constructor(messages?: unknown, code?: string) {
public constructor(messages: unknown, code = 'UnauthorizedException') {
super(401, messages, code);
}
}
8 changes: 7 additions & 1 deletion src/exception/403.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { BaseException } from './base';

export class ForbiddenException extends BaseException {
public constructor(messages?: unknown, code?: string) {
public constructor(messages: unknown, code = 'ForbiddenException') {
super(403, messages, code);
}
}

export class NeedVipException extends BaseException {
public constructor(messages: unknown, code = 'NeedVip') {
super(403, messages, code);
}
}
2 changes: 1 addition & 1 deletion src/exception/404.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from './base';

export class NotFoundException extends BaseException {
public constructor(messages?: unknown, code?: string) {
public constructor(messages: unknown, code = 'NotFoundException') {
super(404, messages, code);
}
}
2 changes: 1 addition & 1 deletion src/exception/4xx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from './base';

export class ClientErrorException extends BaseException {
public constructor(message: string, code?: string) {
public constructor(message: string, code = 'ClientErrorException') {
super(400, message, code);
}
}
2 changes: 1 addition & 1 deletion src/exception/500.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from './base';

export class InternalErrorException extends BaseException {
public constructor(message: string, code?: string) {
public constructor(message: string, code = 'InternalErrorException') {
super(500, message, code);
}
}
2 changes: 1 addition & 1 deletion src/exception/5xx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from './base';

export class ServerErrorException extends BaseException {
public constructor(message: string, code?: string) {
public constructor(message: string, code = 'ServerErrorException') {
super(500, message, code);
}
}
23 changes: 7 additions & 16 deletions src/exception/base.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { get, replace, snakeCase, startCase } from 'lodash';

export class BaseException extends Error {
public name: string;
public status: number;
public code: string;
public message: string;

private _messages: unknown;
private _code: string;
private _status: number;

public constructor(status: number, messages?: unknown, code?: string) {
public constructor(status: number, messages: unknown, code: string) {
super();
this._status = status;
this.status = status;

const baseName = startCase(replace(this.name, /Exception$/, ''));

this._code = snakeCase(`E ${code ? code : baseName}`).toUpperCase();
this._messages = messages ? messages : baseName;

this.message = `${this._code}: ${this.getFirstMessage()}`;
this.name = this.constructor.name;
this.code = snakeCase(`E ${startCase(replace(code, /Exception$/, ''))}`).toUpperCase();
this._messages = messages;
this.message = this.getFirstMessage();

Error.captureStackTrace(this, this.constructor);
}
Expand All @@ -36,8 +31,4 @@ export class BaseException extends Error {

return '';
}

public getStatus(): number {
return this._status;
}
}
3 changes: 2 additions & 1 deletion src/exception/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BadRequestException } from './400';
import { UnauthorizedException } from './401';
import { ForbiddenException } from './403';
import { ForbiddenException, NeedVipException } from './403';
import { NotFoundException } from './404';
import { ClientErrorException } from './4xx';
import { InternalErrorException } from './500';
Expand All @@ -19,5 +19,6 @@ export const exception = {
BadRequestException,
UnauthorizedException,
ForbiddenException,
NeedVipException,
NotFoundException,
};

0 comments on commit 9acfbca

Please sign in to comment.