Enable options and callback function
parent
b47e0004e2
commit
8e16d4c9a0
|
@ -39,6 +39,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^3.14.0",
|
"@cloudflare/workers-types": "^3.14.0",
|
||||||
|
"@sentry/types": "^7.10.0",
|
||||||
"@types/jest": "^28.1.4",
|
"@types/jest": "^28.1.4",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.32.0",
|
"@typescript-eslint/eslint-plugin": "^5.32.0",
|
||||||
"@typescript-eslint/parser": "^5.32.0",
|
"@typescript-eslint/parser": "^5.32.0",
|
||||||
|
@ -58,4 +59,4 @@
|
||||||
"ts-jest": "^28.0.5",
|
"ts-jest": "^28.0.5",
|
||||||
"typescript": "^4.7.4"
|
"typescript": "^4.7.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
28
src/index.ts
28
src/index.ts
|
@ -1,15 +1,39 @@
|
||||||
|
import type { Options as SentryOptions, StackFrame } from '@sentry/types'
|
||||||
import type { Handler } from 'hono'
|
import type { Handler } from 'hono'
|
||||||
import Toucan from 'toucan-js'
|
import Toucan from 'toucan-js'
|
||||||
|
|
||||||
export const sentry = (): Handler => {
|
export type RewriteFrames = {
|
||||||
|
root?: string
|
||||||
|
iteratee?: (frame: StackFrame) => StackFrame
|
||||||
|
}
|
||||||
|
|
||||||
|
type Options = {
|
||||||
|
dsn?: SentryOptions['dsn']
|
||||||
|
allowedCookies?: string[] | RegExp
|
||||||
|
allowedHeaders?: string[] | RegExp
|
||||||
|
allowedSearchParams?: string[] | RegExp
|
||||||
|
attachStacktrace?: SentryOptions['attachStacktrace']
|
||||||
|
debug?: SentryOptions['debug']
|
||||||
|
environment?: SentryOptions['environment']
|
||||||
|
maxBreadcrumbs?: SentryOptions['maxBreadcrumbs']
|
||||||
|
pkg?: Record<string, any>
|
||||||
|
release?: SentryOptions['release']
|
||||||
|
rewriteFrames?: RewriteFrames
|
||||||
|
}
|
||||||
|
|
||||||
|
export const sentry = (options?: Options, callback?: (sentry: Toucan) => void): Handler => {
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
const sentry = new Toucan({
|
const sentry = new Toucan({
|
||||||
dsn: c.env.SENTRY_DSN || c.env.NEXT_PUBLIC_SENTRY_DSN,
|
dsn: c.env.SENTRY_DSN || c.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||||
request: c.req,
|
|
||||||
allowedHeaders: ['user-agent'],
|
allowedHeaders: ['user-agent'],
|
||||||
allowedSearchParams: /(.*)/,
|
allowedSearchParams: /(.*)/,
|
||||||
|
request: c.req,
|
||||||
|
context: c.executionCtx,
|
||||||
|
...options,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (callback) callback(sentry)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await next()
|
await next()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -1,6 +1,16 @@
|
||||||
import { Hono } from 'hono'
|
import { Hono } from 'hono'
|
||||||
import { sentry } from '../src'
|
import { sentry } from '../src'
|
||||||
|
|
||||||
|
// Mock
|
||||||
|
class Context implements ExecutionContext {
|
||||||
|
passThroughOnException(): void {
|
||||||
|
throw new Error('Method not implemented.')
|
||||||
|
}
|
||||||
|
async waitUntil(promise: Promise<any>): Promise<void> {
|
||||||
|
await promise
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const captureException = jest.fn()
|
const captureException = jest.fn()
|
||||||
jest.mock('toucan-js', () => jest.fn().mockImplementation(() => ({ captureException })))
|
jest.mock('toucan-js', () => jest.fn().mockImplementation(() => ({ captureException })))
|
||||||
|
|
||||||
|
@ -14,13 +24,15 @@ describe('Sentry middleware', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should initialize Toucan', async () => {
|
it('Should initialize Toucan', async () => {
|
||||||
const res = await app.request('http://localhost/sentry/foo')
|
const req = new Request('http://localhost/sentry/foo')
|
||||||
|
const res = await app.fetch(req, {}, new Context())
|
||||||
expect(res).not.toBeNull()
|
expect(res).not.toBeNull()
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should report errors', async () => {
|
it('Should report errors', async () => {
|
||||||
const res = await app.request('http://localhost/sentry/error')
|
const req = new Request('http://localhost/sentry/error')
|
||||||
|
const res = await app.fetch(req, {}, new Context())
|
||||||
expect(res).not.toBeNull()
|
expect(res).not.toBeNull()
|
||||||
expect(res.status).toBe(500)
|
expect(res.status).toBe(500)
|
||||||
expect(captureException).toHaveBeenCalled()
|
expect(captureException).toHaveBeenCalled()
|
||||||
|
|
|
@ -892,6 +892,11 @@
|
||||||
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.19.6.tgz#70513f9dca05d23d7ab9c2a6cb08d4db6763ca67"
|
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.19.6.tgz#70513f9dca05d23d7ab9c2a6cb08d4db6763ca67"
|
||||||
integrity sha512-QH34LMJidEUPZK78l+Frt3AaVFJhEmIi05Zf8WHd9/iTt+OqvCHBgq49DDr1FWFqyYWm/QgW/3bIoikFpfsXyQ==
|
integrity sha512-QH34LMJidEUPZK78l+Frt3AaVFJhEmIi05Zf8WHd9/iTt+OqvCHBgq49DDr1FWFqyYWm/QgW/3bIoikFpfsXyQ==
|
||||||
|
|
||||||
|
"@sentry/types@^7.10.0":
|
||||||
|
version "7.10.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.10.0.tgz#c91d634768336238ac30ed750fa918326c384cbb"
|
||||||
|
integrity sha512-1UBwdbS0xXzANzp63g4eNQly/qKIXp0swP5OTKWoADvKBtL4anroLUA/l8ADMtuwFZYtVANc8WRGxM2+YmaXtg==
|
||||||
|
|
||||||
"@sentry/utils@6.19.6":
|
"@sentry/utils@6.19.6":
|
||||||
version "6.19.6"
|
version "6.19.6"
|
||||||
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-6.19.6.tgz#2ddc9ef036c3847084c43d0e5a55e4646bdf9021"
|
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-6.19.6.tgz#2ddc9ef036c3847084c43d0e5a55e4646bdf9021"
|
||||||
|
|
Loading…
Reference in New Issue