@pbvision/fastify-firestore-service 0.0.39 → 0.0.41

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.
package/docs/api.md CHANGED
@@ -23,6 +23,7 @@ This library is used to define APIs.
23
23
  - [Asynchronous Processing](#asynchronous-processing)
24
24
  - [Cross Origin (CORS)](#cross-origin-cors)
25
25
  - [Calling other APIs](#calling-other-apis)
26
+ - [Sentry Context](#sentry-context)
26
27
  - [Niche Concepts](#niche-concepts)
27
28
  - [Other API Input Data Options](#other-api-input-data-options)
28
29
  - [Custom Middleware](#custom-middleware)
@@ -226,6 +227,9 @@ class DupErrorCodeAPI extends API {
226
227
  }
227
228
 
228
229
  async computeResponse (req) {
230
+ this.setSentryTag('test', 'me')
231
+ this.setSentryTag('another', 'test')
232
+ this.setSentryUserInfo({ email: 'x@example.com', id: '123' })
229
233
  if (req.body.exception === 'notfound') {
230
234
  throw new NotFoundException() // default error message "Not found"
231
235
  } else {
@@ -542,6 +546,13 @@ static CORS_ORIGIN = '*'
542
546
  pass some information in a cookie, see the `redirectToWebApp()` helper method.
543
547
 
544
548
 
549
+ ## Sentry Context
550
+ By default, query parameter, path parameter and body parameters are included as
551
+ context for reports to Sentry. To disable or customize this, override the `getInputsToTrackWithSentry()` method. You can also use the
552
+ `setSentryContext()`, `setSentryTag()` and `setSentryUserInfo()` methods to
553
+ add custom metadata for the request.
554
+
555
+
545
556
  # Niche Concepts
546
557
  This section explains niche functionality.
547
558
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pbvision/fastify-firestore-service",
3
- "version": "0.0.39",
3
+ "version": "0.0.41",
4
4
  "description": "Web Framework using Fastify and Firestore ORM",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
package/src/api/api.js CHANGED
@@ -214,6 +214,8 @@ class API {
214
214
  }
215
215
  reply.logRequestBodyOnError = this.constructor.LOG_REQUEST_BODY_ON_ERROR
216
216
  reply.apiName = this.constructor.name
217
+ this.req.__sentry = { context: {}, tags: {} }
218
+ this.__trackInputsWithSentry()
217
219
  }
218
220
 
219
221
  /**
@@ -405,6 +407,49 @@ class API {
405
407
  return ret
406
408
  }
407
409
 
410
+ /**
411
+ * Adds inputs from the query string, path params and body to the Sentry
412
+ * context. This provides helpful debugging information but should only be
413
+ * used if these fields don't include sensitive information.
414
+ */
415
+ __trackInputsWithSentry () {
416
+ const inputs = this.getInputsToTrackWithSentry()
417
+ // istanbul ignore else
418
+ if (inputs) {
419
+ this.setSentryContext('inputs', inputs)
420
+ }
421
+ }
422
+
423
+ /**
424
+ * Returns a map of inputs to track in Sentry's context.
425
+ *
426
+ * By default, includes the query string, path params and body. This provides
427
+ * helpful debugging information but should only be
428
+ * used if these fields don't include sensitive information.
429
+ */
430
+ getInputsToTrackWithSentry () {
431
+ return {
432
+ ...this.req.query,
433
+ ...this.req.params,
434
+ ...this.req.body
435
+ }
436
+ }
437
+
438
+ /** Sets context to be transmitted to Sentry if this request fails. */
439
+ setSentryContext (key, contextObject) {
440
+ this.req.__sentry.context[key] = contextObject
441
+ }
442
+
443
+ /** Sets a tag to be transmitted to Sentry if this request fails. */
444
+ setSentryTag (tag, tagValue) {
445
+ this.req.__sentry.tags[tag] = tagValue
446
+ }
447
+
448
+ /** Sets user info to be transmitted to Sentry if this request fails. */
449
+ setSentryUserInfo (userInfo) {
450
+ this.req.__sentry.userInfo = userInfo
451
+ }
452
+
408
453
  /**
409
454
  * Redirects to a URL optionally with query string and cookie.
410
455
  *
@@ -752,6 +797,7 @@ class API {
752
797
  let ret
753
798
  try {
754
799
  if (req.validationError) {
800
+ req.__sentry = { context: { validationError: req.validationError } }
755
801
  throw new InvalidInputException(req.validationError)
756
802
  }
757
803
  ret = await this._callAndHandleRequestDone(reply, async () => {
@@ -106,12 +106,20 @@ export default fp(function (fastify, options, next) {
106
106
  user.ip = req.ip
107
107
  }
108
108
  scope.setLevel(isCrash ? 'error' : 'warning')
109
- scope.setUser(user)
109
+ scope.setUser({
110
+ ...user,
111
+ ...(req.__sentry?.userInfo ?? {})
112
+ })
110
113
  scope.setTags({
114
+ ...(req.__sentry?.tags ?? {}),
111
115
  method: req.method,
112
116
  url: req.url,
113
117
  status: errInfo.status
114
118
  })
119
+ const customContexts = req.__sentry?.context ?? {}
120
+ for (const [k, v] of Object.entries(customContexts)) {
121
+ scope.setContext(k, v)
122
+ }
115
123
  const extras = {
116
124
  msg: errInfo.message,
117
125
  reqId: req.id,