UNPKG

4.99 kBJavaScriptView Raw
1// © Copyright Mocoolka Corporation 2015,2017.
2// Node module: mocoolka-tools
3// LICENSE: MIT
4
5/** @module mocoolka-tools-error */
6const typeTools = require('./type-tools.js');
7const fileTools = require('./file-tools');
8const validTools = require('./validation-tools');
9const transformTools = require('./transform-tools');
10const iteratorTools = require('./iterator-tools');
11const serviceTools = require('./service-tools');
12
13/**
14 * param must can't be blank
15 * @param {string|number|Array}param - param will be checked
16 * @param {Object} [options]
17 * @property {boolean}[options.plain] -true will direct output error or format with message
18 */
19const paramRequired = (param)=> {
20 if (validTools.blank(param)) {
21 throwError({
22 id: 'ERROR/E-NOT-BLANK',
23 value: { path },
24 });
25 }
26};
27/**
28 * check variable is the type ,throw error if no
29 * @param {Object} variable -check variable
30 * @param {string}expected - type name
31 */
32const 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 * check directory exist by path ,throw error if not exist
46 * @param {string} path -directory path
47 */
48const 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 * check file exist by path ,throw error if not exist
60 * @param {string} path -file path
61 */
62const 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 * validate json with schema,throw error if validate fail
74 * @param {Object} msg
75 */
76const validateJsonSchema = (msg)=> {
77
78 validTools.validateJsonSchema(msg, function (errors) {
79 if (errors) {
80 let messages = [];
81 iteratorTools.iterator(errors, (item)=> {
82 //console.log(errors);
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 * throw Error
96 * @param {Object}errorObject
97 * @property {string} errorObject.id - error id
98 * @property {Object} errorObject.value -param value in intl
99 */
100const throwError = (errorObject) => {
101 throw getError(errorObject);
102 };
103
104/**
105 * throw ERROR/E-MISS-PARSE-FILE error
106 * @param {string}path - file path
107 */
108const throwParseFileError = (path) => (
109 throwError({ id: 'ERROR/E-MISS-PARSE-FILE', value: {
110 path,
111 }, })
112);
113
114const 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 * handler Error
140 * @param {Object} error - Error instance
141 * @param {Function} [callback]
142 * @return {boolean} - true if the Error is Trusted; otherwise, false.
143 */
144const 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 // console.error(messageContent);
173 if (callback)
174 callback(messageContent);
175 return isTrustedError;
176};
177
178const errorTools = {
179 trustedErrors: ['error', 'warning'],
180 paramRequired,
181 directoryNotExist,
182 type,
183 fileNotExist,
184 throwError,
185 throwParseFileError,
186 errorHandler,
187 validateJsonSchema,
188
189};
190
191module.exports = errorTools;