UNPKG

2.21 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.NestedError = exports.executeFinally = undefined;
7
8var _bluebirdLst;
9
10function _load_bluebirdLst() {
11 return _bluebirdLst = require("bluebird-lst");
12}
13
14// you don't need to handle error in your task - it is passed only indicate status of promise
15let executeFinally = exports.executeFinally = (() => {
16 var _ref = (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* (promise, task) {
17 let result = null;
18 try {
19 result = yield promise;
20 } catch (originalError) {
21 try {
22 yield task(true);
23 } catch (taskError) {
24 throw new NestedError([originalError, taskError]);
25 }
26 throw originalError;
27 }
28 try {
29 yield task(false);
30 } catch (taskError) {
31 throw taskError;
32 }
33 return result;
34 });
35
36 return function executeFinally(_x, _x2) {
37 return _ref.apply(this, arguments);
38 };
39})();
40
41exports.printErrorAndExit = printErrorAndExit;
42exports.orNullIfFileNotExist = orNullIfFileNotExist;
43exports.orIfFileNotExist = orIfFileNotExist;
44
45var _chalk;
46
47function _load_chalk() {
48 return _chalk = require("chalk");
49}
50
51function printErrorAndExit(error) {
52 console.error((0, (_chalk || _load_chalk()).red)((error.stack || error).toString()));
53 process.exit(-1);
54}class NestedError extends Error {
55 constructor(errors) {
56 let message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Compound error: ";
57
58 let m = message;
59 let i = 1;
60 for (const error of errors) {
61 const prefix = `Error #${i++} `;
62 m += "\n\n" + prefix + "-".repeat(80) + "\n" + error.stack;
63 }
64 super(m);
65 }
66}
67exports.NestedError = NestedError;
68function orNullIfFileNotExist(promise) {
69 return orIfFileNotExist(promise, null);
70}
71function orIfFileNotExist(promise, fallbackValue) {
72 return promise.catch(e => {
73 if (e.code === "ENOENT" || e.code === "ENOTDIR") {
74 return fallbackValue;
75 }
76 throw e;
77 });
78}
79//# sourceMappingURL=promise.js.map
\No newline at end of file