All files / src/lib/routes util.ts

87.88% Statements 29/33
78.95% Branches 15/19
100% Functions 3/3
87.5% Lines 28/32

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 7675x     75x   75x               1815x   12x   1803x       75x   75x       75x 124x     124x   124x 1x     123x   41x   10x   2x   1x   1x   2x       1x   6x       1x   38x   15x   3x           2x 2x      
import joi from 'joi';
import { Response } from 'express';
import { Logger } from '../logger';
import BaseError from '../error/base-error';
 
export const customJoi = joi.extend((j) => ({
    type: 'isUrlFriendly',
    base: j.string(),
    messages: {
        'isUrlFriendly.base': '{{#label}} must be URL friendly',
    },
    validate(value, helpers) {
        // Base validation regardless of the rules applied
        if (encodeURIComponent(value) !== value) {
            // Generate an error, state and options need to be passed
            return { value, errors: helpers.error('isUrlFriendly.base') };
        }
        return undefined;
    },
}));
 
export const nameType = customJoi.isUrlFriendly().min(1).max(100).required();
 
export const handleErrors: (
    res: Response,
    logger: Logger,
    error: Error,
) => void = (res, logger, error) => {
    logger.warn(error.message);
    // @ts-ignore
    // eslint-disable-next-line no-param-reassign
    error.isJoi = true;
 
    if (error instanceof BaseError) {
        return res.status(error.statusCode).json(error).end();
    }
 
    switch (error.name) {
        case 'ValidationError':
            return res.status(400).json(error).end();
        case 'BadDataError':
            return res.status(400).json(error).end();
        case 'OwaspValidationError':
            return res.status(400).json(error).end();
        case 'PasswordUndefinedError':
            return res.status(400).json(error).end();
        case 'MinimumOneEnvironmentError':
            return res.status(400).json(error).end();
        case 'InvalidTokenError':
            return res.status(401).json(error).end();
        case 'NoAccessError':
            return res.status(403).json(error).end();
        case 'UsedTokenError':
            return res.status(403).json(error).end();
        case 'InvalidOperationError':
            return res.status(403).json(error).end();
        case 'IncompatibleProjectError':
            return res.status(403).json(error).end();
        case 'OperationDeniedError':
            return res.status(403).json(error).end();
        case 'NotFoundError':
            return res.status(404).json(error).end();
        case 'NameExistsError':
            return res.status(409).json(error).end();
        case 'FeatureHasTagError':
            return res.status(409).json(error).end();
        case 'RoleInUseError':
            return res.status(400).json(error).end();
        case 'ProjectWithoutOwnerError':
            return res.status(409).json(error).end();
        default:
            logger.error('Server failed executing request', error);
            return res.status(500).end();
    }
};