Handle JSONified errors while registering

Closes #617
This commit is contained in:
Lee Starnes 2019-08-06 18:03:31 +00:00 committed by HJ
parent 0e6489d840
commit 4fc27414d2
3 changed files with 40 additions and 15 deletions

View file

@ -3,7 +3,6 @@ import oauthApi from '../services/new_api/oauth.js'
import { compact, map, each, merge, last, concat, uniq } from 'lodash' import { compact, map, each, merge, last, concat, uniq } from 'lodash'
import { set } from 'vue' import { set } from 'vue'
import { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js' import { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js'
import { humanizeErrors } from './errors'
// TODO: Unify with mergeOrAdd in statuses.js // TODO: Unify with mergeOrAdd in statuses.js
export const mergeOrAdd = (arr, obj, item) => { export const mergeOrAdd = (arr, obj, item) => {
@ -382,16 +381,8 @@ const users = {
store.dispatch('loginUser', data.access_token) store.dispatch('loginUser', data.access_token)
} catch (e) { } catch (e) {
let errors = e.message let errors = e.message
// replace ap_id with username
if (typeof errors === 'object') {
if (errors.ap_id) {
errors.username = errors.ap_id
delete errors.ap_id
}
errors = humanizeErrors(errors)
}
store.commit('signUpFailure', errors) store.commit('signUpFailure', errors)
throw Error(errors) throw e
} }
}, },
async getCaptcha (store) { async getCaptcha (store) {

View file

@ -1,7 +1,7 @@
import { each, map, concat, last } from 'lodash' import { each, map, concat, last } from 'lodash'
import { parseStatus, parseUser, parseNotification, parseAttachment } from '../entity_normalizer/entity_normalizer.service.js' import { parseStatus, parseUser, parseNotification, parseAttachment } from '../entity_normalizer/entity_normalizer.service.js'
import 'whatwg-fetch' import 'whatwg-fetch'
import { StatusCodeError } from '../errors/errors' import { RegistrationError, StatusCodeError } from '../errors/errors'
/* eslint-env browser */ /* eslint-env browser */
const EXTERNAL_PROFILE_URL = '/api/externalprofile/show.json' const EXTERNAL_PROFILE_URL = '/api/externalprofile/show.json'
@ -199,12 +199,11 @@ const register = ({ params, credentials }) => {
...rest ...rest
}) })
}) })
.then((response) => [response.ok, response]) .then((response) => {
.then(([ok, response]) => { if (response.ok) {
if (ok) {
return response.json() return response.json()
} else { } else {
return response.json().then((error) => { throw new Error(error) }) return response.json().then((error) => { throw new RegistrationError(error) })
} }
}) })
} }

View file

@ -1,3 +1,5 @@
import { humanizeErrors } from '../../modules/errors'
export function StatusCodeError (statusCode, body, options, response) { export function StatusCodeError (statusCode, body, options, response) {
this.name = 'StatusCodeError' this.name = 'StatusCodeError'
this.statusCode = statusCode this.statusCode = statusCode
@ -12,3 +14,36 @@ export function StatusCodeError (statusCode, body, options, response) {
} }
StatusCodeError.prototype = Object.create(Error.prototype) StatusCodeError.prototype = Object.create(Error.prototype)
StatusCodeError.prototype.constructor = StatusCodeError 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') {
// replace ap_id with username
if (error.ap_id) {
error.username = error.ap_id
delete error.ap_id
}
this.message = humanizeErrors(error)
} else {
this.message = error
}
} catch (e) {
// can't parse it, so just treat it like a string
this.message = error
}
}
}