forked from wbhob/nest-middlewares
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(errorhandler): add errorhandler middleware
- Loading branch information
Showing
4 changed files
with
88 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ErrorHandlerMiddleware } from './index'; | ||
import { expect } from 'chai'; | ||
|
||
describe('ErrorHandlerMiddleware', () => { | ||
let middleware: ErrorHandlerMiddleware; | ||
describe('properly configured', () => { | ||
beforeEach(() => { | ||
ErrorHandlerMiddleware.configure({ | ||
log: true, | ||
}); | ||
middleware = new ErrorHandlerMiddleware(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(middleware).to.not.be.undefined; | ||
}); | ||
|
||
it('should have a function called resolve', () => { | ||
expect(middleware.resolve).to.be.instanceof(Function); | ||
}); | ||
|
||
it('should should return a middleware from calling resolve', () => { | ||
expect(middleware.resolve()).to.be.an.instanceof(Function); | ||
}); | ||
afterEach(() => { | ||
ErrorHandlerMiddleware.configure(undefined); | ||
}); | ||
}); | ||
|
||
describe('not configured', () => { | ||
middleware = new ErrorHandlerMiddleware(); | ||
it('should should return a middleware from calling resolve', () => { | ||
expect(middleware.resolve()).to.be.an.instanceof(Function); | ||
}); | ||
}); | ||
}); |
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 * as errorhandler from 'errorhandler'; | ||
|
||
import { ErrorRequestHandler, RequestHandler } from 'express'; | ||
import { Middleware, NestMiddleware } from '@nestjs/common'; | ||
|
||
@Middleware() | ||
export class ErrorHandlerMiddleware implements NestMiddleware { | ||
|
||
// DELETE THESE LINES IF MIDDLEWARE DOES NOT TAKE OPTIONS | ||
public static configure(opts: errorhandler.Options) { | ||
this.options = opts; | ||
} | ||
|
||
private static options: errorhandler.Options; | ||
|
||
public resolve(...args: any[]): RequestHandler { | ||
if (ErrorHandlerMiddleware.options) { | ||
return errorhandler(ErrorHandlerMiddleware.options) as any as RequestHandler; | ||
} else { | ||
return errorhandler() as any as RequestHandler; | ||
} | ||
} | ||
} |
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,28 @@ | ||
{ | ||
"name": "@nest-middlewares/errorhandler", | ||
"version": "1.0.0", | ||
"description": "NestJS middleware for Error Handler", | ||
"main": "index", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/wbhob/nest-middlewares.git" | ||
}, | ||
"keywords": [ | ||
"nest", | ||
"middlewares", | ||
"express", | ||
"node" | ||
], | ||
"author": "Wilson Hobbs <wilsonhobbs1@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/wbhob/nest-middlewares/issues" | ||
}, | ||
"homepage": "https://github.com/wbhob/nest-middlewares#readme", | ||
"peerDependencies": { | ||
"@nestjs/common": "^4.0.0" | ||
}, | ||
"dependencies": { | ||
"errorhandler": "^1.5.0" | ||
} | ||
} |