mirror of
https://github.com/flarum/core.git
synced 2025-07-24 18:21:33 +02:00
Better handling of API server errors
This commit is contained in:
@@ -47,7 +47,10 @@ export default class DiscussionList extends Component {
|
|||||||
this.loading(true);
|
this.loading(true);
|
||||||
this.discussions([]);
|
this.discussions([]);
|
||||||
m.endComputation();
|
m.endComputation();
|
||||||
this.loadResults().then(this.parseResults.bind(this));
|
this.loadResults().then(this.parseResults.bind(this), response => {
|
||||||
|
this.loading(false);
|
||||||
|
m.redraw();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onunload() {
|
onunload() {
|
||||||
|
@@ -36,7 +36,7 @@ export default class Model {
|
|||||||
|
|
||||||
this.pushData(data);
|
this.pushData(data);
|
||||||
|
|
||||||
return m.request({
|
return app.request({
|
||||||
method: this.exists ? 'PUT' : 'POST',
|
method: this.exists ? 'PUT' : 'POST',
|
||||||
url: app.config['api_url']+'/'+this.data().type+(this.exists ? '/'+this.data().id : ''),
|
url: app.config['api_url']+'/'+this.data().type+(this.exists ? '/'+this.data().id : ''),
|
||||||
data: {data},
|
data: {data},
|
||||||
@@ -51,7 +51,7 @@ export default class Model {
|
|||||||
delete() {
|
delete() {
|
||||||
if (!this.exists) { return; }
|
if (!this.exists) { return; }
|
||||||
|
|
||||||
return m.request({
|
return app.request({
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
url: app.config['api_url']+'/'+this.data().type+'/'+this.data().id,
|
url: app.config['api_url']+'/'+this.data().type+'/'+this.data().id,
|
||||||
background: true,
|
background: true,
|
||||||
|
@@ -36,7 +36,7 @@ export default class Store {
|
|||||||
endpoint += '/'+id
|
endpoint += '/'+id
|
||||||
params = query
|
params = query
|
||||||
}
|
}
|
||||||
return m.request({
|
return app.request({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: app.config['api_url']+'/'+endpoint,
|
url: app.config['api_url']+'/'+endpoint,
|
||||||
data: params,
|
data: params,
|
||||||
|
@@ -1,10 +1,12 @@
|
|||||||
import ItemList from 'flarum/utils/item-list';
|
import ItemList from 'flarum/utils/item-list';
|
||||||
import Alert from 'flarum/components/alert';
|
import Alert from 'flarum/components/alert';
|
||||||
|
import ServerError from 'flarum/utils/server-error';
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.initializers = new ItemList();
|
this.initializers = new ItemList();
|
||||||
this.cache = {};
|
this.cache = {};
|
||||||
|
this.serverError = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
boot() {
|
boot() {
|
||||||
@@ -15,6 +17,26 @@ class App {
|
|||||||
document.title = (title ? title+' - ' : '')+this.config['forum_title'];
|
document.title = (title ? title+' - ' : '')+this.config['forum_title'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
request(options) {
|
||||||
|
options.extract = options.extract || function(xhr, xhrOptions) {
|
||||||
|
if (xhr.status === 500) {
|
||||||
|
throw new ServerError;
|
||||||
|
}
|
||||||
|
return xhr.responseText;
|
||||||
|
};
|
||||||
|
|
||||||
|
return m.request(options).then(response => {
|
||||||
|
this.alerts.dismiss(this.serverError);
|
||||||
|
return response;
|
||||||
|
}, response => {
|
||||||
|
this.alerts.dismiss(this.serverError);
|
||||||
|
if (response instanceof ServerError) {
|
||||||
|
this.alerts.show(this.serverError = new Alert({ type: 'warning', message: 'Oops! Something went wrong on the server. Please try again.' }))
|
||||||
|
}
|
||||||
|
throw response;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
handleApiErrors(response) {
|
handleApiErrors(response) {
|
||||||
this.alerts.clear();
|
this.alerts.clear();
|
||||||
|
|
||||||
|
2
framework/core/js/lib/utils/server-error.js
Normal file
2
framework/core/js/lib/utils/server-error.js
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export default class ServerError {
|
||||||
|
}
|
Reference in New Issue
Block a user