2022-12-13 14:40:22 +08:00
|
|
|
import type { Context, MiddlewareHandler } from 'https://deno.land/x/hono/mod.ts'
|
2023-02-04 15:19:47 +08:00
|
|
|
import Toucan from 'https://cdn.skypack.dev/toucan-js@2.7.0'
|
2022-08-04 07:54:30 +08:00
|
|
|
|
2022-12-13 14:40:22 +08:00
|
|
|
declare module 'https://deno.land/x/hono/mod.ts' {
|
2022-08-12 11:41:03 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-13 06:50:21 +08:00
|
|
|
export const sentry = (
|
|
|
|
options?: Options,
|
|
|
|
callback?: (sentry: Toucan) => void
|
2023-03-02 14:14:11 +08:00
|
|
|
): MiddlewareHandler => {
|
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({
|
2023-06-02 05:31:51 +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: /(.*)/,
|
2023-03-02 14:14:11 +08:00
|
|
|
request: c.req.raw,
|
2022-08-12 11:41:03 +08:00
|
|
|
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-10-10 00:07:26 +08:00
|
|
|
await next()
|
|
|
|
if (c.error) {
|
|
|
|
sentry.captureException(c.error)
|
2022-08-11 03:54:58 +08:00
|
|
|
}
|
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')
|
|
|
|
}
|