Skip to content

Commit 85352d6

Browse files
committed
handler wrapper
1 parent 7e1ac4a commit 85352d6

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

controllers/DockerController.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
const ValidationError = require('../errors/ValidationError');
22
const DockerService = require('../services/DockerService');
33
const extractYaml = require('../helpers/extractYaml');
4+
const handler = require('../helpers/handler');
45

56
class DockerController {
6-
static async applyYaml(req, res, next) {
7-
try {
7+
static async applyYaml(req, res) {
88
const file = extractYaml(req.files);
99
if (!file) {
10-
next(new ValidationError('no file provided'));
11-
return;
10+
throw new ValidationError('no file provided');
1211
}
1312

1413
const result = await DockerService.applyYaml(file.data.toString());
1514
res.json(result);
16-
} catch (e) {
17-
next(e);
18-
}
1915
}
2016
}
2117

22-
module.exports = DockerController;
18+
module.exports = handler(DockerController);
2319

2420

helpers/handler.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = function (controller) {
2+
for (const property of Object.getOwnPropertyNames(controller)) {
3+
let endpoint = controller[property];
4+
if (typeof endpoint === 'function') {
5+
controller[property] = async function (req, res, next) {
6+
try {
7+
await endpoint(req, res, next);
8+
} catch (e) {
9+
next(e)
10+
}
11+
}
12+
}
13+
}
14+
return controller;
15+
};

0 commit comments

Comments
 (0)