@naturalcycles/backend-lib 3.5.0 → 4.0.0
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.
|
@@ -2,6 +2,11 @@ import { SentrySharedService } from '../sentry/sentry.shared.service';
|
|
|
2
2
|
import { BackendErrorRequestHandler, BackendRequest, BackendResponse } from './server.model';
|
|
3
3
|
export interface GenericErrorMiddlewareCfg {
|
|
4
4
|
sentryService?: SentrySharedService;
|
|
5
|
+
/**
|
|
6
|
+
* Defaults to false.
|
|
7
|
+
* So, by default, it will report ALL errors, not only 5xx.
|
|
8
|
+
*/
|
|
9
|
+
reportOnly5xx?: boolean;
|
|
5
10
|
}
|
|
6
11
|
/**
|
|
7
12
|
* Generic error handler.
|
|
@@ -7,6 +7,7 @@ const { APP_ENV } = process.env;
|
|
|
7
7
|
const includeErrorStack = APP_ENV !== 'prod' && APP_ENV !== 'test';
|
|
8
8
|
// Hacky way to store the sentryService, so it's available to `respondWithError` function
|
|
9
9
|
let sentryService;
|
|
10
|
+
let reportOnly5xx = false;
|
|
10
11
|
/**
|
|
11
12
|
* Generic error handler.
|
|
12
13
|
* Returns HTTP code based on err.data.httpStatusCode (default to 500).
|
|
@@ -14,6 +15,7 @@ let sentryService;
|
|
|
14
15
|
*/
|
|
15
16
|
function genericErrorMiddleware(cfg = {}) {
|
|
16
17
|
sentryService || (sentryService = cfg.sentryService);
|
|
18
|
+
reportOnly5xx = cfg.reportOnly5xx || false;
|
|
17
19
|
return (err, req, res, _next) => {
|
|
18
20
|
// if (res.headersSent) {
|
|
19
21
|
// Here we don't even log this error
|
|
@@ -30,9 +32,6 @@ function genericErrorMiddleware(cfg = {}) {
|
|
|
30
32
|
};
|
|
31
33
|
}
|
|
32
34
|
exports.genericErrorMiddleware = genericErrorMiddleware;
|
|
33
|
-
// export interface ResponseWithError extends Response {
|
|
34
|
-
// __err?: any
|
|
35
|
-
// }
|
|
36
35
|
function respondWithError(req, res, err) {
|
|
37
36
|
var _a, _b;
|
|
38
37
|
const { headersSent } = res;
|
|
@@ -73,5 +72,8 @@ function shouldReportToSentry(err) {
|
|
|
73
72
|
if (e.data.report === false)
|
|
74
73
|
return false;
|
|
75
74
|
// Report if http 5xx, otherwise not
|
|
76
|
-
|
|
75
|
+
// If no httpCode - report
|
|
76
|
+
// if httpCode >= 500 - report
|
|
77
|
+
// Otherwise - report, unless !reportOnly5xx is set
|
|
78
|
+
return !reportOnly5xx || !e.data.httpStatusCode || e.data.httpStatusCode >= 500;
|
|
77
79
|
}
|
package/package.json
CHANGED
|
@@ -12,6 +12,12 @@ import { BackendErrorRequestHandler, BackendRequest, BackendResponse } from './s
|
|
|
12
12
|
|
|
13
13
|
export interface GenericErrorMiddlewareCfg {
|
|
14
14
|
sentryService?: SentrySharedService
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Defaults to false.
|
|
18
|
+
* So, by default, it will report ALL errors, not only 5xx.
|
|
19
|
+
*/
|
|
20
|
+
reportOnly5xx?: boolean
|
|
15
21
|
}
|
|
16
22
|
|
|
17
23
|
const { APP_ENV } = process.env
|
|
@@ -19,6 +25,7 @@ const includeErrorStack = APP_ENV !== 'prod' && APP_ENV !== 'test'
|
|
|
19
25
|
|
|
20
26
|
// Hacky way to store the sentryService, so it's available to `respondWithError` function
|
|
21
27
|
let sentryService: SentrySharedService | undefined
|
|
28
|
+
let reportOnly5xx = false
|
|
22
29
|
|
|
23
30
|
/**
|
|
24
31
|
* Generic error handler.
|
|
@@ -29,6 +36,7 @@ export function genericErrorMiddleware(
|
|
|
29
36
|
cfg: GenericErrorMiddlewareCfg = {},
|
|
30
37
|
): BackendErrorRequestHandler {
|
|
31
38
|
sentryService ||= cfg.sentryService
|
|
39
|
+
reportOnly5xx = cfg.reportOnly5xx || false
|
|
32
40
|
|
|
33
41
|
return (err, req, res, _next) => {
|
|
34
42
|
// if (res.headersSent) {
|
|
@@ -47,9 +55,6 @@ export function genericErrorMiddleware(
|
|
|
47
55
|
}
|
|
48
56
|
}
|
|
49
57
|
|
|
50
|
-
// export interface ResponseWithError extends Response {
|
|
51
|
-
// __err?: any
|
|
52
|
-
// }
|
|
53
58
|
export function respondWithError(req: BackendRequest, res: BackendResponse, err: any): void {
|
|
54
59
|
const { headersSent } = res
|
|
55
60
|
|
|
@@ -95,5 +100,8 @@ function shouldReportToSentry(err: Error): boolean {
|
|
|
95
100
|
if (e.data.report === false) return false
|
|
96
101
|
|
|
97
102
|
// Report if http 5xx, otherwise not
|
|
98
|
-
|
|
103
|
+
// If no httpCode - report
|
|
104
|
+
// if httpCode >= 500 - report
|
|
105
|
+
// Otherwise - report, unless !reportOnly5xx is set
|
|
106
|
+
return !reportOnly5xx || !e.data.httpStatusCode || e.data.httpStatusCode >= 500
|
|
99
107
|
}
|