<commit_before>import * as assert from 'assert';
import {Request as ExpressRequest, RequestHandler as ExpressRequestHandler} from 'express';
import {HttpError} from 'httperr';
import * as url from 'url';
import {GUEST, User} from '../../identity-types';
import {default as RoutistRequest} from '../../request';
import {default as RoutistResponse} from '../../response';
import debug from '../../util/debug';





// TODO: ...
export default function createMiddleware(handler: RoutistRequestHandler): ExpressRequestHandler {
    return async (req, res, next) => {
        let reqx = augmentRequest(req);
        try {
            let handled = await handler(reqx, res);
            if (!handled) {
                return next();
            }
        }
        catch (err) {
            // TODO: temp testing... better format/info?
            debug(`REQUEST ERROR: ${err}`);

            if (err instanceof HttpError) {
                res.status(err.statusCode);
                res.send(err.message); // TODO: don't leak server details to client... how to ensure this reliably??
            }
            else {
                res.status(500).send('Internal server error');
            }
        }
    };
}

<commit_msg>Don't send response on errors (except HttpErrors)<commit_after>import * as assert from 'assert';
import {Request as ExpressRequest, RequestHandler as ExpressRequestHandler} from 'express';
import {HttpError} from 'httperr';
import * as url from 'url';
import {GUEST, User} from '../../identity-types';
import {default as RoutistRequest} from '../../request';
import {default as RoutistResponse} from '../../response';
import debug from '../../util/debug';





// TODO: ...
export default function createMiddleware(handler: RoutistRequestHandler): ExpressRequestHandler {
    return async (req, res, next) => {
        let reqx = augmentRequest(req);
        try {
            let handled = await handler(reqx, res);
            if (!handled) {
                return next();
            }
        }
        catch (err) {
            // TODO: temp testing... better format/info?
            debug(`REQUEST ERROR: ${err}`);

            // If the error is an HttpError, then respond to it. The permissions system uses this to respond with 403.
            // Client can also use it as a way to get routist to handle errors (ie send response) directly.
            // TODO: doc special case handling of HttpError instances - this is effectively part of the public API.
            if (err instanceof HttpError) {
                res.status(err.statusCode);
                res.send(err.message); // TODO: don't leak server details to client... how to ensure this reliably??
                return;
            }

            // In general, routist doesn't handle/respond on errors.The client application should do that for itself.
            next(err);
        }
    };
}


{'fraction_non_alphanumeric': 0.11813471502590674, 'fraction_numerical': 0.002072538860103627, 'mean_word_length': 2.134199134199134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6805210', 'n_tokens_mistral': 739, 'n_tokens_neox': 714, 'n_words': 273}