"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAggregateError = exports.getStatusCode = void 0;
const BadRequestHttpError_1 = require("./BadRequestHttpError");
const ErrorUtil_1 = require("./ErrorUtil");
const HttpError_1 = require("./HttpError");
const InternalServerError_1 = require("./InternalServerError");
/**
 * Returns the HTTP status code corresponding to the error.
 */
function getStatusCode(error) {
    return HttpError_1.HttpError.isInstance(error) ? error.statusCode : 500;
}
exports.getStatusCode = getStatusCode;
/**
 * Combines a list of errors into a single HttpErrors.
 * Status code depends on the input errors. If they all share the same status code that code will be re-used.
 * If they are all within the 4xx range, 400 will be used, otherwise 500.
 *
 * @param errors - Errors to combine.
 */
function createAggregateError(errors) {
    const httpErrors = errors.map((error) => HttpError_1.HttpError.isInstance(error) ? error : new InternalServerError_1.InternalServerError((0, ErrorUtil_1.createErrorMessage)(error)));
    const messages = httpErrors.map((error) => error.message).filter((msg) => msg.length > 0);
    // Let message depend on the messages that were present.
    // This prevents a bunch of empty strings being joined in the case most of them were 404s.
    let message;
    if (messages.length === 0) {
        message = '';
    }
    else if (messages.length === 1) {
        message = messages[0];
    }
    else {
        message = `Multiple handler errors: ${messages.join(', ')}`;
    }
    // Check if all errors have the same status code
    if (httpErrors.length > 0 && httpErrors.every((error) => error.statusCode === httpErrors[0].statusCode)) {
        return new HttpError_1.HttpError(httpErrors[0].statusCode, httpErrors[0].name, message);
    }
    // Find the error range (4xx or 5xx)
    if (httpErrors.some((error) => error.statusCode >= 500)) {
        return new InternalServerError_1.InternalServerError(message);
    }
    return new BadRequestHttpError_1.BadRequestHttpError(message);
}
exports.createAggregateError = createAggregateError;
//# sourceMappingURL=HttpErrorUtil.js.map