1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const typeTools = require('./type-tools.js');
|
7 | const fileTools = require('./file-tools');
|
8 | const validTools = require('./validation-tools');
|
9 | const transformTools = require('./transform-tools');
|
10 | const iteratorTools = require('./iterator-tools');
|
11 | const serviceTools = require('./service-tools');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const paramRequired = (param)=> {
|
20 | if (validTools.blank(param)) {
|
21 | throwError({
|
22 | id: 'ERROR/E-NOT-BLANK',
|
23 | value: { path },
|
24 | });
|
25 | }
|
26 | };
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | const type = (variable, expected)=> {
|
33 | let actual = typeTools.typeDetect(variable);
|
34 | if (actual !== expected) {
|
35 | throwError({
|
36 | id: 'ERROR/E-DIR-NOT-EXIST',
|
37 | value: {
|
38 | variable,
|
39 | expected,
|
40 | actual, },
|
41 | });
|
42 | }
|
43 | };
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const directoryNotExist = (path)=> {
|
49 | paramRequired(path);
|
50 | if (fileTools.directoryExist(path) === false) {
|
51 | throwError({
|
52 | id: 'ERROR/E-DIR-NOT-EXIST',
|
53 | value: { path },
|
54 | });
|
55 | }
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | const fileNotExist = (path)=> {
|
63 | paramRequired(path, 'path');
|
64 | if (fileTools.fileExist(path) === false) {
|
65 | throwError({
|
66 | id: 'ERROR/E-FILE-NOT-EXIST',
|
67 | value: { path },
|
68 | });
|
69 | }
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | const validateJsonSchema = (msg)=> {
|
77 |
|
78 | validTools.validateJsonSchema(msg, function (errors) {
|
79 | if (errors) {
|
80 | let messages = [];
|
81 | iteratorTools.iterator(errors, (item)=> {
|
82 |
|
83 | let id = `VALIDATE/E-${item.keyword.toUpperCase()}`;
|
84 | let params = item.params;
|
85 | messages.push({ id, value: params });
|
86 |
|
87 | });
|
88 | throwError({ id: 'VALIDATE', value: messages });
|
89 | }
|
90 |
|
91 | });
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | const throwError = (errorObject) => {
|
101 | throw getError(errorObject);
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | const throwParseFileError = (path) => (
|
109 | throwError({ id: 'ERROR/E-MISS-PARSE-FILE', value: {
|
110 | path,
|
111 | }, })
|
112 | );
|
113 |
|
114 | const getError = (errorObject) => {
|
115 |
|
116 | let error = new Error(errorObject.id);
|
117 | error.mkMessage = errorObject;
|
118 |
|
119 | error.toString = function () {
|
120 | let result = `${this.mkMessage.id}:`;
|
121 | if (typeTools.isArray(this.mkMessage.value)) {
|
122 | this.mkMessage.value.map(item=> {
|
123 | result += `${item.id}: ${item.value ? `${JSON.stringify(item.value)}` : ''}
|
124 | ${item.options ? `options:${item.options}` : ''}
|
125 | `;
|
126 | });
|
127 | } else {
|
128 | result += ` ${this.mkMessage.value ? `${JSON.stringify(this.mkMessage.value)}` : ''}
|
129 | ${this.mkMessage.options ? `options:${this.mkMessage.options}` : ''} `;
|
130 | }
|
131 |
|
132 | return result;
|
133 | };
|
134 |
|
135 | return error;
|
136 | };
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | const errorHandler = (error, callback) => {
|
145 | let messageContent;
|
146 | let isTrustedError = false;
|
147 |
|
148 | if (error.mkMessage) {
|
149 | error.mkMessage.errorLevel = error.mkMessage.errorLevel || 'warning';
|
150 | if (error.mkMessage.id === 'I18N.MISS-SYSTEM')
|
151 | messageContent = `${error.mkMessage.id}: miss system error message.`;
|
152 | else
|
153 | messageContent = error.toString();
|
154 | try {
|
155 | if (!error.mkMessage.stack) {
|
156 | error.mkMessage.stack = error.stack;
|
157 | }
|
158 |
|
159 | serviceTools.standClient('mocoolka-log', 'rfc5424',
|
160 | { level: error.mkMessage.errorLevel, message: error.mkMessage,
|
161 | }, (error, data)=> {
|
162 | if (error)
|
163 | console.error(error);
|
164 | });
|
165 | }
|
166 | catch (ex) {
|
167 | console.error(ex);
|
168 | }
|
169 |
|
170 | isTrustedError = errorTools.trustedErrors.includes(error.mkMessage.errorLevel);
|
171 | }
|
172 |
|
173 | if (callback)
|
174 | callback(messageContent);
|
175 | return isTrustedError;
|
176 | };
|
177 |
|
178 | const errorTools = {
|
179 | trustedErrors: ['error', 'warning'],
|
180 | paramRequired,
|
181 | directoryNotExist,
|
182 | type,
|
183 | fileNotExist,
|
184 | throwError,
|
185 | throwParseFileError,
|
186 | errorHandler,
|
187 | validateJsonSchema,
|
188 |
|
189 | };
|
190 |
|
191 | module.exports = errorTools;
|