Skip to content

Commit fd81c3f

Browse files
committed
Adding handler with view engine (createWebHandler)
1 parent 0397a90 commit fd81c3f

File tree

5 files changed

+100
-3
lines changed

5 files changed

+100
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Custom Error Exceptions
22

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)
44

55
Custom error exceptions are made to make it easier to handling errors. You can also make custom errors as needed.
66

@@ -165,6 +165,8 @@ The list above is based on the client error list and server error list as on thi
165165
- Adding logging middleware in every request with winston
166166
* version 1.2.1
167167
- Adding and showing error logger to console if request error
168+
* version 1.2.2
169+
- Adding handler with view engine (createWebHandler)
168170

169171

170172
# Contributor

lib/handlers/createWebHandler.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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;

lib/handlers/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const errorHandler = require('./errorHandler');
22
const notFoundHandler = require('./notFoundHandler');
33
const createHandler = require('./createHandler');
4+
const createWebHandler = require('./createWebHandler');
45

56
module.exports = {
67
errorHandler,
78
notFoundHandler,
8-
createHandler
9+
createHandler,
10+
createWebHandler
911
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "custom-error-exceptions",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "Custom error exceptions for microservices",
55
"keywords": [
66
"error",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
});

0 commit comments

Comments
 (0)