2022-08-13 07:42:45 +08:00
|
|
|
import type { Context, Handler } from 'https://raw.githubusercontent.com/honojs/hono/v2.0.6/deno_dist/mod.ts'
|
2022-09-03 01:11:34 +08:00
|
|
|
import Toucan from 'https://cdn.skypack.dev/toucan-js@2.6.1'
|
2022-08-04 07:54:30 +08:00
|
|
|
|
2022-08-12 11:41:03 +08:00
|
|
|
declare module 'https://raw.githubusercontent.com/honojs/hono/v2.0.6/deno_dist/mod.ts' {
|
|
|
|
interface ContextVariableMap {
|
|
|
|
sentry: Toucan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockContext implements ExecutionContext {
|
|
|
|
passThroughOnException(): void {
|
|
|
|
throw new Error('Method not implemented.')
|
|
|
|
}
|
|
|
|
async waitUntil(promise: Promise<any>): Promise<void> {
|
|
|
|
await promise
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Options = {
|
|
|
|
dsn?: string
|
|
|
|
allowedCookies?: string[] | RegExp
|
|
|
|
allowedHeaders?: string[] | RegExp
|
|
|
|
allowedSearchParams?: string[] | RegExp
|
|
|
|
attachStacktrace?: boolean
|
|
|
|
debug?: boolean
|
|
|
|
environment?: string
|
|
|
|
maxBreadcrumbs?: number
|
|
|
|
pkg?: Record<string, any>
|
|
|
|
release?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export const sentry = (options?: Options, callback?: (sentry: Toucan) => void): Handler => {
|
2022-08-04 07:54:30 +08:00
|
|
|
return async (c, next) => {
|
2022-08-12 11:41:03 +08:00
|
|
|
let hasExecutionContext = true
|
|
|
|
try {
|
|
|
|
c.executionCtx
|
|
|
|
} catch {
|
|
|
|
hasExecutionContext = false
|
|
|
|
}
|
|
|
|
const sentry = new Toucan({
|
2022-08-11 03:54:58 +08:00
|
|
|
dsn: c.env.SENTRY_DSN || c.env.NEXT_PUBLIC_SENTRY_DSN,
|
2022-08-12 11:41:03 +08:00
|
|
|
allowedHeaders: ['user-agent'],
|
|
|
|
allowedSearchParams: /(.*)/,
|
|
|
|
request: c.req,
|
|
|
|
context: hasExecutionContext ? c.executionCtx : new MockContext(),
|
|
|
|
...options,
|
2022-08-11 03:54:58 +08:00
|
|
|
})
|
2022-09-03 01:11:34 +08:00
|
|
|
c.set('sentry', sentry)
|
2022-08-12 11:41:03 +08:00
|
|
|
if (callback) callback(sentry)
|
|
|
|
|
2022-08-11 03:54:58 +08:00
|
|
|
try {
|
|
|
|
await next()
|
|
|
|
} catch (error) {
|
2022-08-12 11:41:03 +08:00
|
|
|
sentry.captureException(error)
|
2022-08-11 03:54:58 +08:00
|
|
|
throw error
|
|
|
|
}
|
2022-08-04 07:54:30 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-12 11:41:03 +08:00
|
|
|
|
|
|
|
export const getSentry = (c: Context) => {
|
|
|
|
return c.get('sentry')
|
|
|
|
}
|