mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-28 03:00:29 -04:00
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>
This commit is contained in:
@@ -23,6 +23,18 @@ export async function POST() {
|
||||
|
||||
return NextResponse.json({ token: response.token })
|
||||
} catch (error) {
|
||||
// better-auth's sessionMiddleware throws APIError("UNAUTHORIZED") with no message
|
||||
// when the session is missing/expired — surface this as a 401, not a 500.
|
||||
if (
|
||||
error instanceof Error &&
|
||||
('statusCode' in error || 'status' in error) &&
|
||||
((error as Record<string, unknown>).statusCode === 401 ||
|
||||
(error as Record<string, unknown>).status === 'UNAUTHORIZED')
|
||||
) {
|
||||
logger.warn('Socket token request with invalid/expired session')
|
||||
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
||||
}
|
||||
|
||||
logger.error('Failed to generate socket token', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
stack: error instanceof Error ? error.stack : undefined,
|
||||
|
||||
Reference in New Issue
Block a user