summaryrefslogtreecommitdiffstats
path: root/lib/utils/error.js
blob: 4ccf83d0b26152d1b990301612d0df9278193440 (plain)
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
var _ = require('lodash');
var TypedError = require('error/typed');
var WrappedError = require('error/wrapped');

// Enforce as an Error object, and cleanup message
function enforce(err) {
    if (_.isString(err)) err = new Error(err);
    err.message = err.message.replace(/^Error: /, '');

    return err;
}

// Random error wrappers during parsing/generation
var ParsingError = WrappedError({
    message: 'Parsing Error: {origMessage}',
    type: 'server.parsing-failed'
});
var GenerationError = WrappedError({
    message: 'Generation Error: {origMessage}',
    type: 'server.parsing-failed'
});

// Error when output generator does not exists
var GeneratorNotFoundError = TypedError({
    type: 'server.404',
    message: 'Generator "{generator}" does not exists',
    generator: null
});

// A file does not exists
var FileNotFoundError = TypedError({
    type: 'server.404',
    message: 'No "{filename}" file (or is ignored)',
    filename: null
});

// A file is outside the scope
var FileOutOfScopeError = TypedError({
    type: 'server.404',
    message: '"{filename}" not in "{root}"',
    filename: null,
    root: null,
    code: 'EACCESS'
});

// Error for nunjucks templates
var TemplateError = WrappedError({
    message: 'Error compiling template "{filename}": {origMessage}',
    type: 'client.template-failed',
    filename: null
});

module.exports = {
    enforce: enforce,

    ParsingError: ParsingError,
    GenerationError: GenerationError,

    FileNotFoundError: FileNotFoundError,
    FileOutOfScopeError: FileOutOfScopeError,

    GeneratorNotFoundError: GeneratorNotFoundError,
    TemplateError: TemplateError
};