Work in progress on a React-based error reporting mechanism.
The idea is that REST handlers always fail with throwing an Error (subclass of InteroperableError). The InteroperableError contains type and data field which are JSON-serialized and sent to client. It's up to the client to interpret the error and choose an appropriate way to present it.
This commit is contained in:
parent
4504d539c5
commit
79ea9e1897
10 changed files with 304 additions and 142 deletions
|
@ -4,28 +4,31 @@ const passport = require('../lib/passport');
|
|||
const router = require('../lib/router-async').create();
|
||||
const _ = require('../lib/translate')._;
|
||||
const namespaces = require('../lib/models/namespaces');
|
||||
const interoperableErrors = require('../lib/interoperable-errors');
|
||||
|
||||
router.all('/rest/*', (req, res, next) => {
|
||||
req.needsJSONResponse = true;
|
||||
|
||||
router.all('/*', (req, res, next) => {
|
||||
if (!req.user) {
|
||||
req.flash('danger', _('Need to be logged in to access restricted content'));
|
||||
return res.redirect('/users/login?next=' + encodeURIComponent(req.originalUrl));
|
||||
throw new interoperableErrors.NotLoggedInError();
|
||||
}
|
||||
// res.setSelectedMenu('namespaces');
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
router.getAsyncJSON('/rest/namespaces/:nsId', async (req, res) => {
|
||||
|
||||
router.getAsync('/rest/namespaces/:nsId', async (req, res) => {
|
||||
const ns = await namespaces.getById(req.params.nsId);
|
||||
return res.json(ns);
|
||||
});
|
||||
|
||||
router.postAsyncJSON('/rest/namespaces', passport.csrfProtection, async (req, res) => {
|
||||
router.postAsync('/rest/namespaces', passport.csrfProtection, async (req, res) => {
|
||||
console.log(req.body);
|
||||
// await namespaces.create(req.body);
|
||||
return res.json();
|
||||
});
|
||||
|
||||
router.putAsyncJSON('/rest/namespaces/:nsId', passport.csrfProtection, async (req, res) => {
|
||||
router.putAsync('/rest/namespaces/:nsId', passport.csrfProtection, async (req, res) => {
|
||||
console.log(req.body);
|
||||
ns = req.body;
|
||||
ns.id = req.params.nsId;
|
||||
|
@ -34,13 +37,13 @@ router.putAsyncJSON('/rest/namespaces/:nsId', passport.csrfProtection, async (re
|
|||
return res.json();
|
||||
});
|
||||
|
||||
router.deleteAsyncJSON('/rest/namespaces/:nsId', passport.csrfProtection, async (req, res) => {
|
||||
router.deleteAsync('/rest/namespaces/:nsId', passport.csrfProtection, async (req, res) => {
|
||||
console.log(req.body);
|
||||
// await namespaces.remove(req.params.nsId);
|
||||
return res.json();
|
||||
});
|
||||
|
||||
router.getAsyncJSON('/rest/namespacesTree', async (req, res) => {
|
||||
router.getAsync('/rest/namespacesTree', async (req, res) => {
|
||||
const entries = {};
|
||||
|
||||
/* Example of roots:
|
||||
|
@ -93,6 +96,15 @@ router.getAsyncJSON('/rest/namespacesTree', async (req, res) => {
|
|||
return res.json(roots);
|
||||
});
|
||||
|
||||
router.all('/*', (req, res, next) => {
|
||||
if (!req.user) {
|
||||
req.flash('danger', _('Need to be logged in to access restricted content'));
|
||||
return res.redirect('/users/login?next=' + encodeURIComponent(req.originalUrl));
|
||||
}
|
||||
// res.setSelectedMenu('namespaces');
|
||||
next();
|
||||
});
|
||||
|
||||
router.getAsync('/*', passport.csrfProtection, async (req, res) => {
|
||||
res.render('react-root', {
|
||||
title: _('Namespaces'),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue