File tree 5 files changed +100
-3
lines changed 5 files changed +100
-3
lines changed Original file line number Diff line number Diff line change 1
1
# Custom Error Exceptions
2
2
3
- [ ![ Test Status] ( https://img.shields.io/badge/coverage-100%25-brightgreen )] ( https://github.com/brothergiez/custom-error-exceptions/ ) [ ![ Version Status] ( https ://img.shields.io/badge/npm-v1.1.0 -blue)] ( https://github.com/brothergiez/custom-error-exceptions/ ) [ ![ Issues Status] ( https://img.shields.io/github/issues/brothergiez/custom-error-exceptions )] ( https://github.com/brothergiez/custom-error-exceptions/issues )
3
+ [ ![ Test Status] ( https://img.shields.io/badge/coverage-100%25-brightgreen )] ( https://github.com/brothergiez/custom-error-exceptions/ ) [ ![ Version Status] ( http ://img.shields.io/badge/npm-v1.2.2 -blue)] ( https://github.com/brothergiez/custom-error-exceptions/ ) [ ![ Issues Status] ( https://img.shields.io/github/issues/brothergiez/custom-error-exceptions )] ( https://github.com/brothergiez/custom-error-exceptions/issues )
4
4
5
5
Custom error exceptions are made to make it easier to handling errors. You can also make custom errors as needed.
6
6
@@ -165,6 +165,8 @@ The list above is based on the client error list and server error list as on thi
165
165
- Adding logging middleware in every request with winston
166
166
* version 1.2.1
167
167
- Adding and showing error logger to console if request error
168
+ * version 1.2.2
169
+ - Adding handler with view engine (createWebHandler)
168
170
169
171
170
172
# Contributor
Original file line number Diff line number Diff line change
1
+ const {
2
+ errorLogger
3
+ } = require ( '../logger' ) ;
4
+ /**
5
+ * create web handler function
6
+ * @param {function } handler logic handler to be executed
7
+ * @returns {object } whatever "next" returns
8
+ */
9
+ const createWebHandler = ( handler ) => {
10
+ let response ;
11
+ response = async ( req , res ) => {
12
+ try {
13
+ await handler ( req , res ) ;
14
+ } catch ( err ) {
15
+ if ( ! err . statusCode ) {
16
+ errorLogger ( err , req , res ) ;
17
+ response = res . status ( 500 ) . send ( {
18
+ code : 500 ,
19
+ statusCode : 'INTERNAL_SERVER_ERROR' ,
20
+ message : err . message
21
+ } ) ;
22
+ }
23
+ response = res . status ( err . statusCode ) . send ( {
24
+ code : err . code ,
25
+ statusCode : err . statusCode ,
26
+ message : err . message
27
+ } ) ;
28
+ }
29
+ } ;
30
+ return response ;
31
+ } ;
32
+
33
+ module . exports = createWebHandler ;
Original file line number Diff line number Diff line change 1
1
const errorHandler = require ( './errorHandler' ) ;
2
2
const notFoundHandler = require ( './notFoundHandler' ) ;
3
3
const createHandler = require ( './createHandler' ) ;
4
+ const createWebHandler = require ( './createWebHandler' ) ;
4
5
5
6
module . exports = {
6
7
errorHandler,
7
8
notFoundHandler,
8
- createHandler
9
+ createHandler,
10
+ createWebHandler
9
11
} ;
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " custom-error-exceptions" ,
3
- "version" : " 1.2.1 " ,
3
+ "version" : " 1.2.2 " ,
4
4
"description" : " Custom error exceptions for microservices" ,
5
5
"keywords" : [
6
6
" error" ,
Original file line number Diff line number Diff line change
1
+ const chai = require ( 'chai' ) ;
2
+ const sinon = require ( 'sinon' ) ;
3
+ const sinonChai = require ( 'sinon-chai' ) ;
4
+
5
+ const { createWebHandler } = require ( '../../../lib/handlers' ) ;
6
+
7
+ const { expect } = chai ;
8
+ chai . should ( ) ;
9
+ chai . use ( sinonChai ) ;
10
+
11
+ describe ( 'createWebHandler' , ( ) => {
12
+ let stub ;
13
+ let req ;
14
+ let res ;
15
+ let func ;
16
+ let errorLogger ;
17
+
18
+ beforeEach ( ( ) => {
19
+ stub = sinon . stub ( ) ;
20
+ req = sinon . stub ( ) ;
21
+ res = {
22
+ status : ( ) => ( {
23
+ send : sinon . stub ( )
24
+ } ) ,
25
+ on : sinon . stub ( )
26
+ } ;
27
+ func = createWebHandler ( stub ) ;
28
+ errorLogger = ( ) => sinon . stub ( ) ;
29
+ } ) ;
30
+
31
+ it ( 'should return a function that calls' , async ( ) => {
32
+ await func ( req , res ) ;
33
+ stub . resolves ( ) ;
34
+ expect ( stub . calledWith ( req , res ) ) . to . equal ( true ) ;
35
+ } ) ;
36
+
37
+ it ( 'should calls with the error' , async ( ) => {
38
+ const error = {
39
+ statusCode : 500 ,
40
+ code : 'INTERNAL_SERVER_ERROR' ,
41
+ message : 'Internal Server Erro'
42
+ } ;
43
+ stub . rejects ( error ) ;
44
+ await func ( req , res ) ;
45
+ res . status . send = error ;
46
+ expect ( res . status . send ) . to . deep . equal ( error ) ;
47
+ } ) ;
48
+
49
+ it ( 'should calls with the error without status code' , async ( ) => {
50
+ const error = {
51
+ code : 'INTERNAL_SERVER_ERROR' ,
52
+ message : 'Internal Server Erro'
53
+ } ;
54
+ stub . rejects ( error ) ;
55
+ await func ( req , res ) ;
56
+ errorLogger ( error , req , res ) ;
57
+ res . status . send = error ;
58
+ expect ( res . status . send ) . to . deep . equal ( error ) ;
59
+ } ) ;
60
+ } ) ;
You can’t perform that action at this time.
0 commit comments