errors.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. let Errors = {
  2. unknown: [
  3. 'An unknown error occured on our end. Please try again later',
  4. 500
  5. ],
  6. accountAlreadyCreated: [
  7. 'This account has already been created',
  8. 400
  9. ],
  10. categoryAlreadyExists: [
  11. 'This category has already been created',
  12. 400
  13. ],
  14. accountDoesNotExist: [
  15. 'This account does not exist',
  16. 400
  17. ],
  18. invalidCategory: [
  19. 'This category does not exist',
  20. 400
  21. ],
  22. invalidLoginCredentials: [
  23. 'The username or password provided was incorrect',
  24. 401
  25. ],
  26. requestNotAuthorized: [
  27. 'The request was not authorized',
  28. 401
  29. ],
  30. invalidToken: [
  31. 'The token provided was not valid',
  32. 401
  33. ],
  34. noSettings: [
  35. 'You haven\'t added any settings yet',
  36. 500
  37. ],
  38. passwordSame: [
  39. 'You can\'t set it to the same password',
  40. 400
  41. ],
  42. cannotLikeOwnPost: [
  43. 'You can\'t like your own post',
  44. 400
  45. ],
  46. threadLocked: [
  47. 'You can\'t post to a locked thread',
  48. 400
  49. ],
  50. postRemoved: [
  51. 'You can\'t reply to a removed post',
  52. 400
  53. ]
  54. }
  55. function processErrors(errorName) {
  56. let arr = Errors[errorName]
  57. temp = {}
  58. temp.name = errorName
  59. temp.message = arr[0]
  60. temp.status = arr[1]
  61. return {
  62. name: errorName,
  63. message: arr[0],
  64. status: arr[1]
  65. }
  66. }
  67. let ProcessedErrors = {}
  68. for(var errorName in Errors) {
  69. ProcessedErrors[errorName] = processErrors(errorName)
  70. }
  71. ProcessedErrors.VALIDATION_ERROR = 'VALIDATION_ERROR';
  72. ProcessedErrors.invalidParameter = function (param, message) {
  73. let punctuatedMessage = '';
  74. if(message) {
  75. punctuatedMessage = ': ' + message;
  76. }
  77. return {
  78. name: errorName,
  79. message: `${param} is invalid${punctuatedMessage}`,
  80. status: 400,
  81. parameter: param
  82. };
  83. }
  84. ProcessedErrors.sequelizeValidation = (sequelize, obj) => {
  85. return new sequelize.ValidationError(obj.error, [
  86. new sequelize.ValidationErrorItem(
  87. obj.error,
  88. 'Validation error',
  89. obj.path,
  90. obj.value
  91. )
  92. ])
  93. }
  94. module.exports = ProcessedErrors