Skip to content

Commit

Permalink
Merge pull request #1048 from huridocs/24-redirect-to-404
Browse files Browse the repository at this point in the history
404 redirect for missing entities or documents
  • Loading branch information
daneryl authored Jun 7, 2017
2 parents 849e226 + ae44bce commit d6416a5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/api/documents/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 7 additions & 1 deletion app/api/entities/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
7 changes: 4 additions & 3 deletions app/react/App/NoMatch.js
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<Helmet title='Not Found' />
<h2>404</h2> Page was not found
<h2>404</h2>
</div>
);
}
Expand Down
16 changes: 16 additions & 0 deletions app/react/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/react/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const routes =
<Route path='document/:documentId' component={ViewDocument} />
<Route path='entity/:entityId' component={EntityView} />
<Route path='page/:pageId' component={PageView} />
<Route path='404' component={NoMatch} />
</Route>
;

Expand Down
15 changes: 13 additions & 2 deletions app/react/utils/api.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions app/react/utils/specs/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}});
});

Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit d6416a5

Please sign in to comment.