mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) move home server into core
Summary: This moves enough server material into core to run a home server. The data engine is not yet incorporated (though in manual testing it works when ported). Test Plan: existing tests pass Reviewers: dsagal Reviewed By: dsagal Differential Revision: https://phab.getgrist.com/D2552
This commit is contained in:
36
app/server/lib/expressWrap.ts
Normal file
36
app/server/lib/expressWrap.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import {RequestWithLogin} from 'app/server/lib/Authorizer';
|
||||
import * as log from 'app/server/lib/log';
|
||||
import * as express from 'express';
|
||||
|
||||
/**
|
||||
* Wrapper for async express endpoints to catch errors and forward them to the error handler.
|
||||
*/
|
||||
export function expressWrap(callback: express.RequestHandler): express.RequestHandler {
|
||||
return async (req, res, next) => {
|
||||
try {
|
||||
await callback(req, res, next);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Error-handling middleware that responds to errors in json. The status code is taken from
|
||||
* error.status property (for which ApiError is convenient), and defaults to 500.
|
||||
*/
|
||||
export const jsonErrorHandler: express.ErrorRequestHandler = (err, req, res, next) => {
|
||||
const mreq = req as RequestWithLogin;
|
||||
log.warn("Error during api call to %s: (%s) user %d params %s body %s", req.path, err.message,
|
||||
mreq.userId,
|
||||
JSON.stringify(req.params), JSON.stringify(req.body));
|
||||
res.status(err.status || 500).json({error: err.message || 'internal error',
|
||||
details: err.details});
|
||||
};
|
||||
|
||||
/**
|
||||
* Middleware that responds with a 404 status and a json error object.
|
||||
*/
|
||||
export const jsonNotFoundHandler: express.RequestHandler = (req, res, next) => {
|
||||
res.status(404).json({error: `not found: ${req.url}`});
|
||||
};
|
||||
Reference in New Issue
Block a user