2018-05-10 08:56:07 -04:00
|
|
|
import * as express from 'express';
|
|
|
|
import {promisify} from 'util';
|
|
|
|
import {UploadError} from './upload-error';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the response to report that an error has occurred.
|
|
|
|
* @param res The response to configure as an error.
|
|
|
|
* @param err The error that needs to be reported.
|
|
|
|
*/
|
2018-08-10 06:10:22 -04:00
|
|
|
export async function respondWithError(res: express.Response, err: any): Promise<void> {
|
2018-05-10 08:56:07 -04:00
|
|
|
if (!(err instanceof UploadError)) {
|
|
|
|
err = new UploadError(500, String((err && err.message) || err));
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(err.status);
|
|
|
|
await promisify(res.end.bind(res))(err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw an exception that describes the given error information.
|
|
|
|
* @param status The HTTP status code include in the error.
|
|
|
|
* @param error The error message to include in the error.
|
|
|
|
* @param req The request that triggered this error.
|
|
|
|
*/
|
|
|
|
export function throwRequestError(status: number, error: string, req: express.Request): never {
|
|
|
|
const message = `${error} in request: ${req.method} ${req.originalUrl}` +
|
|
|
|
(!req.body ? '' : ` ${JSON.stringify(req.body)}`);
|
|
|
|
throw new UploadError(status, message);
|
|
|
|
}
|