All files / src/http controller.js

95% Statements 19/20
81.25% Branches 13/16
100% Functions 1/1
95% Lines 19/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56  3x     3x           9x 5x       4x 3x       1x         1x 1x   1x 2x             1x     1x 1x   1x 2x     1x     1x 1x          
import Joi from "joi";
const ValidationError = Joi.ValidationError;
 
import { newLogger } from "@common/utils/logger.js";
const logger = newLogger("Controller|BaseController");
 
import { HttpException } from "@common/exceptions/appExceptions.js";
 
class BaseController {
    reportBadData(error, req, res) {
        if (!(error instanceof ValidationError)) {
            throw error;
        }
 
        // Softly check if res is a response object
        if (typeof res == "undefined" || typeof res.app == "undefined" || typeof res.locals == "undefined") {
            throw new HttpException(500, "Response is required for reporting a Validation Error");
        }
 
        // Softly check if req is a request object
        Iif (typeof req == "undefined" || typeof req.app == "undefined" || typeof req.method == "undefined") {
            throw new HttpException(500, "Request is required for reporting a Validation Error");
        }
 
        // Copy the validation errors, if any, to the list of flash messages
        Eif (Array.isArray(error.details)) {
            const validation = [];
 
            for (const d of error.details) {
                validation.push({
                    message: d.message,
                    layer: "validation",
                    type: "info"
                })
            }
 
            req.flash('validation', validation);
        }
 
        Eif (req.body) {
            const content = {};
 
            for (const d in req.body) {
                content[d] = req.body[d];
            }
 
            req.flash('content', content);
        }
 
        logger.info("Invalid data (%j): %s", req.body, error);
        return res.status(400);
    }
}
 
export default BaseController;