Skip to content

[Feature?]: Could createMiddleware expose like "onError" handler for processing errors uniformly聽#1660

Closed as not planned

Description

@emogua

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 馃挕

No response

Examples 馃寛

No response

Motivation 馃敠

No response

Activity

  1. lxsmnsyc commented on Oct 14, 2026

    @lxsmnsyc
    Member

    The issue doesn't say which errors you want to handle, so here is what SolidStart 2.x offers today. I tested each case in dev on 2.x. I have not checked 1.x, where middleware has no next(), so none of this applies there.

    Where the error is thrown How to handle it on 2.x
    API routes, and any middleware that runs after yours an h3 middleware that wraps await next() in try/catch
    server functions the serverFunctions.onError option
    rendering a page only visible as a 500 status after next(); the error itself does not reach the middleware
    // src/middleware.ts
    import { createMiddleware } from "@solidjs/start/middleware";
    
    export default createMiddleware([
      async (event, next) => {
        try {
          const response = await next();
          // A page that failed to render arrives here with a 500 status, but
          // without the error object.
          if (event.res.status >= 500) reportFailedPage(event.url.pathname);
          return response;
        } catch (error) {
          // Errors thrown in API routes and in later middleware.
          reportError(error);
          return new Response("Internal Server Error", { status: 500 });
        }
      },
    ]);

    Server functions don't throw to the middleware, because their errors are sent to the caller. They have their own hook, which runs before the error is sent:

    // vite.config.ts
    solidStart({ serverFunctions: { onError: "./src/server-fn-error.ts" } });
    
    // src/server-fn-error.ts
    import type { ServerFunctionErrorHandler } from "@solidjs/start/server";
    
    const onServerFunctionError: ServerFunctionErrorHandler = thrown => {
      if (thrown instanceof Response) return thrown; // keep redirect() and other control flow
      reportError(thrown);
    };
    
    export default onServerFunctionError;

    On 2.x, the onRequest / onBeforeResponse object form of createMiddleware is deprecated in favor of h3 middleware like the one above, so a new onError key there is unlikely. The remaining gap is getting the error object when a page fails to render.

    @emogua, which errors do you need to handle, and on which SolidStart version? If you need page render errors on the server, or you are on 1.x, a small example of what you're trying to report would help decide what to add.

  2. lxsmnsyc commented on Oct 18, 2026

    @lxsmnsyc
    Member

    Closing, as we don't plan to add an onError option to createMiddleware.

    On 2.x, the object form of createMiddleware (onRequest / onBeforeResponse) is deprecated in favor of h3 middleware. The approaches in my previous comment cover errors from API routes, later middleware and server functions.

    If you need something those don't cover, please open a new issue with the errors you want to handle, the SolidStart version, and a small example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions