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 | NoLogFileName: 20,
|
24 | LogDoesNotExist: 21,
|
25 | IncompleteChangesInLogFile: 22,
|
26 | SpoolChangesError: 30,
|
27 | HTTPFatalError: 40,
|
28 | BulkGetError: 50
|
29 | };
|
30 |
|
31 | class BackupError extends Error {
|
32 | constructor(name, message) {
|
33 | super(message);
|
34 | this.name = name;
|
35 | }
|
36 | }
|
37 |
|
38 | class HTTPError extends BackupError {
|
39 | constructor(responseError, name) {
|
40 | var errMsg = `${responseError.statusCode} ${responseError.statusMessage || ''}: ` +
|
41 | `${responseError.request.method} ${(typeof responseError.request.uri === 'object') ? responseError.request.uri.href : responseError.request.uri}`;
|
42 | if (responseError.error && responseError.reason) {
|
43 | errMsg += ` - Error: ${responseError.error}, Reason: ${responseError.reason}`;
|
44 | }
|
45 |
|
46 | switch (responseError.statusCode) {
|
47 | case 401:
|
48 | name = 'Unauthorized';
|
49 | break;
|
50 | case 403:
|
51 | name = 'Forbidden';
|
52 | break;
|
53 | default:
|
54 | name = name || 'HTTPFatalError';
|
55 | }
|
56 | super(name, errMsg);
|
57 | }
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function checkResponse(err) {
|
65 | if (err) {
|
66 |
|
67 |
|
68 | if (err.statusCode >= 400) {
|
69 | return new HTTPError(err);
|
70 | } else {
|
71 |
|
72 | return augmentMessage(err);
|
73 | }
|
74 | }
|
75 | }
|
76 |
|
77 | function convertResponseError(responseError, errorFactory) {
|
78 | if (!errorFactory) {
|
79 | errorFactory = checkResponse;
|
80 | }
|
81 | return errorFactory(responseError);
|
82 | }
|
83 |
|
84 | function augmentMessage(err) {
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | if (err && err.code) {
|
90 | err.message = `${err.message} ${err.code}`;
|
91 | }
|
92 | if (err && err.description) {
|
93 | err.message = `${err.message} ${err.description}`;
|
94 | }
|
95 | return err;
|
96 | }
|
97 |
|
98 | module.exports = {
|
99 | BackupError: BackupError,
|
100 | HTTPError: HTTPError,
|
101 | convertResponseError: convertResponseError,
|
102 | terminationCallback: function terminationCallback(err, data) {
|
103 | if (err) {
|
104 | process.on('uncaughtException', function(err) {
|
105 | console.error(`ERROR: ${err.message}`);
|
106 | process.exitCode = codes[err.name] || 1;
|
107 | });
|
108 | throw err;
|
109 | }
|
110 | }
|
111 | };
|