feat: Improved error filtering and reporting (#1293)
This commit is contained in:
@@ -4,6 +4,14 @@ import { map, trim } from 'lodash';
|
||||
import invariant from 'invariant';
|
||||
import stores from 'stores';
|
||||
import download from './download';
|
||||
import {
|
||||
AuthorizationError,
|
||||
NetworkError,
|
||||
NotFoundError,
|
||||
OfflineError,
|
||||
RequestError,
|
||||
UpdateRequiredError,
|
||||
} from './errors';
|
||||
|
||||
type Options = {
|
||||
baseUrl?: string,
|
||||
@@ -62,9 +70,9 @@ class ApiClient {
|
||||
});
|
||||
} catch (err) {
|
||||
if (window.navigator.onLine) {
|
||||
throw new Error('A network error occurred, try again?');
|
||||
throw new NetworkError('A network error occurred, try again?');
|
||||
} else {
|
||||
throw new Error('No internet connection available');
|
||||
throw new OfflineError('No internet connection available');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,9 +112,18 @@ class ApiClient {
|
||||
|
||||
if (response.status === 400 && error.error === 'editor_update_required') {
|
||||
window.location.reload(true);
|
||||
throw new UpdateRequiredError(error.message);
|
||||
}
|
||||
|
||||
throw new Error(error.message);
|
||||
if (response.status === 403) {
|
||||
throw new AuthorizationError(error.message);
|
||||
}
|
||||
|
||||
if (response.status === 404) {
|
||||
throw new NotFoundError(error.message);
|
||||
}
|
||||
|
||||
throw new RequestError(error.message);
|
||||
};
|
||||
|
||||
get = (path: string, data: ?Object, options?: Object) => {
|
||||
|
||||
9
app/utils/errors.js
Normal file
9
app/utils/errors.js
Normal file
@@ -0,0 +1,9 @@
|
||||
// @flow
|
||||
import ExtendableError from 'es6-error';
|
||||
|
||||
export class AuthorizationError extends ExtendableError {}
|
||||
export class NetworkError extends ExtendableError {}
|
||||
export class NotFoundError extends ExtendableError {}
|
||||
export class OfflineError extends ExtendableError {}
|
||||
export class RequestError extends ExtendableError {}
|
||||
export class UpdateRequiredError extends ExtendableError {}
|
||||
Reference in New Issue
Block a user