1
0
mirror of https://github.com/flarum/core.git synced 2025-10-10 14:34:30 +02:00

Improve client XHR error handling

The default XHR error handler produce an alert which is appropriate to the response status code. It can be overridden per-request (by specifying the `errorHandler` option) so that the alert can be suppressed or displayed in a different position (e.g. inside a modal).

ref #118
This commit is contained in:
Toby Zerner
2015-10-20 12:48:26 +10:30
parent 7490709af8
commit 26a821e3e2
26 changed files with 192 additions and 175 deletions

View File

@@ -109,27 +109,28 @@ export default class Modal extends Component {
}
/**
* Show an alert describing errors returned from the API, and give focus to
* Stop loading.
*/
loaded() {
this.loading = false;
m.redraw();
}
/**
* Show an alert describing an error returned from the API, and give focus to
* the first relevant field.
*
* @param {Object} response
* @param {RequestError} error
*/
handleErrors(response) {
const errors = response && response.errors;
if (errors) {
this.alert = new Alert({
type: 'error',
children: errors.map((error, k) => [error.detail, k < errors.length - 1 ? m('br') : ''])
});
}
onerror(error) {
this.alert = error.alert;
m.redraw();
if (errors) {
this.$('form [name=' + errors[0].source.pointer.replace('/data/attributes/', '') + ']').select();
if (error.status === 422 && error.response.errors) {
this.$('form [name=' + error.response.errors[0].source.pointer.replace('/data/attributes/', '') + ']').select();
} else {
this.$('form :input:first').select();
this.onready();
}
}
}

View File

@@ -5,10 +5,6 @@ export default class RequestErrorModal extends Modal {
return 'RequestErrorModal Modal--large';
}
title() {
return this.props.error.message;
}
content() {
let responseText;