Skip to content

Commit ca4db0d

Browse files
committed
Merge branch 'feature/improve-error-handling' into dev
2 parents f52d11d + 263e6cd commit ca4db0d

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,13 @@ git flow feature finish upgrade-mirror
206206

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

211213
### v1.0+
212214

213215
- [ ] Social Login
214-
- [ ] [Error handling](http://goldbergyoni.com/checklist-best-practices-of-node-js-error-handling/)
215216
- [ ] Disable submit button when form submitting
216217
- [ ] Todo#Update API & Todo#Edit Functionality
217218
- [ ] Mail System

src/common/actions/errorActions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Errors from '../constants/Errors';
12
import actionTypes from '../constants/actionTypes';
23

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

1314
export const pushErrors = (errors) => {
15+
if (errors && errors.length === undefined) {
16+
return pushError(Errors.UNKNOWN_EXCEPTION, errors);
17+
}
1418
return {
1519
type: actionTypes.PUSH_ERRORS,
1620
errors,

src/common/components/utils/ErrorList.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ let ErrorList = ({ errors, dispatch }) => (
3333
<p key="1">
3434
{error.meta.path && `(at path '${error.meta.path}')`}
3535
</p>
36+
), (
37+
<p key="2">
38+
{error.code === Errors.UNKNOWN_EXCEPTION.code && (
39+
<span>{error.meta.toString()}</span>
40+
)}
41+
</p>
3642
)]}
3743
</div>
3844
))}

src/server/app.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ const appPromise = new Promise((resolve, reject) => {
1515
const app = express();
1616
app.set('env', env);
1717

18+
// error handler for the whole app process
19+
process.on('uncaughtException', (err) => {
20+
console.log('uncaughtException', err);
21+
process.exit(1);
22+
});
23+
24+
process.on('unhandledRejection', (reason, p) => {
25+
throw reason;
26+
});
27+
1828
// initialize firebase
1929
if (configs.firebase && clientConfigs.firebase) {
2030
let firebase = require('firebase');
@@ -36,6 +46,15 @@ const appPromise = new Promise((resolve, reject) => {
3646
console.log('[Service] [Mongo]\tenabled');
3747
middlewares({ app });
3848
routes({ app });
49+
// error handler for the current request
50+
app.use((err, req, res, next) => {
51+
console.error(err.stack);
52+
if (env !== 'production') {
53+
res.status(500).send(`<pre>${err.stack}</pre>`);
54+
} else {
55+
res.status(500).send('Service Unavailable');
56+
}
57+
});
3958
return resolve(app);
4059
});
4160
} else {

0 commit comments

Comments
 (0)