1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 |
|
17 | const codes = {
|
18 | Error: 1,
|
19 | InvalidOption: 2,
|
20 | DatabaseNotFound: 10,
|
21 | Unauthorized: 11,
|
22 | Forbidden: 12,
|
23 | DatabaseNotEmpty: 13,
|
24 | NoLogFileName: 20,
|
25 | LogDoesNotExist: 21,
|
26 | IncompleteChangesInLogFile: 22,
|
27 | SpoolChangesError: 30,
|
28 | HTTPFatalError: 40,
|
29 | BulkGetError: 50
|
30 | };
|
31 |
|
32 | class BackupError extends Error {
|
33 | constructor(name, message) {
|
34 | super(message);
|
35 | this.name = name;
|
36 | }
|
37 | }
|
38 |
|
39 | class HTTPError extends BackupError {
|
40 | constructor(responseError, name) {
|
41 |
|
42 | switch (responseError.status) {
|
43 | case 401:
|
44 | name = 'Unauthorized';
|
45 | break;
|
46 | case 403:
|
47 | name = 'Forbidden';
|
48 | break;
|
49 | default:
|
50 | name = name || 'HTTPFatalError';
|
51 | }
|
52 | super(name, responseError.message);
|
53 | }
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function checkResponse(err) {
|
61 | if (err) {
|
62 |
|
63 |
|
64 | if (err.status >= 400) {
|
65 | return new HTTPError(err);
|
66 | } else {
|
67 |
|
68 | return augmentMessage(err);
|
69 | }
|
70 | }
|
71 | }
|
72 |
|
73 | function convertResponseError(responseError, errorFactory) {
|
74 | if (!errorFactory) {
|
75 | errorFactory = checkResponse;
|
76 | }
|
77 | return errorFactory(responseError);
|
78 | }
|
79 |
|
80 | function augmentMessage(err) {
|
81 |
|
82 |
|
83 |
|
84 | if (err && err.statusText) {
|
85 | err.message = `${err.message} ${err.statusText}`;
|
86 | }
|
87 | if (err && err.description) {
|
88 | err.message = `${err.message} ${err.description}`;
|
89 | }
|
90 | return err;
|
91 | }
|
92 |
|
93 | module.exports = {
|
94 | BackupError: BackupError,
|
95 | HTTPError: HTTPError,
|
96 | convertResponseError: convertResponseError,
|
97 | terminationCallback: function terminationCallback(err, data) {
|
98 | if (err) {
|
99 | console.error(`ERROR: ${err.message}`);
|
100 | process.exitCode = codes[err.name] || 1;
|
101 | process.exit();
|
102 | }
|
103 | }
|
104 | };
|