diff options
| author | Shpuld Shpludson <shp@cock.li> | 2020-01-15 16:35:13 +0000 |
|---|---|---|
| committer | Shpuld Shpludson <shp@cock.li> | 2020-01-15 16:35:13 +0000 |
| commit | 3ab128e73924ce34d190ff609cb9b081cdffe402 (patch) | |
| tree | bba013a7d61688b90c1f59a8f9fa6c3323b72a05 /src/services/errors/errors.js | |
| parent | 7c26435e66fd7e142ea4b88fbe51eede32eeb5ce (diff) | |
| parent | 7397636914a9d3e7fd30373034c25175273ab808 (diff) | |
Merge branch 'develop' into 'master'
`master` refresh with `develop`
See merge request pleroma/pleroma-fe!1028
Diffstat (limited to 'src/services/errors/errors.js')
| -rw-r--r-- | src/services/errors/errors.js | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/services/errors/errors.js b/src/services/errors/errors.js index 548f3c68..d4cf9132 100644 --- a/src/services/errors/errors.js +++ b/src/services/errors/errors.js @@ -1,3 +1,5 @@ +import { humanizeErrors } from '../../modules/errors' + export function StatusCodeError (statusCode, body, options, response) { this.name = 'StatusCodeError' this.statusCode = statusCode @@ -12,3 +14,42 @@ export function StatusCodeError (statusCode, body, options, response) { } StatusCodeError.prototype = Object.create(Error.prototype) StatusCodeError.prototype.constructor = StatusCodeError + +export class RegistrationError extends Error { + constructor (error) { + super() + if (Error.captureStackTrace) { + Error.captureStackTrace(this) + } + + try { + // the error is probably a JSON object with a single key, "errors", whose value is another JSON object containing the real errors + if (typeof error === 'string') { + error = JSON.parse(error) + if (error.hasOwnProperty('error')) { + error = JSON.parse(error.error) + } + } + + if (typeof error === 'object') { + const errorContents = JSON.parse(error.error) + // keys will have the property that has the error, for example 'ap_id', + // 'email' or 'captcha', the value will be an array of its error + // like "ap_id": ["has been taken"] or "captcha": ["Invalid CAPTCHA"] + + // replace ap_id with username + if (errorContents.ap_id) { + errorContents.username = errorContents.ap_id + delete errorContents.ap_id + } + + this.message = humanizeErrors(errorContents) + } else { + this.message = error + } + } catch (e) { + // can't parse it, so just treat it like a string + this.message = error + } + } +} |
