@@ -4,7 +4,6 @@ Enonic XP Library for enhancing data sent to Sentry.
4
4
5
5
[ ![ ] ( https://repo.itemtest.no/api/badge/latest/releases/no/item/lib-xp-sentry )] ( https://repo.itemtest.no/#/releases/no/item/lib-xp-sentry )
6
6
7
-
8
7
<img src =" https://github.com/ItemConsulting/lib-xp-sentry/raw/main/docs/icon.svg?sanitize=true " width =" 150 " >
9
8
10
9
## Installation
@@ -26,32 +25,50 @@ dependencies {
26
25
27
26
## Usage
28
27
29
- ``` typescript
30
- import * as Sentry from " /lib/sentry" ;
31
- import { parseUserAgent } from " /lib/sentry/user-agent" ;
32
-
33
- export function parseRequestAndAddToSentry(req : XP .Request ): void {
34
- const { browser, os, device } = parseUserAgent (req );
35
-
36
- Sentry .configureScope ((scope ) => {
37
- scope
38
- .setTags ({
39
- branch: req .branch ,
40
- repositoryId: req .repositoryId ,
41
- })
42
- .setRequest ({
43
- url: req .url ,
44
- method: req .method ,
45
- })
46
- .setContext (" browser" , browser )
47
- .setContext (" os" , os );
48
-
49
- if (device .family ) {
50
- scope .setContext (" device" , {
51
- family: device .family ,
52
- });
53
- }
54
- });
28
+ You can use the ` Sentry.init ` method to configure the DSN for the Sentry client in your ` main.ts ` file.
29
+
30
+ ``` typescript
31
+ import { Sentry } from " /lib/sentry" ;
32
+
33
+ Sentry .init ((opts ): void => {
34
+ opts .setDsn (" https://public@sentry.example.com/1" );
35
+ opts .setRelease (app .version );
36
+ opts .setEnvironment (getInstallation ());
37
+ });
38
+ ```
39
+
40
+ You can use your _ /error/error.ts_ to get access to the ` Exception ` thrown, and send it to Sentry using
41
+ ` Sentry.captureException ` .
42
+
43
+ ``` typescript
44
+ import { Sentry } from " /lib/sentry" ;
45
+ import { ScopeCallbackBuilder } from " /lib/sentry/helpers" ;
46
+ import type { ErrorRequest , Request , Response } from " @enonic-types/core" ;
47
+
48
+ export function handleError(err : ErrorRequest ): Response {
49
+ if (err .status !== 404 ) {
50
+ reportToSentry (err );
51
+ }
52
+
53
+ return {
54
+ status: err .status ,
55
+ body: ` <h1>${err .status } error</h1> `
56
+ }
57
+ }
58
+
59
+ function reportToSentry(err : ErrorRequest ): void {
60
+ try {
61
+ const req = err .request ;
62
+
63
+ const callback = new ScopeCallbackBuilder ()
64
+ .setRequest (err .request )
65
+ .setTag (" branch" , err .request .branch )
66
+ .build ();
67
+
68
+ Sentry .captureException (err .exception , callback );
69
+ } catch (e ) {
70
+ log .error (" Failed to configure Sentry scope" , e );
71
+ }
55
72
}
56
73
```
57
74
0 commit comments