Skip to content

Commit

Permalink
feat(sentry): bump up toucan-js to v3 (#137)
Browse files Browse the repository at this point in the history
* feat(sentry): bump up `toucan-js` to v3

* update readme

* add changeset

* update readme
  • Loading branch information
yusukebe authored Aug 24, 2023
1 parent 05f4b47 commit c2093e1
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 122 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-frogs-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/sentry': major
---

feat: bump up `toucan-js` to v3
59 changes: 52 additions & 7 deletions packages/sentry/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
# Sentry middleware for Hono
# Sentry Middleware for Hono

Sentry middleware for [Hono](https://github.com/honojs/hono).
This middleware sends captured exceptions to the specified Sentry data source name via [toucan-js](https://github.com/robertcepa/toucan-js).
This middleware integrates [Hono](https://github.com/honojs/hono) with Sentry. It captures exceptions and sends them to the specified Sentry data source name (DSN) using [toucan-js](https://github.com/robertcepa/toucan-js).

## Usage
## Installation

```plain
npm i hono @hono/sentry
```

## Configuration

If you're running your application on Cloudflare Workers, set a binding value named `SENTRY_DSN`, which will be used as the DSN. For instance, during development, you can specify this in `.dev.vars`:

```plain
SENTRY_DSN=<Your DSN>
```

On other platforms, you can directly provide the DSN by passing it as an option:

```ts
sentry({
dsn: `<Your DSN>`,
})
```

## How to Use

```ts
import { Hono } from 'hono'
Expand All @@ -17,7 +38,14 @@ app.get('/', (c) => c.text('foo'))
export default app
```

## Deno
Options:

```ts
import type { Options as ToucanOptions } from 'toucan-js'
type Options = Omit<ToucanOptions, 'request' | 'context'>
```
### For Deno Users
```ts
import { serve } from 'https://deno.land/std/http/server.ts'
Expand All @@ -32,9 +60,26 @@ app.get('/', (c) => c.text('foo'))
serve(app.fetch)
```

## Author
### Accessing an instance of `Sentry`

You can retrieve an instance of `Sentry` using `c.get('sentry')`.

```ts
app.onError((e, c) => {
c.get('sentry').setContext('character', {
name: 'Mighty Fighter',
age: 19,
attack_type: 'melee',
})
c.get('sentry').captureException(e)
return c.text('Internal Server Error', 500)
})
```

## Authors

Samuel Lippert <https://github.com/sam-lippert>
- Samuel Lippert - <https://github.com/sam-lippert>
- Yusuke Wada - <https://github.com/yusukebe>

## License

Expand Down
9 changes: 5 additions & 4 deletions packages/sentry/deno_dist/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Context, MiddlewareHandler } from 'https://deno.land/x/hono/mod.ts'
import Toucan from 'https://cdn.skypack.dev/[email protected]'
import type { Options as ToucanOptions } from 'https://cdn.skypack.dev/[email protected]'
import { Toucan, type Options as ToucanOptions } from 'https://esm.sh/[email protected]'

declare module 'https://deno.land/x/hono/mod.ts' {
interface ContextVariableMap {
Expand Down Expand Up @@ -33,8 +32,10 @@ export const sentry = (
}
const sentry = new Toucan({
dsn: c.env?.SENTRY_DSN ?? c.env?.NEXT_PUBLIC_SENTRY_DSN,
allowedHeaders: ['user-agent'],
allowedSearchParams: /(.*)/,
requestDataOptions: {
allowedHeaders: ['user-agent'],
allowedSearchParams: /(.*)/,
},
request: c.req.raw,
context: hasExecutionContext ? c.executionCtx : new MockContext(),
...options,
Expand Down
2 changes: 1 addition & 1 deletion packages/sentry/deno_test/deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { assert, assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
export { Hono } from 'https://deno.land/x/hono@v3.2.3/mod.ts'
export { Hono } from 'https://deno.land/x/hono/mod.ts'
6 changes: 3 additions & 3 deletions packages/sentry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"access": "public"
},
"peerDependencies": {
"hono": "^3.2.3"
"hono": "3.*"
},
"dependencies": {
"toucan-js": "^2.6.1"
"toucan-js": "^3.2.2"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.14.0",
Expand All @@ -52,7 +52,7 @@
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"hono": "^3.0.2",
"hono": "^3.5.1",
"jest": "^28.1.2",
"jest-environment-miniflare": "^2.6.0",
"np": "^7.6.2",
Expand Down
8 changes: 5 additions & 3 deletions packages/sentry/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context, MiddlewareHandler } from 'hono'
import Toucan from 'toucan-js'
import { Toucan } from 'toucan-js'
import type { Options as ToucanOptions } from 'toucan-js'

declare module 'hono' {
Expand Down Expand Up @@ -33,8 +33,10 @@ export const sentry = (
}
const sentry = new Toucan({
dsn: c.env?.SENTRY_DSN ?? c.env?.NEXT_PUBLIC_SENTRY_DSN,
allowedHeaders: ['user-agent'],
allowedSearchParams: /(.*)/,
requestDataOptions: {
allowedHeaders: ['user-agent'],
allowedSearchParams: /(.*)/,
},
request: c.req.raw,
context: hasExecutionContext ? c.executionCtx : new MockContext(),
...options,
Expand Down
2 changes: 1 addition & 1 deletion packages/sentry/src/replacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ makeThisModuleAnExecutableReplacer(async ({ parsedImportExportStatement, version
...parsedImportExportStatement,
parsedArgument: {
type: 'URL',
url: `https://cdn.skypack.dev/toucan-js@${version}`,
url: `https://esm.sh/toucan-js@${version}`,
},
})
}
Expand Down
8 changes: 6 additions & 2 deletions packages/sentry/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class Context implements ExecutionContext {

const captureException = jest.fn()
const log = jest.fn()
jest.mock('toucan-js', () => jest.fn().mockImplementation(() => ({ captureException, log })))

jest.mock('toucan-js', () => ({
Toucan: jest.fn().mockImplementation(() => ({ captureException, log })),
}))

const callback = jest.fn()

describe('Sentry middleware', () => {
Expand All @@ -23,7 +27,7 @@ describe('Sentry middleware', () => {
app.use('/sentry/*', sentry(undefined, callback))
app.get('/sentry/foo', (c) => c.text('foo'))
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// @ts-expect-error
app.get('/sentry/bar', (c) => getSentry(c).log('bar') || c.text('bar'))
app.get('/sentry/error', () => {
throw new Error('a catastrophic error')
Expand Down
Loading

0 comments on commit c2093e1

Please sign in to comment.