Skip to content

Commit

Permalink
Merge branch 'feature/improve-error-handling' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
gocreating committed Oct 14, 2016
2 parents f52d11d + 263e6cd commit ca4db0d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ git flow feature finish upgrade-mirror

- [x] Travis Testing
- [x] Asynchronous redux-form validation (detect duplicate email)
- [x] Error handling
- reference: [Checklist: Best Practices of Node.JS Error Handling](http://goldbergyoni.com/checklist-best-practices-of-node-js-error-handling/)
- [ ] Pagination Mechanism

### v1.0+

- [ ] Social Login
- [ ] [Error handling](http://goldbergyoni.com/checklist-best-practices-of-node-js-error-handling/)
- [ ] Disable submit button when form submitting
- [ ] Todo#Update API & Todo#Edit Functionality
- [ ] Mail System
4 changes: 4 additions & 0 deletions src/common/actions/errorActions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Errors from '../constants/Errors';
import actionTypes from '../constants/actionTypes';

export const pushError = (error, meta) => {
Expand All @@ -11,6 +12,9 @@ export const pushError = (error, meta) => {
};

export const pushErrors = (errors) => {
if (errors && errors.length === undefined) {
return pushError(Errors.UNKNOWN_EXCEPTION, errors);
}
return {
type: actionTypes.PUSH_ERRORS,
errors,
Expand Down
6 changes: 6 additions & 0 deletions src/common/components/utils/ErrorList.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ let ErrorList = ({ errors, dispatch }) => (
<p key="1">
{error.meta.path && `(at path '${error.meta.path}')`}
</p>
), (
<p key="2">
{error.code === Errors.UNKNOWN_EXCEPTION.code && (
<span>{error.meta.toString()}</span>
)}
</p>
)]}
</div>
))}
Expand Down
19 changes: 19 additions & 0 deletions src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ const appPromise = new Promise((resolve, reject) => {
const app = express();
app.set('env', env);

// error handler for the whole app process
process.on('uncaughtException', (err) => {
console.log('uncaughtException', err);
process.exit(1);
});

process.on('unhandledRejection', (reason, p) => {
throw reason;
});

// initialize firebase
if (configs.firebase && clientConfigs.firebase) {
let firebase = require('firebase');
Expand All @@ -36,6 +46,15 @@ const appPromise = new Promise((resolve, reject) => {
console.log('[Service] [Mongo]\tenabled');
middlewares({ app });
routes({ app });
// error handler for the current request
app.use((err, req, res, next) => {
console.error(err.stack);
if (env !== 'production') {
res.status(500).send(`<pre>${err.stack}</pre>`);
} else {
res.status(500).send('Service Unavailable');
}
});
return resolve(app);
});
} else {
Expand Down

0 comments on commit ca4db0d

Please sign in to comment.