promise.js
1.72 KB
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
});
exports.printErrorAndExit = printErrorAndExit;
exports.executeFinally = executeFinally;
exports.orNullIfFileNotExist = orNullIfFileNotExist;
exports.orIfFileNotExist = orIfFileNotExist;
exports.NestedError = void 0;
function _chalk() {
const data = _interopRequireDefault(require("chalk"));
_chalk = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function printErrorAndExit(error) {
console.error(_chalk().default.red((error.stack || error).toString()));
process.exit(1);
} // you don't need to handle error in your task - it is passed only indicate status of promise
async function executeFinally(promise, task) {
let result = null;
try {
result = await promise;
} catch (originalError) {
try {
await task(true);
} catch (taskError) {
throw new NestedError([originalError, taskError]);
}
throw originalError;
}
await task(false);
return result;
}
class NestedError extends Error {
constructor(errors, message = "Compound error: ") {
let m = message;
let i = 1;
for (const error of errors) {
const prefix = `Error #${i++} `;
m += "\n\n" + prefix + "-".repeat(80) + "\n" + error.stack;
}
super(m);
}
}
exports.NestedError = NestedError;
function orNullIfFileNotExist(promise) {
return orIfFileNotExist(promise, null);
}
function orIfFileNotExist(promise, fallbackValue) {
return promise.catch(e => {
if (e.code === "ENOENT" || e.code === "ENOTDIR") {
return fallbackValue;
}
throw e;
});
}
// __ts-babel@6.0.4
//# sourceMappingURL=promise.js.map