error.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * 全局错误码
  3. */
  4. const ERROR = {
  5. 50403: 50403,
  6. // 参数错误
  7. 51001: 51001,
  8. 51002: 51002,
  9. 51003: 51003,
  10. 51004: 51004,
  11. 51005: 51005,
  12. 51006: 51006,
  13. 51007: 51007,
  14. 51008: 51008,
  15. 51009: 51009,
  16. 51010: 51010,
  17. 51011: 51011,
  18. 51012: 51012,
  19. 51013: 51013,
  20. // 数据不存在
  21. 52001: 52001,
  22. 52002: 52002,
  23. // 运行错误
  24. 53001: 53001,
  25. 53002: 53002,
  26. 53003: 53003,
  27. 53004: 53004,
  28. 53005: 53005,
  29. 54001: 54001,
  30. 54002: 54002,
  31. }
  32. const errSubject = "uni-pay";
  33. function isUniPayError(errCode) {
  34. return Object.values(ERROR).includes(errCode);
  35. }
  36. class UniCloudError extends Error {
  37. constructor(options = {}) {
  38. super(options.message);
  39. this.errMsg = options.message || '';
  40. this.code = this.errCode = options.code;
  41. this.errSubject = options.subject || errSubject;
  42. this.forceReturn = options.forceReturn || false;
  43. this.cause = options.cause;
  44. }
  45. toJson(level = 0) {
  46. if (level >= 10) {
  47. return
  48. }
  49. level++
  50. return {
  51. errCode: this.errCode,
  52. errMsg: this.errMsg,
  53. code: this.errCode,
  54. message: this.message,
  55. errSubject: this.errSubject,
  56. cause: this.cause && this.cause.toJson ? this.cause.toJson(level) : this.cause
  57. }
  58. }
  59. }
  60. module.exports = {
  61. ERROR,
  62. isUniPayError,
  63. UniCloudError
  64. }