"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationError = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const objects_model_1 = require("@jacquesparis/objects-model");
const rest_1 = require("@loopback/rest");
const lodash_1 = require("lodash");
class ApplicationError {
    constructor(error, errorCode, 
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    errorArgs) {
        this.errorCode = errorCode;
        this.errorArgs = errorArgs;
        lodash_1.merge(this, error);
    }
    static authenticationNeeded() {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.AUTHENTICATION_NEEDED].statusCode]('Authentication is required and has not been provided.'), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.AUTHENTICATION_NEEDED].errorCode, {});
    }
    static forbidden() {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.FORBIDDEN].statusCode]('The access rights do not allow access to this resource.'), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.FORBIDDEN].errorCode, {});
    }
    static authenticationFailed() {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.AUTHENTICATION_FAILED].statusCode]('Authentication failed.'), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.AUTHENTICATION_FAILED].errorCode, {});
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    static unexpectedError(err) {
        console.trace(err);
        if (undefined !== err.statusCode) {
            return new ApplicationError(err, objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.UNEXPECTED_ERROR].errorCode, {});
        }
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.UNEXPECTED_ERROR].statusCode]('The request could not be executed as requested.'), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.UNEXPECTED_ERROR].errorCode, {});
    }
    static notFound(fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.NOT_FOUND].statusCode]('No ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.NOT_FOUND].errorCode, fields);
    }
    static wrongValue(fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.WRONG_VALUE].statusCode]('Wrong value accessing ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.WRONG_VALUE].errorCode, fields);
    }
    static format(format, fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.FORMAT].statusCode]('Format error. Format expected ' +
            format +
            ' for ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.FORMAT].errorCode, lodash_1.merge({ format }, fields));
    }
    static tooMany(fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.TOO_MANY_OBJECT_CONSTRAINT].statusCode]('Too many ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.TOO_MANY_OBJECT_CONSTRAINT].errorCode, fields);
    }
    static corruptedData(fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.CORRUPTED_OBJECT].statusCode]('Corrupted ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.CORRUPTED_OBJECT].errorCode, fields);
    }
    static conflict(fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.CONFLICT].statusCode]('Duplicated ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.CONFLICT].errorCode, fields);
    }
    static missing(fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.MISSING].statusCode]('Missing ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.MISSING].errorCode, fields);
    }
    static incompatible(fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.INCOMPATIBLE].statusCode]('Incompatible with ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.INCOMPATIBLE].errorCode, fields);
    }
    static missingParameter(parameter) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.MISSING_PARAMETER].statusCode]('Missing parameter ' + parameter), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.MISSING_PARAMETER].errorCode, { parameter: parameter });
    }
    static unauthorizedValue(fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.UNAUTHORIZED_VALUE].statusCode]('Unauthorized ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.UNAUTHORIZED_VALUE].errorCode, fields);
    }
    static notImplemented(fields) {
        return new ApplicationError(new rest_1.HttpErrors[objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.NOT_IMPLEMENTED].statusCode]('Not implemented ' +
            Object.keys(fields)
                .map((key) => key + ' ' + fields[key])
                .join(', ')), objects_model_1.APPLICATION_ERRORS[objects_model_1.ApplicationErrors.NOT_IMPLEMENTED].errorCode, fields);
    }
}
exports.ApplicationError = ApplicationError;
//# sourceMappingURL=application-error.js.map