@@ -5,6 +5,7 @@ import { z } from 'zod'
55import { AuthUser } from '../../mcp-observability/src'
66import { getAuthorizationURL , getAuthToken , refreshAuthToken } from './cloudflare-auth'
77import { McpError } from './mcp-error'
8+ import { useSentry } from './sentry'
89
910import type {
1011 OAuthHelpers ,
@@ -13,14 +14,15 @@ import type {
1314} from '@cloudflare/workers-oauth-provider'
1415import type { Context } from 'hono'
1516import type { MetricsTracker } from '../../mcp-observability/src'
17+ import type { BaseHonoContext } from './sentry'
1618
1719type AuthContext = {
1820 Bindings : {
1921 OAUTH_PROVIDER : OAuthHelpers
2022 CLOUDFLARE_CLIENT_ID : string
2123 CLOUDFLARE_CLIENT_SECRET : string
2224 }
23- }
25+ } & BaseHonoContext
2426
2527const AuthRequestSchema = z . object ( {
2628 responseType : z . string ( ) ,
@@ -94,11 +96,11 @@ async function getTokenAndUser(
9496
9597 if ( ! userResponse . ok ) {
9698 console . log ( await userResponse . text ( ) )
97- throw new McpError ( 'Failed to fetch user' , 500 )
99+ throw new McpError ( 'Failed to fetch user' , 500 , { reportToSentry : true } )
98100 }
99101 if ( ! accountsResponse . ok ) {
100102 console . log ( await accountsResponse . text ( ) )
101- throw new McpError ( 'Failed to fetch accounts' , 500 )
103+ throw new McpError ( 'Failed to fetch accounts' , 500 , { reportToSentry : true } )
102104 }
103105
104106 // Fetch the user & accounts info from Cloudflare
@@ -153,6 +155,9 @@ export function createAuthHandlers({
153155} ) {
154156 {
155157 const app = new Hono < AuthContext > ( )
158+ app . use ( useSentry )
159+ // TODO: Add useOnError middleware rather than handling errors in each handler
160+ // app.onError(useOnError)
156161 /**
157162 * OAuth Authorization Endpoint
158163 *
@@ -178,6 +183,7 @@ export function createAuthHandlers({
178183
179184 return Response . redirect ( res . authUrl , 302 )
180185 } catch ( e ) {
186+ c . var . sentry ?. recordError ( e )
181187 if ( e instanceof Error ) {
182188 metrics . logEvent (
183189 new AuthUser ( {
@@ -254,6 +260,7 @@ export function createAuthHandlers({
254260
255261 return Response . redirect ( redirectTo , 302 )
256262 } catch ( e ) {
263+ c . var . sentry ?. recordError ( e )
257264 if ( e instanceof Error ) {
258265 console . error ( e )
259266 metrics . logEvent (
0 commit comments