// 错误接口
interface Error {
name: string;
message: string;
stack?: string;
}
// 错误构造器接口
interface ErrorConstructor {
new(message?: string): Error;
(message?: string): Error;
readonly prototype: Error;
}
// 错误类
declare var Error: ErrorConstructor;
// Http 异常类
export class HttpException extends Error {
public readonly message: any;
constructor(
private readonly response: string | object,
private readonly status: number,
) {
super();
this.message = response;
}
// 获取响应对象
public getResponse(): string | object {
return this.response;
}
// 获取状态码
public getStatus(): number {
return this.status;
}
// 转变成字符串
public toString(): string {
const message = this.getErrorString(this.message);
return `Error: ${message}`;
}
// 获取错误字符串
private getErrorString(target: string | object): string {
return isString(target) ? target : JSON.stringify(target);
}
// 静态方法创建 body
public static createBody(
message: object | string,
error?: string,
statusCode?: number,
) {
if (!message) {
return { statusCode, error };
}
return isObject(message) && !Array.isArray(message)
? message
: { statusCode, error, message };
}
}
// HttpStatus.BAD_REQUEST 是 400
export class BadRequestException extends HttpException {
constructor(message?: string | object | any, error = 'Bad Request') {
super(
HttpException.createBody(message, error, HttpStatus.BAD_REQUEST),
HttpStatus.BAD_REQUEST,
);
}
}
// ConflictException 是 409
export class ConflictException extends HttpException {
constructor(message?: string | object | any, error = 'Conflict') {
super(
HttpException.createBody(message, error, HttpStatus.CONFLICT),
HttpStatus.CONFLICT,
);
}
}
// ForbiddenException 是 403
export class ForbiddenException extends HttpException {
constructor(message?: string | object | any, error = 'Forbidden') {
super(
HttpException.createBody(message, error, HttpStatus.FORBIDDEN),
HttpStatus.FORBIDDEN,
);
}
}
// GatewayTimeoutException 504
export class GatewayTimeoutException extends HttpException {
constructor(message?: string | object | any, error = 'Gateway Timeout') {
super(
HttpException.createBody(message, error, HttpStatus.GATEWAY_TIMEOUT),
HttpStatus.GATEWAY_TIMEOUT,
);
}
}
// GoneException 401
export class GoneException extends HttpException {
constructor(message?: string | object | any, error = 'Gone') {
super(
HttpException.createBody(message, error, HttpStatus.GONE),
HttpStatus.GONE,
);
}
}
// 505
export class HttpVersionNotSupportedException extends HttpException {
constructor(
message?: string | object | any,
error = 'HTTP Version Not Supported',
) {
super(
HttpException.createBody(
message,
error,
HttpStatus.HTTP_VERSION_NOT_SUPPORTED,
),
HttpStatus.HTTP_VERSION_NOT_SUPPORTED,
);
}
}
// 418
export class ImATeapotException extends HttpException {
constructor(message?: string | object | any, error = `I'm a teapot`) {
super(
HttpException.createBody(message, error, HttpStatus.I_AM_A_TEAPOT),
HttpStatus.I_AM_A_TEAPOT,
);
}
}
// 500
export class InternalServerErrorException extends HttpException {
constructor(
message?: string | object | any,
error = 'Internal Server Error',
) {
super(
HttpException.createBody(
message,
error,
HttpStatus.INTERNAL_SERVER_ERROR,
),
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
// 405
export class MethodNotAllowedException extends HttpException {
constructor(message?: string | object | any, error = 'Method Not Allowed') {
super(
HttpException.createBody(message, error, HttpStatus.METHOD_NOT_ALLOWED),
HttpStatus.METHOD_NOT_ALLOWED,
);
}
}
// 406
export class NotAcceptableException extends HttpException {
constructor(message?: string | object | any, error = 'Not Acceptable') {
super(
HttpException.createBody(message, error, HttpStatus.NOT_ACCEPTABLE),
HttpStatus.NOT_ACCEPTABLE,
);
}
}
// 404
export class NotFoundException extends HttpException {
constructor(message?: string | object | any, error = 'Not Found') {
super(
HttpException.createBody(message, error, HttpStatus.NOT_FOUND),
HttpStatus.NOT_FOUND,
);
}
}
// 501
export class NotImplementedException extends HttpException {
constructor(message?: string | object | any, error = 'Not Implemented') {
super(
HttpException.createBody(message, error, HttpStatus.NOT_IMPLEMENTED),
HttpStatus.NOT_IMPLEMENTED,
);
}
}
// 413
export class PayloadTooLargeException extends HttpException {
constructor(message?: string | object | any, error = 'Payload Too Large') {
super(
HttpException.createBody(message, error, HttpStatus.PAYLOAD_TOO_LARGE),
HttpStatus.PAYLOAD_TOO_LARGE,
);
}
}
// 408
export class RequestTimeoutException extends HttpException {
constructor(message?: string | object | any, error = 'Request Timeout') {
super(
HttpException.createBody(message, error, HttpStatus.REQUEST_TIMEOUT),
HttpStatus.REQUEST_TIMEOUT,
);
}
}
// 503
export class ServiceUnavailableException extends HttpException {
constructor(message?: string | object | any, error = 'Service Unavailable') {
super(
HttpException.createBody(message, error, HttpStatus.SERVICE_UNAVAILABLE),
HttpStatus.SERVICE_UNAVAILABLE,
);
}
}
// 401
export class UnauthorizedException extends HttpException {
constructor(message?: string | object | any, error = 'Unauthorized') {
super(
HttpException.createBody(message, error, HttpStatus.UNAUTHORIZED),
HttpStatus.UNAUTHORIZED,
);
}
}
// ...