客户端可以通过 请求方法获取服务器性能,HTTP、脚本化HTTP 与 nestjs 异常处理

// 错误接口

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,

);

}

}

// ...

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值