Exception,所有异常的基类
- ErrorException 错误异常类
- InternalServerError,500
- GatewayTime-out,504
- BadGateway,502
- NotFoundException,404
- ForbiddenException,403
- Method Not Allowed,405
- BadRequestException,400
- UnauthorizedException,401
- RequestTimeOutException,408
- SuccessException,200
- UpdateException, 204
Java Exception异常类参考 https://docs.spring.io/spring-security/site/docs/current/api/allclasses.html
PHP Exception异常参考 https://www.php.net/manual/zh/language.exceptions.extending.php
http状态码参考 https://www.runoob.com/http/http-status-codes.html
定义好每种异常的 class类,规范好错误信息;需要的时候,直接 new
exception/index.js
const {
HttpException,
SuccessException,
AuthFailedException,
ForbiddenException,
NotFoundException
} = require('./http-exception')
const {
ParameterException,
LikeException,
UnlikeException
} = require('./exception')
module.exports = {
HttpException,
ParameterException,
SuccessException,
AuthFailedException,
ForbiddenException,
NotFoundException,
LikeException,
UnlikeException
}
Exception.js
把所有的 已知错误封装成 HttpException,只是一个基类,还可以继续定义子类
HttpException extends Error 是为了 throw error 抛出错误
HttpException 默认 500 serverError
const {
HttpException
} = require('./http-exception')
const {
parameter,
likeError,
unlikeError
} = require('./code-exception')
class ParameterException extends HttpException {
constructor(message, code) {
super()
this.status = parameter.status
this.code = code || parameter.code
this.message = message || parameter.message
}
}
// 点赞
class LikeException extends HttpException {
constructor(message, code) {
super()
this.status = likeError.status
this.code = code || likeError.code
this.message = message || likeError.message
}
}
// 取消点赞
class UnlikeException extends HttpException {
constructor(message, code) {
super()
this.status = unlikeError.status
this.code = code || unlikeError.code
this.message = message || unlikeError.message
}
}
module.exports = {
ParameterException,
LikeException,
UnlikeException
}
HttpException
把所有的 已知错误封装成 HttpException,只是一个基类,还可以继续定义子类
HttpException extends Error 是为了 throw error 抛出错误
HttpException 默认 500 serverError
const {
success,
notFound,
forbidden,
authFailed
} = require('./code-exception')
const error = {
code: 1005,
message: '服务器在忙',
status: 500
}
// 500 默认
class HttpException extends Error {
constructor(code= error.code, message=error.message, status=error.status) {
super()
this.status = status
this.code = code
this.message = message
}
}
// 200 抛错方式返回成功
class SuccessException extends HttpException {
constructor(message, code) {
super()
this.status = success.status
this.code = code || success.code
this.message = message || success.message
}
}
// 404
class NotFoundException extends HttpException {
constructor(message, code) {
super();
this.status = notFound.status
this.code = code || notFound.code
this.message = message || notFound.message
}
}
// 401
class AuthFailedException extends HttpException {
constructor(message, code) {
super()
this.status = authFailed.status
this.code = code || authFailed.code
this.message = message || authFailed.message
}
}
// 403
class ForbiddenException extends HttpException {
constructor(message, code) {
super()
this.status = forbidden.status
this.code = code || forbidden.code
this.message = message || forbidden.message
}
}
module.exports = {
HttpException,
SuccessException,
AuthFailedException,
ForbiddenException,
NotFoundException
}