UNPKG

2.47 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.ApplicationError = void 0;
19var 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//# sourceMappingURL=application-error.js.map
\No newline at end of file