You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Start] Function middleware: try/catch doesn't catch errors from next() - must check result.error instead #6418
Function middleware (type: 'function') cannot catch errors from inner middleware/handlers using try/catch. The docs show this pattern, but it doesn't work for function middleware.
Reproduction
// This does NOT work - catch block never firesconsterrorMiddleware=createMiddleware({type: 'function'}).server(async({ next })=>{try{returnawaitnext()}catch(error){// Never reached when inner middleware/handler throwsconsole.log('caught',error)throwerror}})
Root Cause
In createServerFn.js, userNext catches errors and returns them as { error } instead of throwing:
// Line 154-161constuserNext=async(userCtx={})=>{try{returnawaitcallNextMiddleware(nextCtx);}catch(error){return{ ...nextCtx, error };// Returns, doesn't throw}};
Working Solution
Must check result.error instead:
consterrorMiddleware=createMiddleware({type: 'function'}).server(async({ next })=>{constresult=awaitnext()if(result.error){// Handle error herereturn{ ...result,error: newError('Sanitized message')}}returnresult})
Issues
Docs show try/catch (observability guide) but that's for request middleware, not function middleware
TypeScript types don't include error on the result - requires cast: (await next()) as { error?: Error }
Ambiguity - unclear when to use try/catch vs result.error
Questions
Is checking result.error the intended pattern for function middleware error handling?
Should TypeScript types include error property on FunctionServerResultWithContext?
Should docs clarify the difference between request middleware and function middleware error handling?
Activity
alexander-zuev commentedon Jan 19, 2026
This was fixed in v1.151.3 via #6392. Closing as resolved. Thanks!