diff --git a/app/api/documents/routes.js b/app/api/documents/routes.js
index 2589c46bd7..3904cc7536 100644
--- a/app/api/documents/routes.js
+++ b/app/api/documents/routes.js
@@ -36,6 +36,10 @@ export default (app) => {
}
documents.getById(id, req.language).then(response => {
+ if (!response) {
+ res.json({}, 404);
+ return;
+ }
res.json({rows: [response]});
})
.catch((error) => res.json({error}, 500));
diff --git a/app/api/entities/routes.js b/app/api/entities/routes.js
index 19828fe427..53fe88dc1d 100644
--- a/app/api/entities/routes.js
+++ b/app/api/entities/routes.js
@@ -41,7 +41,13 @@ export default (app) => {
app.get('/api/entities', (req, res) => {
entities.getById(req.query._id, req.language)
- .then(response => res.json({rows: [response]}))
+ .then((response) => {
+ if (!response) {
+ res.json({}, 404);
+ return;
+ }
+ res.json({rows: [response]});
+ })
.catch(res.error);
});
diff --git a/app/react/App/NoMatch.js b/app/react/App/NoMatch.js
index 4c894b0923..ad72009914 100644
--- a/app/react/App/NoMatch.js
+++ b/app/react/App/NoMatch.js
@@ -1,13 +1,14 @@
-import React, {Component} from 'react';
+import React from 'react';
+import RouteHandler from 'app/App/RouteHandler';
import Helmet from 'react-helmet';
-class NoMatch extends Component {
+class NoMatch extends RouteHandler {
render() {
return (
-
404
Page was not found
+ 404
);
}
diff --git a/app/react/Router.js b/app/react/Router.js
index e3b6426b71..f94e22d11b 100644
--- a/app/react/Router.js
+++ b/app/react/Router.js
@@ -136,6 +136,22 @@ function handleRoute(res, renderProps, req) {
thesauris: Immutable(globalResources.thesauris)
}), globalResources]);
})
+ .catch((error) => {
+ if (error.status === 401) {
+ res.redirect(302, '/login');
+ return Promise.reject(error);
+ }
+
+ if (error.status === 404) {
+ res.redirect(302, '/404');
+ return Promise.reject(error);
+ }
+
+ if (error.status === 500) {
+ handleError(res, error);
+ return Promise.reject(error);
+ }
+ })
.then(([initialData, globalResources]) => {
initialData.user = globalResources.user;
initialData.settings = globalResources.settings;
diff --git a/app/react/Routes.js b/app/react/Routes.js
index e196a3927d..1bf26e6b96 100644
--- a/app/react/Routes.js
+++ b/app/react/Routes.js
@@ -110,6 +110,7 @@ const routes =
+
;
diff --git a/app/react/utils/api.js b/app/react/utils/api.js
index d3549f7ac0..fe58fe7a96 100644
--- a/app/react/utils/api.js
+++ b/app/react/utils/api.js
@@ -1,4 +1,5 @@
import request from '../../shared/JSONRequest';
+import {isClient} from 'app/utils';
import {APIURL} from '../config.js';
import {browserHistory} from 'react-router';
import {notify} from 'app/Notifications/actions/notificationsActions';
@@ -8,17 +9,27 @@ let cookie;
let locale;
let handleError = (error) => {
+ if (!isClient) {
+ return Promise.reject(error);
+ }
+
if (error.status === 401) {
browserHistory.replace('/login');
+ return Promise.reject(error);
+ }
+
+ if (error.status === 404) {
+ browserHistory.replace('/404');
+ return Promise.reject(error);
}
if (error.status === 500) {
store.dispatch(notify('An error has occurred', 'danger'));
- return Promise.reject();
+ return Promise.reject(error);
}
store.dispatch(notify(error.json.error, 'danger'));
- return Promise.reject();
+ return Promise.reject(error);
};
export default {
diff --git a/app/react/utils/specs/api.spec.js b/app/react/utils/specs/api.spec.js
index d88e0267ed..bd29289d70 100644
--- a/app/react/utils/specs/api.spec.js
+++ b/app/react/utils/specs/api.spec.js
@@ -13,6 +13,7 @@ describe('Login', () => {
.post(APIURL + 'test_post', JSON.stringify({method: 'POST'}))
.delete(APIURL + 'test_delete?data=delete', JSON.stringify({method: 'DELETE'}))
.get(APIURL + 'unauthorised', {status: 401, body: {}})
+ .get(APIURL + 'notfound', {status: 404, body: {}})
.get(APIURL + 'error_url', {status: 500, body: {}});
});
@@ -78,6 +79,17 @@ describe('Login', () => {
});
});
+ describe('404', () => {
+ it('should redirect to login', (done) => {
+ spyOn(browserHistory, 'replace');
+ api.get('notfound')
+ .catch(() => {
+ expect(browserHistory.replace).toHaveBeenCalledWith('/404');
+ done();
+ });
+ });
+ });
+
it('should notify the user', (done) => {
spyOn(store, 'dispatch');
spyOn(notifyActions, 'notify').and.returnValue('notify action');