All files RouterError.js

100% Statements 22/22
100% Branches 5/5
100% Functions 13/13
100% Lines 22/22
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              47x 47x 47x 47x 25x     47x       47x 2x 4x 2x 5x 1x 6x 1x 7x 1x 1x 16x 1x   47x 564x   47x  
import {
    toLowerCase,
    toTitleCase
} from './utils.js';
 
export default class RouterError extends Error {
    constructor (errorTemplate, errorOptions={}) {
        let message = errorTemplate.code + ' ★ ';
        message += errorTemplate(errorOptions);
        const {routeName} = errorOptions;
        if (routeName) {
            message += ` Check the route ${routeName}.`;
        }
 
        super(message);
    }
}
 
const errorTemplates = {
    INVALID_ROUTE_NAME: ({desiredRouteName}) => `Invalid route's name; ${desiredRouteName} was not found!`,
    INVALID_INPUT_TYPE: ({entity, type}) => `${toTitleCase(entity)} must be presented by ${toLowerCase(type)}.`,
    INVALID_URI_STRUCTURE: ({entity}) => `${toTitleCase(entity)} cannot be splitted because it has invalid structure.`,
    INVALID_PATH_STRUCTURE: ({entity}) => `Path in ${toLowerCase(entity)} must be absolute when domain is specified!`,
    INVALID_DEFAULT_GETTER: () => `Method getDefaultPart must be presented by function that returns instances of default URI components.`,
    INCONSISTENT_DATA: () => `Data is inconsistent; generated URI cannot be matched.`,
    MATCH_FRAGMENT_EXPECTED: () => `Match function must return null or MatchFragment's instance.`,
    FUNCTION_EXPECTED: ({entity}) => `Only function could be used as parameter for the whole ${toLowerCase(entity)}.`,
    DOMAIN_EXPECTED: () => `URI must contain not empty domain when protocol or port is specified.`,
    ABSOLUTE_PATH_EXPECTED: ({currentPath}) => `Matching URI path must be absolute. Current path is ${currentPath}.`,
    PATH_COMPONENT_EXPECTED: ({paramName}) => `Path component cannot be empty.${paramName ? ' Look at the parameter ' + paramName + '.' : ''}`,
    CUSTOM_ERROR: ({message}) => `${message}`
};
for (let e in errorTemplates) {
    errorTemplates[e].code = e;
}
Object.assign(RouterError, errorTemplates);