Fix getSentry() issue

pull/31/head
Samuel Lippert 2022-09-02 12:11:34 -05:00
parent ca742a2ea3
commit 8e290c0b70
No known key found for this signature in database
GPG Key ID: 3762857AF8948B2C
5 changed files with 17 additions and 7 deletions

View File

@ -1,5 +1,5 @@
import type { Context, Handler } from 'https://raw.githubusercontent.com/honojs/hono/v2.0.6/deno_dist/mod.ts' 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' { declare module 'https://raw.githubusercontent.com/honojs/hono/v2.0.6/deno_dist/mod.ts' {
interface ContextVariableMap { interface ContextVariableMap {
@ -45,7 +45,7 @@ export const sentry = (options?: Options, callback?: (sentry: Toucan) => void):
context: hasExecutionContext ? c.executionCtx : new MockContext(), context: hasExecutionContext ? c.executionCtx : new MockContext(),
...options, ...options,
}) })
c.set('sentry', sentry)
if (callback) callback(sentry) if (callback) callback(sentry)
try { try {

View File

@ -1,6 +1,6 @@
{ {
"name": "@honojs/sentry", "name": "@honojs/sentry",
"version": "0.0.1", "version": "0.0.2",
"description": "Sentry Middleware for Hono", "description": "Sentry Middleware for Hono",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@ -45,7 +45,7 @@ export const sentry = (options?: Options, callback?: (sentry: Toucan) => void):
context: hasExecutionContext ? c.executionCtx : new MockContext(), context: hasExecutionContext ? c.executionCtx : new MockContext(),
...options, ...options,
}) })
c.set('sentry', sentry)
if (callback) callback(sentry) if (callback) callback(sentry)
try { try {

View File

@ -3,7 +3,7 @@ import { makeThisModuleAnExecutableReplacer } from 'denoify'
makeThisModuleAnExecutableReplacer(async ({ parsedImportExportStatement, version }) => { makeThisModuleAnExecutableReplacer(async ({ parsedImportExportStatement, version }) => {
if (parsedImportExportStatement.parsedArgument.nodeModuleName === 'toucan-js') { 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 return undefined
}) })

View File

@ -1,5 +1,5 @@
import { Hono } from 'hono' import { Hono } from 'hono'
import { sentry } from '../src' import { sentry, getSentry } from '../src'
// Mock // Mock
class Context implements ExecutionContext { class Context implements ExecutionContext {
@ -12,7 +12,8 @@ class Context implements ExecutionContext {
} }
const captureException = jest.fn() 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() const callback = jest.fn()
describe('Sentry middleware', () => { describe('Sentry middleware', () => {
@ -20,6 +21,7 @@ describe('Sentry middleware', () => {
app.use('/sentry/*', sentry(undefined, callback)) app.use('/sentry/*', sentry(undefined, callback))
app.get('/sentry/foo', (c) => c.text('foo')) app.get('/sentry/foo', (c) => c.text('foo'))
app.get('/sentry/bar', (c) => getSentry(c).log('bar') && c.text('bar'))
app.get('/sentry/error', () => { app.get('/sentry/error', () => {
throw new Error('a catastrophic error') throw new Error('a catastrophic error')
}) })
@ -32,6 +34,14 @@ describe('Sentry middleware', () => {
expect(callback).toHaveBeenCalled() 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 () => { it('Should report errors', async () => {
const req = new Request('http://localhost/sentry/error') const req = new Request('http://localhost/sentry/error')
const res = await app.fetch(req, {}, new Context()) const res = await app.fetch(req, {}, new Context())