1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.ApplicationError = void 0;
|
19 | var ApplicationError;
|
20 | (function (ApplicationError) {
|
21 | const codes = new Set();
|
22 | function declare(code, factory) {
|
23 | if (codes.has(code)) {
|
24 | throw new Error(`An application error for '${code}' code is already declared`);
|
25 | }
|
26 | codes.add(code);
|
27 | const constructorOpt = Object.assign((...args) => new Impl(code, factory(...args), constructorOpt), {
|
28 | code,
|
29 | is(arg) {
|
30 | return arg instanceof Impl && arg.code === code;
|
31 | }
|
32 | });
|
33 | return constructorOpt;
|
34 | }
|
35 | ApplicationError.declare = declare;
|
36 | function is(arg) {
|
37 | return arg instanceof Impl;
|
38 | }
|
39 | ApplicationError.is = is;
|
40 | function fromJson(code, raw) {
|
41 | return new Impl(code, raw);
|
42 | }
|
43 | ApplicationError.fromJson = fromJson;
|
44 | class Impl extends Error {
|
45 | constructor(code, raw, constructorOpt) {
|
46 | super(raw.message);
|
47 | this.code = code;
|
48 | this.data = raw.data;
|
49 | Object.setPrototypeOf(this, Impl.prototype);
|
50 | if (raw.stack) {
|
51 | this.stack = raw.stack;
|
52 | }
|
53 | else if (Error.captureStackTrace && constructorOpt) {
|
54 | Error.captureStackTrace(this, constructorOpt);
|
55 | }
|
56 | }
|
57 | toJson() {
|
58 | const { message, data, stack } = this;
|
59 | return { message, data, stack };
|
60 | }
|
61 | }
|
62 | })(ApplicationError = exports.ApplicationError || (exports.ApplicationError = {}));
|
63 |
|
\ | No newline at end of file |