@benhepburn/adonis-sentry 1.0.1 → 1.0.4

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/README.md CHANGED
@@ -35,6 +35,13 @@ SENTRY_DSN=https://<...>.sentry.io/<...>
35
35
  SENTRY_TRACES_SAMPLE_RATE=1.0 # Set this to a lower value in production
36
36
  ```
37
37
 
38
+ To enable enhanced event data, you must enable useAsyncLocalStorage in config/app.ts
39
+ so the current request can be fetched ([see caveats here](https://docs.adonisjs.com/guides/async-local-storage#caveats)):
40
+
41
+ ```ts
42
+ useAsyncLocalStorage: true,
43
+ ```
44
+
38
45
  ## Usage
39
46
 
40
47
  Import the service to use it:
@@ -43,13 +50,17 @@ Import the service to use it:
43
50
  import sentry from "@benhepburn/adonis-sentry/service"
44
51
  ```
45
52
 
46
- To capture all exceptions, add this line to app/exceptions/handler inside the handle function:
53
+ To capture all exceptions, add the capture line to app/exceptions/handler.ts inside the report function:
47
54
 
48
55
  ```ts
49
- sentry.captureException(error);
56
+ async report(error: unknown, ctx: HttpContext) {
57
+ sentry.captureException(error); // Add this line
58
+ ...
59
+ return super.report(error, ctx);
60
+ }
50
61
  ```
51
62
 
52
- This can be used to capture exceptions manually as well:
63
+ Exceptions can be captured manually as well:
53
64
 
54
65
  ```ts
55
66
  try {
@@ -1,4 +1,5 @@
1
1
  import * as Sentry from '@sentry/node';
2
+ import { adonisEventProcessor } from '../src/adonis_event_processor.js';
2
3
  export default class SentryProvider {
3
4
  app;
4
5
  constructor(app) {
@@ -12,6 +13,7 @@ export default class SentryProvider {
12
13
  const config = await resolver.make('config');
13
14
  const sentryConfig = config.get('sentry');
14
15
  Sentry.init(sentryConfig);
16
+ Sentry.addEventProcessor(adonisEventProcessor);
15
17
  return Sentry;
16
18
  });
17
19
  }
@@ -0,0 +1,2 @@
1
+ import { EventProcessor } from '@sentry/types/types/eventprocessor.js';
2
+ export declare const adonisEventProcessor: EventProcessor;
@@ -0,0 +1,21 @@
1
+ import { HttpContext } from '@adonisjs/core/http';
2
+ export const adonisEventProcessor = (event, _hint) => {
3
+ const ctx = HttpContext.get();
4
+ if (!ctx)
5
+ return event;
6
+ if (!event.contexts)
7
+ event.contexts = {};
8
+ // Add request data
9
+ event.request = {
10
+ method: ctx.request.method(),
11
+ url: ctx.request.url(true),
12
+ };
13
+ // Add response data
14
+ event.contexts.response = {
15
+ status_code: ctx.response.getStatus(),
16
+ };
17
+ // Add user
18
+ // @ts-ignore
19
+ event.user = ctx.auth?.user || null;
20
+ return event;
21
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@benhepburn/adonis-sentry",
3
3
  "description": "A Sentry integration for AdonisJS v6",
4
- "version": "1.0.1",
4
+ "version": "1.0.4",
5
5
  "engines": {
6
6
  "node": ">=20.0.0"
7
7
  },