diff --git a/deno_dist/index.ts b/deno_dist/index.ts index 48d10319..292d1b82 100644 --- a/deno_dist/index.ts +++ b/deno_dist/index.ts @@ -1,5 +1,5 @@ import type { Context, Handler } from 'https://raw.githubusercontent.com/honojs/hono/v2.0.6/deno_dist/mod.ts' -import Toucan from "https://cdn.skypack.dev/toucan-js@2.6.1" +import Toucan from 'https://cdn.skypack.dev/toucan-js@2.6.1' declare module 'https://raw.githubusercontent.com/honojs/hono/v2.0.6/deno_dist/mod.ts' { interface ContextVariableMap { @@ -45,7 +45,7 @@ export const sentry = (options?: Options, callback?: (sentry: Toucan) => void): context: hasExecutionContext ? c.executionCtx : new MockContext(), ...options, }) - + c.set('sentry', sentry) if (callback) callback(sentry) try { diff --git a/package.json b/package.json index 9e41b342..b424bddc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@honojs/sentry", - "version": "0.0.1", + "version": "0.0.2", "description": "Sentry Middleware for Hono", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 000fd0da..ace2c08c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,7 +45,7 @@ export const sentry = (options?: Options, callback?: (sentry: Toucan) => void): context: hasExecutionContext ? c.executionCtx : new MockContext(), ...options, }) - + c.set('sentry', sentry) if (callback) callback(sentry) try { diff --git a/src/replacer.ts b/src/replacer.ts index c1a222d8..113c5288 100644 --- a/src/replacer.ts +++ b/src/replacer.ts @@ -3,7 +3,7 @@ import { makeThisModuleAnExecutableReplacer } from 'denoify' makeThisModuleAnExecutableReplacer(async ({ parsedImportExportStatement, version }) => { if (parsedImportExportStatement.parsedArgument.nodeModuleName === 'toucan-js') { - return `import Toucan from "https://cdn.skypack.dev/toucan-js@${version}"` + return `import Toucan from 'https://cdn.skypack.dev/toucan-js@${version}'` } return undefined }) diff --git a/test/index.test.ts b/test/index.test.ts index 29a11e2e..ed61cd5b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,5 +1,5 @@ import { Hono } from 'hono' -import { sentry } from '../src' +import { sentry, getSentry } from '../src' // Mock class Context implements ExecutionContext { @@ -12,7 +12,8 @@ class Context implements ExecutionContext { } const captureException = jest.fn() -jest.mock('toucan-js', () => jest.fn().mockImplementation(() => ({ captureException }))) +const log = jest.fn() +jest.mock('toucan-js', () => jest.fn().mockImplementation(() => ({ captureException, log }))) const callback = jest.fn() describe('Sentry middleware', () => { @@ -20,6 +21,7 @@ describe('Sentry middleware', () => { app.use('/sentry/*', sentry(undefined, callback)) app.get('/sentry/foo', (c) => c.text('foo')) + app.get('/sentry/bar', (c) => getSentry(c).log('bar') && c.text('bar')) app.get('/sentry/error', () => { throw new Error('a catastrophic error') }) @@ -32,6 +34,14 @@ describe('Sentry middleware', () => { expect(callback).toHaveBeenCalled() }) + it('Should make Sentry available via context', async () => { + const req = new Request('http://localhost/sentry/bar') + const res = await app.fetch(req, {}, new Context()) + expect(res).not.toBeNull() + expect(res.status).toBe(500) + expect(log).toHaveBeenCalled() + }) + it('Should report errors', async () => { const req = new Request('http://localhost/sentry/error') const res = await app.fetch(req, {}, new Context())