Errors.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. const OError = require('@overleaf/o-error')
  2. const settings = require('settings-sharelatex')
  3. // Error class for legacy errors so they inherit OError while staying
  4. // backward-compatible (can be instantiated with string as argument instead
  5. // of object)
  6. class BackwardCompatibleError extends OError {
  7. constructor(messageOrOptions) {
  8. let options
  9. if (typeof messageOrOptions === 'string') {
  10. options = { message: messageOrOptions }
  11. } else if (!messageOrOptions) {
  12. options = {}
  13. } else {
  14. options = messageOrOptions
  15. }
  16. super(options)
  17. }
  18. }
  19. class NotFoundError extends BackwardCompatibleError {}
  20. class ForbiddenError extends BackwardCompatibleError {}
  21. class ServiceNotConfiguredError extends BackwardCompatibleError {}
  22. class TooManyRequestsError extends BackwardCompatibleError {}
  23. class InvalidNameError extends BackwardCompatibleError {}
  24. class UnsupportedFileTypeError extends BackwardCompatibleError {}
  25. class FileTooLargeError extends BackwardCompatibleError {}
  26. class UnsupportedExportRecordsError extends BackwardCompatibleError {}
  27. class V1HistoryNotSyncedError extends BackwardCompatibleError {}
  28. class ProjectHistoryDisabledError extends BackwardCompatibleError {}
  29. class V1ConnectionError extends BackwardCompatibleError {}
  30. class UnconfirmedEmailError extends BackwardCompatibleError {}
  31. class EmailExistsError extends OError {
  32. constructor(options) {
  33. super({
  34. message: 'Email already exists',
  35. ...options
  36. })
  37. }
  38. }
  39. class InvalidError extends BackwardCompatibleError {}
  40. class NotInV2Error extends BackwardCompatibleError {}
  41. class SLInV2Error extends BackwardCompatibleError {}
  42. class SAMLIdentityExistsError extends BackwardCompatibleError {
  43. constructor(arg) {
  44. super(arg)
  45. if (!this.message) {
  46. this.message =
  47. 'provider and external id already linked to another account'
  48. }
  49. }
  50. }
  51. class SAMLSessionDataMissing extends BackwardCompatibleError {
  52. constructor(arg) {
  53. super(arg)
  54. const samlSession =
  55. typeof arg === 'object' && arg !== null && arg.samlSession
  56. ? arg.samlSession
  57. : {}
  58. this.tryAgain = true
  59. const {
  60. universityId,
  61. universityName,
  62. externalUserId,
  63. institutionEmail
  64. } = samlSession
  65. if (
  66. !universityId &&
  67. !universityName &&
  68. !externalUserId &&
  69. !institutionEmail
  70. ) {
  71. this.message = 'Missing session data.'
  72. } else if (
  73. !institutionEmail &&
  74. samlSession &&
  75. samlSession.userEmailAttributeUnreliable
  76. ) {
  77. this.tryAgain = false
  78. this.message = `Your account settings at your institution prevent us from accessing your email address. You will need to make your email address public at your institution in order to link with ${
  79. settings.appName
  80. }. Please contact your IT department if you have any questions.`
  81. } else if (!institutionEmail) {
  82. this.message = 'Unable to confirm your institution email.'
  83. }
  84. }
  85. }
  86. class ThirdPartyIdentityExistsError extends BackwardCompatibleError {
  87. constructor(arg) {
  88. super(arg)
  89. if (!this.message) {
  90. this.message =
  91. 'provider and external id already linked to another account'
  92. }
  93. }
  94. }
  95. class ThirdPartyUserNotFoundError extends BackwardCompatibleError {
  96. constructor(arg) {
  97. super(arg)
  98. if (!this.message) {
  99. this.message = 'user not found for provider and external id'
  100. }
  101. }
  102. }
  103. class SubscriptionAdminDeletionError extends OError {
  104. constructor(options) {
  105. super({
  106. message: 'subscription admins cannot be deleted',
  107. ...options
  108. })
  109. }
  110. }
  111. class ProjectNotFoundError extends OError {
  112. constructor(options) {
  113. super({
  114. message: 'project not found',
  115. ...options
  116. })
  117. }
  118. }
  119. class UserNotFoundError extends OError {
  120. constructor(options) {
  121. super({
  122. message: 'user not found',
  123. ...options
  124. })
  125. }
  126. }
  127. class UserNotCollaboratorError extends OError {
  128. constructor(options) {
  129. super({
  130. message: 'user not a collaborator',
  131. ...options
  132. })
  133. }
  134. }
  135. class DocHasRangesError extends OError {
  136. constructor(options) {
  137. super({ message: 'document has ranges', ...options })
  138. }
  139. }
  140. class InvalidQueryError extends OError {
  141. constructor(options) {
  142. super({ message: 'invalid search query', ...options })
  143. }
  144. }
  145. module.exports = {
  146. OError,
  147. BackwardCompatibleError,
  148. NotFoundError,
  149. ForbiddenError,
  150. ServiceNotConfiguredError,
  151. TooManyRequestsError,
  152. InvalidNameError,
  153. UnsupportedFileTypeError,
  154. FileTooLargeError,
  155. UnsupportedExportRecordsError,
  156. V1HistoryNotSyncedError,
  157. ProjectHistoryDisabledError,
  158. V1ConnectionError,
  159. UnconfirmedEmailError,
  160. EmailExistsError,
  161. InvalidError,
  162. NotInV2Error,
  163. SAMLIdentityExistsError,
  164. SAMLSessionDataMissing,
  165. SLInV2Error,
  166. ThirdPartyIdentityExistsError,
  167. ThirdPartyUserNotFoundError,
  168. SubscriptionAdminDeletionError,
  169. ProjectNotFoundError,
  170. UserNotFoundError,
  171. UserNotCollaboratorError,
  172. DocHasRangesError,
  173. InvalidQueryError
  174. }