@naturalcycles/backend-lib 9.0.9 → 9.2.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.
|
@@ -81,7 +81,7 @@ export async function createDefaultApp(cfg) {
|
|
|
81
81
|
// Generic error handler
|
|
82
82
|
// It handles errors, returns proper status, does sentry.captureException(),
|
|
83
83
|
// assigns err.data.errorId from sentry
|
|
84
|
-
app.use(genericErrorMiddleware({ sentryService, ...cfg.genericErrorMwCfg }));
|
|
84
|
+
app.use(genericErrorMiddleware({ errorReportingService: sentryService, ...cfg.genericErrorMwCfg }));
|
|
85
85
|
return app;
|
|
86
86
|
}
|
|
87
87
|
function useHandlers(app, handlers = []) {
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { ErrorObject } from '@naturalcycles/js-lib';
|
|
2
|
-
import type { SentrySharedService } from '../sentry/sentry.shared.service.js';
|
|
3
2
|
import type { BackendErrorRequestHandler, BackendRequest, BackendResponse } from './server.model.js';
|
|
4
3
|
export interface GenericErrorMiddlewareCfg {
|
|
5
|
-
|
|
4
|
+
errorReportingService?: ErrorReportingService;
|
|
6
5
|
/**
|
|
7
6
|
* Defaults to false.
|
|
8
7
|
* So, by default, it will report ALL errors, not only 5xx.
|
|
@@ -14,6 +13,18 @@ export interface GenericErrorMiddlewareCfg {
|
|
|
14
13
|
*/
|
|
15
14
|
formatError?: (err: ErrorObject) => void;
|
|
16
15
|
}
|
|
16
|
+
export interface ErrorReportingService {
|
|
17
|
+
/**
|
|
18
|
+
* Call to report an error.
|
|
19
|
+
*
|
|
20
|
+
* It returns an ID for the error (which may be used to reference it later),
|
|
21
|
+
* and may return undefined if the error is not reported.
|
|
22
|
+
* Which may happen if the error is not considered reportable,
|
|
23
|
+
* or if an error reporting rate is configured in the service.
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
captureException: (err: any) => string | undefined;
|
|
27
|
+
}
|
|
17
28
|
/**
|
|
18
29
|
* Generic error handler.
|
|
19
30
|
* Returns HTTP code based on err.data.backendResponseStatusCode (default to 500).
|
|
@@ -2,7 +2,7 @@ import { _anyToError, _errorLikeToErrorObject, _filterUndefinedValues } from '@n
|
|
|
2
2
|
const { APP_ENV } = process.env;
|
|
3
3
|
const includeErrorStack = APP_ENV === 'dev';
|
|
4
4
|
// Hacky way to store the sentryService, so it's available to `respondWithError` function
|
|
5
|
-
let
|
|
5
|
+
let errorService;
|
|
6
6
|
let reportOnly5xx = false;
|
|
7
7
|
let formatError;
|
|
8
8
|
/**
|
|
@@ -11,7 +11,7 @@ let formatError;
|
|
|
11
11
|
* Sends json payload as ErrorResponse, transformed via errorSharedUtil.
|
|
12
12
|
*/
|
|
13
13
|
export function genericErrorMiddleware(cfg = {}) {
|
|
14
|
-
|
|
14
|
+
errorService ||= cfg.errorReportingService;
|
|
15
15
|
reportOnly5xx = cfg.reportOnly5xx || false;
|
|
16
16
|
formatError = cfg.formatError;
|
|
17
17
|
return (err, req, res, _next) => {
|
|
@@ -39,8 +39,9 @@ export function respondWithError(req, res, err) {
|
|
|
39
39
|
}
|
|
40
40
|
const originalError = _anyToError(err);
|
|
41
41
|
let errorId;
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const shouldReport = errorService && shouldReportToSentry(originalError);
|
|
43
|
+
if (shouldReport) {
|
|
44
|
+
errorId = errorService?.captureException(originalError);
|
|
44
45
|
}
|
|
45
46
|
if (headersSent)
|
|
46
47
|
return;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/backend-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "9.0
|
|
4
|
+
"version": "9.2.0",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"@sentry/node": "^9"
|
|
7
7
|
},
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"@naturalcycles/db-lib": "^10",
|
|
10
10
|
"@naturalcycles/js-lib": "^15",
|
|
11
11
|
"@naturalcycles/nodejs-lib": "^14",
|
|
12
|
+
"@types/body-parser": "^1",
|
|
12
13
|
"@types/cookie-parser": "^1",
|
|
13
14
|
"@types/cors": "^2",
|
|
14
15
|
"@types/express": "^5",
|
|
@@ -21,7 +22,8 @@
|
|
|
21
22
|
"firebase-admin": "^13",
|
|
22
23
|
"helmet": "^8",
|
|
23
24
|
"on-finished": "^2",
|
|
24
|
-
"simple-git": "^3"
|
|
25
|
+
"simple-git": "^3",
|
|
26
|
+
"tslib": "^2"
|
|
25
27
|
},
|
|
26
28
|
"devDependencies": {
|
|
27
29
|
"@naturalcycles/dev-lib": "*",
|
|
@@ -66,10 +68,9 @@
|
|
|
66
68
|
"test": "dev-lib test",
|
|
67
69
|
"lint": "dev-lib lint",
|
|
68
70
|
"bt": "dev-lib bt",
|
|
69
|
-
"
|
|
71
|
+
"clean": "dev-lib clean",
|
|
72
|
+
"typecheck": "dev-lib typecheck",
|
|
70
73
|
"dev": "APP_ENV=dev tsx watch src/test/server/server.ts",
|
|
71
|
-
"docs-serve": "NODE_OPTIONS=--openssl-legacy-provider vuepress dev docs",
|
|
72
|
-
"docs-build": "NODE_OPTIONS=--openssl-legacy-provider vuepress build docs",
|
|
73
74
|
"deploy-gae": "tsx src/bin/deploy-gae.ts",
|
|
74
75
|
"deploy-prepare": "tsx src/bin/deploy-prepare.ts",
|
|
75
76
|
"deploy-prepare-debug": "AA=AA1 BB=BB1 tsx src/bin/deploy-prepare.ts --projectDir ./src/test/project",
|
|
@@ -121,7 +121,9 @@ export async function createDefaultApp(cfg: DefaultAppCfg): Promise<BackendAppli
|
|
|
121
121
|
// Generic error handler
|
|
122
122
|
// It handles errors, returns proper status, does sentry.captureException(),
|
|
123
123
|
// assigns err.data.errorId from sentry
|
|
124
|
-
app.use(
|
|
124
|
+
app.use(
|
|
125
|
+
genericErrorMiddleware({ errorReportingService: sentryService, ...cfg.genericErrorMwCfg }),
|
|
126
|
+
)
|
|
125
127
|
|
|
126
128
|
return app
|
|
127
129
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import type { AppError, BackendErrorResponseObject, ErrorObject } from '@naturalcycles/js-lib'
|
|
2
2
|
import { _anyToError, _errorLikeToErrorObject, _filterUndefinedValues } from '@naturalcycles/js-lib'
|
|
3
|
-
import type { SentrySharedService } from '../sentry/sentry.shared.service.js'
|
|
4
3
|
import type { BackendErrorRequestHandler, BackendRequest, BackendResponse } from './server.model.js'
|
|
5
4
|
|
|
6
5
|
export interface GenericErrorMiddlewareCfg {
|
|
7
|
-
|
|
6
|
+
errorReportingService?: ErrorReportingService
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Defaults to false.
|
|
@@ -19,11 +18,24 @@ export interface GenericErrorMiddlewareCfg {
|
|
|
19
18
|
formatError?: (err: ErrorObject) => void
|
|
20
19
|
}
|
|
21
20
|
|
|
21
|
+
export interface ErrorReportingService {
|
|
22
|
+
/**
|
|
23
|
+
* Call to report an error.
|
|
24
|
+
*
|
|
25
|
+
* It returns an ID for the error (which may be used to reference it later),
|
|
26
|
+
* and may return undefined if the error is not reported.
|
|
27
|
+
* Which may happen if the error is not considered reportable,
|
|
28
|
+
* or if an error reporting rate is configured in the service.
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
captureException: (err: any) => string | undefined
|
|
32
|
+
}
|
|
33
|
+
|
|
22
34
|
const { APP_ENV } = process.env
|
|
23
35
|
const includeErrorStack = APP_ENV === 'dev'
|
|
24
36
|
|
|
25
37
|
// Hacky way to store the sentryService, so it's available to `respondWithError` function
|
|
26
|
-
let
|
|
38
|
+
let errorService: ErrorReportingService | undefined
|
|
27
39
|
let reportOnly5xx = false
|
|
28
40
|
let formatError: GenericErrorMiddlewareCfg['formatError']
|
|
29
41
|
|
|
@@ -35,7 +47,7 @@ let formatError: GenericErrorMiddlewareCfg['formatError']
|
|
|
35
47
|
export function genericErrorMiddleware(
|
|
36
48
|
cfg: GenericErrorMiddlewareCfg = {},
|
|
37
49
|
): BackendErrorRequestHandler {
|
|
38
|
-
|
|
50
|
+
errorService ||= cfg.errorReportingService
|
|
39
51
|
reportOnly5xx = cfg.reportOnly5xx || false
|
|
40
52
|
formatError = cfg.formatError
|
|
41
53
|
|
|
@@ -69,8 +81,9 @@ export function respondWithError(req: BackendRequest, res: BackendResponse, err:
|
|
|
69
81
|
|
|
70
82
|
let errorId: string | undefined
|
|
71
83
|
|
|
72
|
-
|
|
73
|
-
|
|
84
|
+
const shouldReport = errorService && shouldReportToSentry(originalError)
|
|
85
|
+
if (shouldReport) {
|
|
86
|
+
errorId = errorService?.captureException(originalError)
|
|
74
87
|
}
|
|
75
88
|
|
|
76
89
|
if (headersSent) return
|