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.statusthis.code = code || parameter.codethis.message = message || parameter.message}}// 点赞class LikeException extends HttpException {constructor(message, code) {super()this.status = likeError.statusthis.code = code || likeError.codethis.message = message || likeError.message}}// 取消点赞class UnlikeException extends HttpException {constructor(message, code) {super()this.status = unlikeError.statusthis.code = code || unlikeError.codethis.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 = statusthis.code = codethis.message = message}}// 200 抛错方式返回成功class SuccessException extends HttpException {constructor(message, code) {super()this.status = success.statusthis.code = code || success.codethis.message = message || success.message}}// 404class NotFoundException extends HttpException {constructor(message, code) {super();this.status = notFound.statusthis.code = code || notFound.codethis.message = message || notFound.message}}// 401class AuthFailedException extends HttpException {constructor(message, code) {super()this.status = authFailed.statusthis.code = code || authFailed.codethis.message = message || authFailed.message}}// 403class ForbiddenException extends HttpException {constructor(message, code) {super()this.status = forbidden.statusthis.code = code || forbidden.codethis.message = message || forbidden.message}}module.exports = {HttpException,SuccessException,AuthFailedException,ForbiddenException,NotFoundException}
