Seems that hierarchical error handling works..
TreeTable component seems to work too. Edit is half-way through. Create / delete are TBD.
This commit is contained in:
parent
79ea9e1897
commit
5e4c86f626
24 changed files with 9967 additions and 261 deletions
|
@ -2,32 +2,24 @@
|
|||
|
||||
const express = require('express');
|
||||
|
||||
function safeAsyncHandler(handler) {
|
||||
return function(req, res, next) {
|
||||
handler(req, res).catch(error => next(error));
|
||||
};
|
||||
}
|
||||
|
||||
function replaceLast(elems, replaceFn) {
|
||||
if (elems.length === 0) {
|
||||
return elems;
|
||||
function replaceLastBySafeHandler(handlers) {
|
||||
if (handlers.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const lastElem = elems[elems.size - 1];
|
||||
const replacement = replaceFn(lastElem);
|
||||
|
||||
elems[elems.size - 1] = replacement;
|
||||
|
||||
return elems;
|
||||
const lastHandler = handlers[handlers.length - 1];
|
||||
const ret = handlers.slice();
|
||||
ret[handlers.length - 1] = (req, res, next) => lastHandler(req, res).catch(error => next(error));
|
||||
return ret;
|
||||
}
|
||||
|
||||
function create() {
|
||||
const router = new express.Router();
|
||||
|
||||
router.getAsync = (path, ...handlers) => router.get(path, ...replaceLast(handlers, safeAsyncHandler));
|
||||
router.postAsync = (path, ...handlers) => router.post(path, ...replaceLast(handlers, safeAsyncHandler));
|
||||
router.putAsync = (path, ...handlers) => router.put(path, ...replaceLast(handlers, safeAsyncHandler));
|
||||
router.deleteAsync = (path, ...handlers) => router.delete(path, ...replaceLast(handlers, safeAsyncHandler));
|
||||
router.getAsync = (path, ...handlers) => router.get(path, ...replaceLastBySafeHandler(handlers));
|
||||
router.postAsync = (path, ...handlers) => router.post(path, ...replaceLastBySafeHandler(handlers));
|
||||
router.putAsync = (path, ...handlers) => router.put(path, ...replaceLastBySafeHandler(handlers));
|
||||
router.deleteAsync = (path, ...handlers) => router.delete(path, ...replaceLastBySafeHandler(handlers));
|
||||
|
||||
return router;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue