"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleStaticError = exports.apiFailMiddleware = void 0;
const http_problem_details_1 = require("http-problem-details");
const logger_1 = require("../helpers/logger");
function apiFailMiddleware(req, res, next) {
    res.fail = options => {
        const { status = 400, message, title, type, data, instance } = options;
        const extension = new http_problem_details_1.ProblemDocumentExtension(Object.assign(Object.assign({}, data), { docs: res.locals.docUrl, code: type, error: message }));
        res.status(status);
        res.setHeader('Content-Type', 'application/problem+json');
        const json = new http_problem_details_1.ProblemDocument({
            status,
            title,
            instance,
            detail: message,
            type: type
                ? `https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/${type}`
                : undefined
        }, extension);
        logger_1.logger.debug('Bad HTTP request.', { json });
        res.json(json);
    };
    if (next)
        next();
}
exports.apiFailMiddleware = apiFailMiddleware;
function handleStaticError(err, req, res, next) {
    const message = err.message || '';
    if (message.includes('ENOENT')) {
        return res.fail({
            status: err.status || 500,
            message: err.message,
            type: err.name
        });
    }
    return next(err);
}
exports.handleStaticError = handleStaticError;
//# sourceMappingURL=error.js.map