logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

errors.js (1843B)


  1. import { humanizeErrors } from '../../modules/errors'
  2. export function StatusCodeError (statusCode, body, options, response) {
  3. this.name = 'StatusCodeError'
  4. this.statusCode = statusCode
  5. this.message = statusCode + ' - ' + (JSON && JSON.stringify ? JSON.stringify(body) : body)
  6. this.error = body // legacy attribute
  7. this.options = options
  8. this.response = response
  9. if (Error.captureStackTrace) { // required for non-V8 environments
  10. Error.captureStackTrace(this)
  11. }
  12. }
  13. StatusCodeError.prototype = Object.create(Error.prototype)
  14. StatusCodeError.prototype.constructor = StatusCodeError
  15. export class RegistrationError extends Error {
  16. constructor (error) {
  17. super()
  18. if (Error.captureStackTrace) {
  19. Error.captureStackTrace(this)
  20. }
  21. try {
  22. // the error is probably a JSON object with a single key, "errors", whose value is another JSON object containing the real errors
  23. if (typeof error === 'string') {
  24. error = JSON.parse(error)
  25. // eslint-disable-next-line
  26. if (error.hasOwnProperty('error')) {
  27. error = JSON.parse(error.error)
  28. }
  29. }
  30. if (typeof error === 'object') {
  31. const errorContents = JSON.parse(error.error)
  32. // keys will have the property that has the error, for example 'ap_id',
  33. // 'email' or 'captcha', the value will be an array of its error
  34. // like "ap_id": ["has been taken"] or "captcha": ["Invalid CAPTCHA"]
  35. // replace ap_id with username
  36. if (errorContents.ap_id) {
  37. errorContents.username = errorContents.ap_id
  38. delete errorContents.ap_id
  39. }
  40. this.message = humanizeErrors(errorContents)
  41. } else {
  42. this.message = error
  43. }
  44. } catch (e) {
  45. // can't parse it, so just treat it like a string
  46. this.message = error
  47. }
  48. }
  49. }