LinkedFilesErrors.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. UrlFetchFailedError = (message) ->
  2. error = new Error(message)
  3. error.name = 'UrlFetchFailedError'
  4. error.__proto__ = UrlFetchFailedError.prototype
  5. return error
  6. UrlFetchFailedError.prototype.__proto__ = Error.prototype
  7. InvalidUrlError = (message) ->
  8. error = new Error(message)
  9. error.name = 'InvalidUrlError'
  10. error.__proto__ = InvalidUrlError.prototype
  11. return error
  12. InvalidUrlError.prototype.__proto__ = Error.prototype
  13. OutputFileFetchFailedError = (message) ->
  14. error = new Error(message)
  15. error.name = 'OutputFileFetchFailedError'
  16. error.__proto__ = OutputFileFetchFailedError.prototype
  17. return error
  18. OutputFileFetchFailedError.prototype.__proto__ = Error.prototype
  19. AccessDeniedError = (message) ->
  20. error = new Error(message)
  21. error.name = 'AccessDenied'
  22. error.__proto__ = AccessDeniedError.prototype
  23. return error
  24. AccessDeniedError.prototype.__proto__ = Error.prototype
  25. BadEntityTypeError = (message) ->
  26. error = new Error(message)
  27. error.name = 'BadEntityType'
  28. error.__proto__ = BadEntityTypeError.prototype
  29. return error
  30. BadEntityTypeError.prototype.__proto__ = Error.prototype
  31. BadDataError = (message) ->
  32. error = new Error(message)
  33. error.name = 'BadData'
  34. error.__proto__ = BadDataError.prototype
  35. return error
  36. BadDataError.prototype.__proto__ = Error.prototype
  37. ProjectNotFoundError = (message) ->
  38. error = new Error(message)
  39. error.name = 'ProjectNotFound'
  40. error.__proto__ = ProjectNotFoundError.prototype
  41. return error
  42. ProjectNotFoundError.prototype.__proto__ = Error.prototype
  43. V1ProjectNotFoundError = (message) ->
  44. error = new Error(message)
  45. error.name = 'V1ProjectNotFound'
  46. error.__proto__ = V1ProjectNotFoundError.prototype
  47. return error
  48. V1ProjectNotFoundError.prototype.__proto__ = Error.prototype
  49. SourceFileNotFoundError = (message) ->
  50. error = new Error(message)
  51. error.name = 'SourceFileNotFound'
  52. error.__proto__ = SourceFileNotFoundError.prototype
  53. return error
  54. SourceFileNotFoundError.prototype.__proto__ = Error.prototype
  55. module.exports = {
  56. UrlFetchFailedError,
  57. InvalidUrlError,
  58. OutputFileFetchFailedError,
  59. AccessDeniedError,
  60. BadEntityTypeError,
  61. BadDataError,
  62. ProjectNotFoundError,
  63. V1ProjectNotFoundError,
  64. SourceFileNotFoundError,
  65. handleError: (error, req, res, next) ->
  66. if error instanceof BadDataError
  67. res.status(400).send("The submitted data is not valid")
  68. else if error instanceof AccessDeniedError
  69. res.status(403).send("You do not have access to this project")
  70. else if error instanceof BadDataError
  71. res.status(400).send("The submitted data is not valid")
  72. else if error instanceof BadEntityTypeError
  73. res.status(400).send("The file is the wrong type")
  74. else if error instanceof SourceFileNotFoundError
  75. res.status(404).send("Source file not found")
  76. else if error instanceof ProjectNotFoundError
  77. res.status(404).send("Project not found")
  78. else if error instanceof V1ProjectNotFoundError
  79. res.status(409).send("Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file")
  80. else if error instanceof OutputFileFetchFailedError
  81. res.status(404).send("Could not get output file")
  82. else if error instanceof UrlFetchFailedError
  83. res.status(422).send(
  84. "Your URL could not be reached (#{error.statusCode} status code). Please check it and try again."
  85. )
  86. else if error instanceof InvalidUrlError
  87. res.status(422).send(
  88. "Your URL is not valid. Please check it and try again."
  89. )
  90. else
  91. next(error)
  92. }