Exception,所有异常的基类

  1. ErrorException 错误异常类
    1. InternalServerError,500
    2. GatewayTime-out,504
    3. BadGateway,502
  2. NotFoundException,404
  3. ForbiddenException,403
  4. Method Not Allowed,405
  5. BadRequestException,400
  6. UnauthorizedException,401
  7. RequestTimeOutException,408
  8. SuccessException,200
  9. 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

  1. const {
  2. HttpException,
  3. SuccessException,
  4. AuthFailedException,
  5. ForbiddenException,
  6. NotFoundException
  7. } = require('./http-exception')
  8. const {
  9. ParameterException,
  10. LikeException,
  11. UnlikeException
  12. } = require('./exception')
  13. module.exports = {
  14. HttpException,
  15. ParameterException,
  16. SuccessException,
  17. AuthFailedException,
  18. ForbiddenException,
  19. NotFoundException,
  20. LikeException,
  21. UnlikeException
  22. }

Exception.js

把所有的 已知错误封装成 HttpException,只是一个基类,还可以继续定义子类
HttpException extends Error 是为了 throw error 抛出错误
HttpException 默认 500 serverError

  1. const {
  2. HttpException
  3. } = require('./http-exception')
  4. const {
  5. parameter,
  6. likeError,
  7. unlikeError
  8. } = require('./code-exception')
  9. class ParameterException extends HttpException {
  10. constructor(message, code) {
  11. super()
  12. this.status = parameter.status
  13. this.code = code || parameter.code
  14. this.message = message || parameter.message
  15. }
  16. }
  17. // 点赞
  18. class LikeException extends HttpException {
  19. constructor(message, code) {
  20. super()
  21. this.status = likeError.status
  22. this.code = code || likeError.code
  23. this.message = message || likeError.message
  24. }
  25. }
  26. // 取消点赞
  27. class UnlikeException extends HttpException {
  28. constructor(message, code) {
  29. super()
  30. this.status = unlikeError.status
  31. this.code = code || unlikeError.code
  32. this.message = message || unlikeError.message
  33. }
  34. }
  35. module.exports = {
  36. ParameterException,
  37. LikeException,
  38. UnlikeException
  39. }

HttpException

把所有的 已知错误封装成 HttpException,只是一个基类,还可以继续定义子类
HttpException extends Error 是为了 throw error 抛出错误
HttpException 默认 500 serverError

  1. const {
  2. success,
  3. notFound,
  4. forbidden,
  5. authFailed
  6. } = require('./code-exception')
  7. const error = {
  8. code: 1005,
  9. message: '服务器在忙',
  10. status: 500
  11. }
  12. // 500 默认
  13. class HttpException extends Error {
  14. constructor(code= error.code, message=error.message, status=error.status) {
  15. super()
  16. this.status = status
  17. this.code = code
  18. this.message = message
  19. }
  20. }
  21. // 200 抛错方式返回成功
  22. class SuccessException extends HttpException {
  23. constructor(message, code) {
  24. super()
  25. this.status = success.status
  26. this.code = code || success.code
  27. this.message = message || success.message
  28. }
  29. }
  30. // 404
  31. class NotFoundException extends HttpException {
  32. constructor(message, code) {
  33. super();
  34. this.status = notFound.status
  35. this.code = code || notFound.code
  36. this.message = message || notFound.message
  37. }
  38. }
  39. // 401
  40. class AuthFailedException extends HttpException {
  41. constructor(message, code) {
  42. super()
  43. this.status = authFailed.status
  44. this.code = code || authFailed.code
  45. this.message = message || authFailed.message
  46. }
  47. }
  48. // 403
  49. class ForbiddenException extends HttpException {
  50. constructor(message, code) {
  51. super()
  52. this.status = forbidden.status
  53. this.code = code || forbidden.code
  54. this.message = message || forbidden.message
  55. }
  56. }
  57. module.exports = {
  58. HttpException,
  59. SuccessException,
  60. AuthFailedException,
  61. ForbiddenException,
  62. NotFoundException
  63. }