"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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;
//# sourceMappingURL=error.js.map