error.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // 数据不存在
  19. 52001: 52001,
  20. 52002: 52002,
  21. // 运行错误
  22. 53001: 53001,
  23. 53002: 53002,
  24. 53003: 53003,
  25. 53004: 53004,
  26. 53005: 53005,
  27. }
  28. const errSubject = "uni-pay";
  29. function isUniPayError(errCode) {
  30. return Object.values(ERROR).includes(errCode);
  31. }
  32. class UniCloudError extends Error {
  33. constructor(options = {}) {
  34. super(options.message);
  35. this.errMsg = options.message || '';
  36. this.code = this.errCode = options.code;
  37. this.errSubject = options.subject || errSubject;
  38. this.forceReturn = options.forceReturn || false;
  39. this.cause = options.cause;
  40. }
  41. toJson(level = 0) {
  42. if (level >= 10) {
  43. return
  44. }
  45. level++
  46. return {
  47. errCode: this.errCode,
  48. errMsg: this.errMsg,
  49. code: this.errCode,
  50. message: this.message,
  51. errSubject: this.errSubject,
  52. cause: this.cause && this.cause.toJson ? this.cause.toJson(level) : this.cause
  53. }
  54. }
  55. }
  56. module.exports = {
  57. ERROR,
  58. isUniPayError,
  59. UniCloudError
  60. }