File size: 3,349 Bytes
e4a10af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
const verror_1 = require("verror");
const VError = require("verror");
function subinspect(obj, options) {
    options = Object.assign({}, options, { depth: options.depth == null ? null : options.depth - 1 });
    return util_1.inspect(obj, options);
}
class PrettyVError extends VError {
    static errorFromList(errors) {
        switch (errors.length) {
            case 0: return null;
            case 1: return errors[0];
            default: return new PrettyMultiError(errors);
        }
    }
    constructor(...params) {
        super(...params);
        this._ownStack = this.stack;
        Object.defineProperty(this, "stack", {
            get() {
                return this[util_1.inspect.custom]();
            },
            set(stack) {
                this._ownStack = stack;
            },
            configurable: true
        });
    }
    [util_1.inspect.custom](depth = util_1.inspect.defaultOptions.depth || 2, options = Object.assign({ stylize(s) { return s; } }, util_1.inspect.defaultOptions)) {
        if (depth < 0)
            return this.toString();
        const cause = this.cause();
        return `${this._ownStack}${cause ? `\ncaused by: ${subinspect(cause, options)}` : ""}`;
    }
}
exports.PrettyVError = PrettyVError;
PrettyVError.prototype.name = Error.prototype.name;
PrettyVError.prototype.toString = Error.prototype.toString;
class PrettyMultiError extends verror_1.MultiError {
    constructor(errors) {
        super(errors);
        this._ownStack = this.stack;
        Object.defineProperty(this, "stack", {
            get() {
                return this[util_1.inspect.custom]();
            },
            set(stack) {
                this._ownStack = stack;
            },
            configurable: true
        });
    }
    [util_1.inspect.custom](depth = util_1.inspect.defaultOptions.depth || 2, options = Object.assign({ stylize(s) { return s; } }, util_1.inspect.defaultOptions)) {
        if (depth < 0)
            return options.stylize(this.toString(), "special");
        const errors = this.errors();
        switch (errors.length) {
            case 0: return `${this.toString()} (empty)`;
            case 1: return util_1.inspect(errors[0]);
            default: return `${errors.length} errors:\n${errors.map((error, errorIndex) => {
                const isLastError = errorIndex + 1 === errors.length;
                const formattedError = util_1.inspect(error, Object.assign({}, options, { depth: options.depth == null ? null : options.depth - 1 }));
                let lines = formattedError.split(/\r\n|\r|\n/);
                const firstLinePrefix = isLastError ? "└" : "├";
                const restLinePrefix = isLastError ? " " : "│";
                lines = lines.map((line, lineIndex) => {
                    const isFirstLine = lineIndex === 0;
                    return `${isFirstLine ? firstLinePrefix : restLinePrefix} ${line}`;
                });
                return lines.join("\n");
            }).join("\n")}`;
        }
    }
}
exports.PrettyMultiError = PrettyMultiError;
PrettyMultiError.prototype.name = Error.prototype.name;
PrettyMultiError.prototype.toString = Error.prototype.toString;
//# sourceMappingURL=format-verror.js.map