Skip to content

Commit db11e75

Browse files
committed
Refactor to remove wrapper around Java Sentry-object
1 parent 45c8462 commit db11e75

12 files changed

+1715
-355
lines changed

.changeset/giant-cups-deliver.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@item-enonic-types/lib-sentry": minor
3+
---
4+
5+
Add helper to build scope callbacks

.changeset/shaggy-islands-lay.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@item-enonic-types/lib-sentry": minor
3+
---
4+
5+
Refactor to expose Sentry Java-class directly

README.md

+44-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Enonic XP Library for enhancing data sent to Sentry.
44

55
[![](https://repo.itemtest.no/api/badge/latest/releases/no/item/lib-xp-sentry)](https://repo.itemtest.no/#/releases/no/item/lib-xp-sentry)
66

7-
87
<img src="https://github.com/ItemConsulting/lib-xp-sentry/raw/main/docs/icon.svg?sanitize=true" width="150">
98

109
## Installation
@@ -26,32 +25,50 @@ dependencies {
2625

2726
## Usage
2827

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+
}
5572
}
5673
```
5774

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ plugins {
77

88
dependencies {
99
compileOnly "com.enonic.xp:lib-admin:${xpVersion}"
10-
implementation "com.github.ua-parser:uap-java:1.5.4"
11-
implementation "io.sentry:sentry:6.17.0"
10+
implementation "com.github.ua-parser:uap-java:1.6.1"
11+
implementation "io.sentry:sentry:6.19.1"
1212
}
1313

1414
repositories {

0 commit comments

Comments
 (0)