@naturalcycles/backend-lib 4.18.1 → 4.18.3

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.
@@ -26,10 +26,10 @@ export declare class SentrySharedService {
26
26
  */
27
27
  setUserId(id: string): void;
28
28
  /**
29
- * Does console.error(err)
29
+ * Does console.log(err)
30
30
  * Returns "eventId" or undefined (if error was not reported).
31
31
  */
32
- captureException(err: any, logError?: boolean): string | undefined;
32
+ captureException(err_: any, logError?: boolean): string | undefined;
33
33
  /**
34
34
  * Returns "eventId"
35
35
  */
@@ -13,6 +13,9 @@ const sentrySeverityMap = {
13
13
  error: 'error',
14
14
  fatal: 'error',
15
15
  };
16
+ const INSPECT_OPT = {
17
+ colors: false,
18
+ };
16
19
  class SentrySharedService {
17
20
  constructor(sentryServiceCfg) {
18
21
  this.sentryServiceCfg = sentryServiceCfg;
@@ -63,20 +66,23 @@ class SentrySharedService {
63
66
  });
64
67
  }
65
68
  /**
66
- * Does console.error(err)
69
+ * Does console.log(err)
67
70
  * Returns "eventId" or undefined (if error was not reported).
68
71
  */
69
- captureException(err, logError = true) {
70
- // console.error(err)
72
+ captureException(err_, logError = true) {
73
+ // normalize the error
74
+ const err = (0, js_lib_1._anyToError)(err_);
75
+ const data = err instanceof js_lib_1.AppError ? err.data : undefined;
71
76
  // Using request-aware logger here
72
77
  if (logError) {
73
- (0, index_1.getRequestLogger)().error('captureException:', err);
78
+ // Log both the error and attached ErrorData (if any)
79
+ (0, index_1.getRequestLogger)().error('captureException:', ...[err_, data].filter(Boolean));
74
80
  }
75
- if (err?.data?.report === false) {
81
+ if (data?.report === false) {
76
82
  // Skip reporting the error
77
83
  return;
78
84
  }
79
- if (err?.data?.reportRate) {
85
+ if (data?.reportRate) {
80
86
  const reportRate = err.data.reportRate;
81
87
  // E.g rate of 0.1 means 10% of errors are reported
82
88
  if (Math.random() > reportRate)
@@ -86,12 +92,15 @@ class SentrySharedService {
86
92
  // It will log additional "breadcrumb object" before the error
87
93
  // It's a Breadcrumb, not a console.log, because console.log are NOT automatically attached as Breadcrumbs in cron-job environments (outside of Express)
88
94
  this.sentry().addBreadcrumb({
89
- message: (0, nodejs_lib_1.inspectAny)(err, {
90
- colors: false,
91
- }),
92
- // data: (err as AppError).data, // included in message
95
+ message: (0, nodejs_lib_1.inspectAny)(err, INSPECT_OPT),
93
96
  });
94
- return this.sentry().captureException((0, js_lib_1._anyToError)(err));
97
+ if (data) {
98
+ // Log the attached ErrorData (if any)
99
+ this.sentry().addBreadcrumb({
100
+ message: (0, nodejs_lib_1.inspectAny)(data, INSPECT_OPT),
101
+ });
102
+ }
103
+ return this.sentry().captureException(err);
95
104
  }
96
105
  /**
97
106
  * Returns "eventId"
@@ -115,11 +124,7 @@ class SentrySharedService {
115
124
  log: () => { },
116
125
  warn: () => { },
117
126
  error: (...args) => {
118
- const message = args
119
- .map(arg => (0, nodejs_lib_1.inspectAny)(arg, {
120
- colors: false,
121
- }))
122
- .join(' ');
127
+ const message = args.map(arg => (0, nodejs_lib_1.inspectAny)(arg, INSPECT_OPT)).join(' ');
123
128
  this.sentry().addBreadcrumb({
124
129
  message,
125
130
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "4.18.1",
3
+ "version": "4.18.3",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "serve": "APP_ENV=dev nodemon",
@@ -1,5 +1,12 @@
1
- import { _anyToError, _Memo, AppError, CommonLogger, CommonLogLevel } from '@naturalcycles/js-lib'
2
- import { inspectAny } from '@naturalcycles/nodejs-lib'
1
+ import {
2
+ _anyToError,
3
+ _Memo,
4
+ AppError,
5
+ CommonLogger,
6
+ CommonLogLevel,
7
+ ErrorData,
8
+ } from '@naturalcycles/js-lib'
9
+ import { inspectAny, InspectAnyOptions } from '@naturalcycles/nodejs-lib'
3
10
  import type { Breadcrumb, NodeOptions, SeverityLevel } from '@sentry/node'
4
11
  import type * as SentryLib from '@sentry/node'
5
12
  import { BackendErrorRequestHandler, BackendRequestHandler, getRequestLogger } from '../index'
@@ -15,6 +22,10 @@ const sentrySeverityMap: Record<SeverityLevel, CommonLogLevel> = {
15
22
  fatal: 'error',
16
23
  }
17
24
 
25
+ const INSPECT_OPT: InspectAnyOptions = {
26
+ colors: false,
27
+ }
28
+
18
29
  export class SentrySharedService {
19
30
  constructor(private sentryServiceCfg: SentrySharedServiceCfg) {}
20
31
 
@@ -73,22 +84,26 @@ export class SentrySharedService {
73
84
  }
74
85
 
75
86
  /**
76
- * Does console.error(err)
87
+ * Does console.log(err)
77
88
  * Returns "eventId" or undefined (if error was not reported).
78
89
  */
79
- captureException(err: any, logError = true): string | undefined {
80
- // console.error(err)
90
+ captureException(err_: any, logError = true): string | undefined {
91
+ // normalize the error
92
+ const err = _anyToError(err_)
93
+ const data = err instanceof AppError ? (err.data as ErrorData) : undefined
94
+
81
95
  // Using request-aware logger here
82
96
  if (logError) {
83
- getRequestLogger().error('captureException:', err)
97
+ // Log both the error and attached ErrorData (if any)
98
+ getRequestLogger().error('captureException:', ...[err_, data].filter(Boolean))
84
99
  }
85
100
 
86
- if (err?.data?.report === false) {
101
+ if (data?.report === false) {
87
102
  // Skip reporting the error
88
103
  return
89
104
  }
90
105
 
91
- if (err?.data?.reportRate) {
106
+ if (data?.reportRate) {
92
107
  const reportRate = (err as AppError).data.reportRate!
93
108
  // E.g rate of 0.1 means 10% of errors are reported
94
109
  if (Math.random() > reportRate) return
@@ -98,13 +113,17 @@ export class SentrySharedService {
98
113
  // It will log additional "breadcrumb object" before the error
99
114
  // It's a Breadcrumb, not a console.log, because console.log are NOT automatically attached as Breadcrumbs in cron-job environments (outside of Express)
100
115
  this.sentry().addBreadcrumb({
101
- message: inspectAny(err, {
102
- colors: false,
103
- }),
104
- // data: (err as AppError).data, // included in message
116
+ message: inspectAny(err, INSPECT_OPT),
105
117
  })
106
118
 
107
- return this.sentry().captureException(_anyToError(err))
119
+ if (data) {
120
+ // Log the attached ErrorData (if any)
121
+ this.sentry().addBreadcrumb({
122
+ message: inspectAny(data, INSPECT_OPT),
123
+ })
124
+ }
125
+
126
+ return this.sentry().captureException(err)
108
127
  }
109
128
 
110
129
  /**
@@ -131,13 +150,7 @@ export class SentrySharedService {
131
150
  log: () => {}, // noop
132
151
  warn: () => {}, // noop
133
152
  error: (...args) => {
134
- const message = args
135
- .map(arg =>
136
- inspectAny(arg, {
137
- colors: false,
138
- }),
139
- )
140
- .join(' ')
153
+ const message = args.map(arg => inspectAny(arg, INSPECT_OPT)).join(' ')
141
154
 
142
155
  this.sentry().addBreadcrumb({
143
156
  message,