@cley_faye/boilerplate 3.16.0 → 3.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -32,15 +32,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
32
32
|
const express_1 = __importDefault(require("express"));
|
|
33
33
|
const winston_js_1 = require("../winston.js");
|
|
34
34
|
const logger_js_1 = require("./logger.js");
|
|
35
|
+
const unprocessableentityerror_js_1 = require("./unprocessableentityerror.js");
|
|
36
|
+
const getErrorData = (error) => {
|
|
37
|
+
const unprocessable = (0, unprocessableentityerror_js_1.get422Fields)(error);
|
|
38
|
+
if (unprocessable) {
|
|
39
|
+
return {
|
|
40
|
+
message: unprocessable.message,
|
|
41
|
+
extraFields: { fields: unprocessable.fields, errors: unprocessable.errors },
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return error.expose ? { message: error.message } : {};
|
|
45
|
+
};
|
|
35
46
|
const defaultErrorHandler = (errorHandlerConfig) => (err, req, res, next) => {
|
|
36
47
|
const noErrorFilter = typeof errorHandlerConfig === "boolean"
|
|
37
48
|
? false
|
|
38
49
|
: Boolean(errorHandlerConfig.noErrorFilter);
|
|
39
50
|
if (!res.headersSent) {
|
|
40
51
|
if (err.statusCode) {
|
|
41
|
-
const
|
|
42
|
-
? err.message
|
|
43
|
-
: undefined;
|
|
52
|
+
const errorData = getErrorData(err);
|
|
44
53
|
const accepted = req.accepts([
|
|
45
54
|
"text",
|
|
46
55
|
"html",
|
|
@@ -49,12 +58,13 @@ const defaultErrorHandler = (errorHandlerConfig) => (err, req, res, next) => {
|
|
|
49
58
|
if (accepted === "json") {
|
|
50
59
|
res.status(err.statusCode).send({
|
|
51
60
|
statusCode: err.statusCode,
|
|
52
|
-
message,
|
|
61
|
+
message: errorData.message,
|
|
62
|
+
...errorData.extraFields,
|
|
53
63
|
});
|
|
54
64
|
return;
|
|
55
65
|
}
|
|
56
|
-
if (message) {
|
|
57
|
-
res.status(err.statusCode).send(message);
|
|
66
|
+
if (errorData.message) {
|
|
67
|
+
res.status(err.statusCode).send(errorData.message);
|
|
58
68
|
}
|
|
59
69
|
else {
|
|
60
70
|
res.sendStatus(err.statusCode);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
* MIT License
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2021 Gabriel Paul "Cley Faye" Risterucci
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
"use strict";
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.get422Fields = void 0;
|
|
30
|
+
const UNPROCESSABLE_ENTITY = 422;
|
|
31
|
+
const get422Fields = (error) => {
|
|
32
|
+
const { statusCode, cause, fields } = error;
|
|
33
|
+
if (statusCode !== UNPROCESSABLE_ENTITY
|
|
34
|
+
|| !cause
|
|
35
|
+
|| !Array.isArray(cause)
|
|
36
|
+
|| !fields
|
|
37
|
+
|| !Array.isArray(fields)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
let fieldsStr;
|
|
41
|
+
try {
|
|
42
|
+
fieldsStr = JSON.stringify(fields);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
fieldsStr = "<error>";
|
|
46
|
+
}
|
|
47
|
+
let errorsStr;
|
|
48
|
+
try {
|
|
49
|
+
errorsStr = JSON.stringify(cause);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
errorsStr = "<error>";
|
|
53
|
+
}
|
|
54
|
+
const message = `${error.message}: fields=${fieldsStr}, errors=${errorsStr}`;
|
|
55
|
+
return { message, fields, errors: cause };
|
|
56
|
+
};
|
|
57
|
+
exports.get422Fields = get422Fields;
|
package/lib/winston.d.ts
CHANGED
|
@@ -23,9 +23,11 @@ interface ExtendedErrorFields {
|
|
|
23
23
|
request?: {
|
|
24
24
|
_currentUrl?: string;
|
|
25
25
|
};
|
|
26
|
+
fields?: Set<string>;
|
|
26
27
|
statusCode?: number;
|
|
28
|
+
expose?: boolean;
|
|
27
29
|
}
|
|
28
|
-
type ExtendedError = Error & ExtendedErrorFields;
|
|
30
|
+
export type ExtendedError = Error & ExtendedErrorFields;
|
|
29
31
|
/** Return a string with the full error and stacktrace, including causes */
|
|
30
32
|
export declare const filterError: (error: ExtendedError, collapseStacktrace?: boolean, extraFilter?: Array<RegExp> | RegExp) => string;
|
|
31
33
|
export declare const customFormat: (timestamp?: boolean, collapseStacktrace?: boolean, stacktraceFilter?: Array<RegExp> | RegExp) => Format;
|
package/lib/winston.js
CHANGED
|
@@ -31,6 +31,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
32
|
exports.consoleLogger = exports.transports = exports.setConfig = exports.customFormat = exports.filterError = exports.prefixOutput = exports.filterStacktrace = void 0;
|
|
33
33
|
const winston_1 = __importDefault(require("winston"));
|
|
34
|
+
const unprocessableentityerror_js_1 = require("./express/unprocessableentityerror.js");
|
|
34
35
|
const LOG_LEVEL_PADDING_SIZE = 10;
|
|
35
36
|
const stackFilterRegex = /(?:at process|at runMicrotasks)/u;
|
|
36
37
|
const isFiltered = (line, extraFilter) => {
|
|
@@ -150,9 +151,20 @@ const getCause = (error) => {
|
|
|
150
151
|
return new Error("Unknown cause");
|
|
151
152
|
}
|
|
152
153
|
};
|
|
154
|
+
/** Handle 422
|
|
155
|
+
*
|
|
156
|
+
* If return a string, do not process the error normally.
|
|
157
|
+
*/
|
|
158
|
+
const process422Error = (error) => {
|
|
159
|
+
const errorInfo = (0, unprocessableentityerror_js_1.get422Fields)(error);
|
|
160
|
+
return errorInfo?.message;
|
|
161
|
+
};
|
|
153
162
|
/** Return a string with the full error and stacktrace, including causes */
|
|
154
163
|
const filterError = (error, collapseStacktrace = true, extraFilter) => {
|
|
155
164
|
const resultRows = [];
|
|
165
|
+
const unprocessableCheck = process422Error(error);
|
|
166
|
+
if (unprocessableCheck)
|
|
167
|
+
return unprocessableCheck;
|
|
156
168
|
let cursor = error;
|
|
157
169
|
let indentLevel = 0;
|
|
158
170
|
while (cursor) {
|