-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
error handling middleware refactor and users error handling changed to
calling next with the error
- Loading branch information
Showing
9 changed files
with
135 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,10 @@ | ||
import errorLog from 'api/log/errorLog'; | ||
import handleError from './handleError'; | ||
|
||
export default function (req, res, next) { | ||
res.error = (error) => { | ||
let result = error; | ||
if (error instanceof Error) { | ||
result = error.stack.split('\n'); | ||
} | ||
export default function (error, req, res, next) { | ||
const handled = handleError(error); | ||
|
||
if (error.code) { | ||
result = error.message; | ||
} | ||
res.status(handled.code); | ||
res.json({ error: handled.message }); | ||
|
||
let code = error.code || 500; | ||
|
||
if (!(error.code > 0) || error.name === 'MongoError') { | ||
code = 500; | ||
} | ||
|
||
res.status(code); | ||
res.json({ error: result }); | ||
|
||
if (error.code === 500) { | ||
errorLog.error(result); | ||
} | ||
}; | ||
next(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import errorLog from 'api/log/errorLog'; | ||
|
||
export default (error, uncaught = false) => { | ||
let result = error; | ||
|
||
if (error instanceof Error) { | ||
result = { code: 500, message: error.stack }; | ||
} | ||
|
||
if (error.name === 'MongoError') { | ||
result.code = 500; | ||
} | ||
|
||
if (uncaught) { | ||
result.message = `uncaught exception or unhandled rejection, Node process finished !!\n ${result.message}`; | ||
} | ||
|
||
if (result.code === 500) { | ||
errorLog.error(result.message); | ||
} | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import errorLog from 'api/log/errorLog'; | ||
import { createError } from 'api/utils'; | ||
|
||
import handleError from '../handleError'; | ||
|
||
describe('handleError', () => { | ||
beforeEach(() => { | ||
spyOn(errorLog, 'error'); | ||
}); | ||
|
||
describe('when error is instance of Error', () => { | ||
it('should return the error with 500 code', () => { | ||
const errorInstance = new Error('error'); | ||
const error = handleError(errorInstance); | ||
|
||
expect(error.code).toBe(500); | ||
expect(error.message).toEqual(errorInstance.stack); | ||
}); | ||
it('should log the error', () => { | ||
const error = new Error('error'); | ||
handleError(error); | ||
|
||
expect(errorLog.error).toHaveBeenCalledWith(error.stack); | ||
}); | ||
}); | ||
|
||
describe('when error is created with createError', () => { | ||
it('should return the error', () => { | ||
const error = handleError(createError('test error', 400)); | ||
expect(error).toEqual(createError('test error', 400)); | ||
}); | ||
it('should not log the error when code is not 500', () => { | ||
handleError(createError('test error', 400)); | ||
expect(errorLog.error).not.toHaveBeenCalled(); | ||
|
||
handleError(createError('test error')); | ||
expect(errorLog.error).toHaveBeenCalledWith('test error'); | ||
}); | ||
}); | ||
describe('when error is a MongoError', () => { | ||
it('should return the error with a 500 code', () => { | ||
const error = handleError({ name: 'MongoError', message: 'error', code: '345' }); | ||
expect(error.code).toBe(500); | ||
expect(error.message).toBe('error'); | ||
}); | ||
}); | ||
describe('when error is uncaught', () => { | ||
it('should append the info into the message', () => { | ||
const uncaught = true; | ||
const error = handleError({ message: 'error' }, uncaught); | ||
expect(error.message).toBe('uncaught exception or unhandled rejection, Node process finished !!\n error'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters