Skip to content

Commit c833492

Browse files
TheodoreSpeaksTheodore Li
andauthored
fix(error): catch socket auth error as 4xx (#4059)
* fix(error): catch socket auth error as 4xx * Switch to type guard --------- Co-authored-by: Theodore Li <theo@sim.ai>
1 parent 1856635 commit c833492

File tree

1 file changed

+12
-0
lines changed
  • apps/sim/app/api/auth/socket-token

1 file changed

+12
-0
lines changed

apps/sim/app/api/auth/socket-token/route.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ export async function POST() {
2323

2424
return NextResponse.json({ token: response.token })
2525
} catch (error) {
26+
// better-auth's sessionMiddleware throws APIError("UNAUTHORIZED") with no message
27+
// when the session is missing/expired — surface this as a 401, not a 500.
28+
if (
29+
error instanceof Error &&
30+
('statusCode' in error || 'status' in error) &&
31+
((error as Record<string, unknown>).statusCode === 401 ||
32+
(error as Record<string, unknown>).status === 'UNAUTHORIZED')
33+
) {
34+
logger.warn('Socket token request with invalid/expired session')
35+
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
36+
}
37+
2638
logger.error('Failed to generate socket token', {
2739
error: error instanceof Error ? error.message : String(error),
2840
stack: error instanceof Error ? error.stack : undefined,

0 commit comments

Comments
 (0)