{"size": 1133, "ext": "js", "max_stars_repo_path": "server/src/error.js", "max_stars_repo_name": "benkyd/U30221-Application-Programming", "max_stars_repo_stars_event_min_datetime": "2021-05-24T19:30:34.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-24T19:30:34.000Z", "max_issues_repo_path": "server/src/error.js", "max_issues_repo_name": "benkyd/scrabble-online", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/src/error.js", "max_forks_repo_name": "benkyd/scrabble-online", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.5344827586, "max_line_length": 66, "alphanum_fraction": 0.5207413945}
const Logger = require('./logger.js');

const Express = require('express');

/*
STANDARD ERROR RESPONSE
{
    errors: [
        {
            code: error code,
            error: "Short error",
            desc: "Description of the error"
        }   
    ]
}
*/
// Would automatically convert code to error
// but that would be too much effort and i cba
// TODO: That ^
module.exports = class Error
{
    constructor()
    {
        this.errors = [];
    }

    addError(code, error, desc)
    {
        this.errors.push({code: code, error: error, desc: desc});
    }

    isErrored()
    {
        return this.errors.length >= 0;
    }

    end(expressRes)
    {
        expressRes.status(this.code).end(this.toString);
    }

    get toError()
    {
        return this.errors;
    }

    get toString()
    {
        return JSON.stringify({errors: this.toError});
    }

    get code()
    {
        // Get the error code of the last error in the list
        // to return to the client if there is multiple
        return this.errors[this.errors.length - 1].code;
    }
}