honojs / vite-plugins

Vite Plugins for Hono
https://hono.dev
134 stars 35 forks source link

feat(dev-server): leverage vite error handling #63

Closed marbemac closed 10 months ago

marbemac commented 10 months ago

TODO

Requires https://github.com/honojs/node-server/pull/131.


This PR adjusts the dev server so that it passes errors / unexpected conditions through to Vite. This allows us to take advantage of the standard Vite error overlays, stack traces, etc.

Before

Screenshot 2024-01-26 at 10 25 48 AM

After

Screenshot 2024-01-26 at 10 26 29 AM

Or if an error object is returned from the hono endpoint, stack trace is included.:

Screenshot 2024-01-26 at 10 38 09 AM

This is really helpful when rendering client apps in order to get the original stack trace and nice overlay for SSR errors, for example:

/**
 * The frontend app
 */
honoApp.get('*', async c => {
  try {
    // react, solid, etc
    const app = await renderClientApp(<App />);

    return new Response(app, { headers: { 'Content-Type': 'text/html' } });
  } catch (err: any) {
    // in dev, pass the error back to the vite dev server to display in the error overlay
    if (import.meta.env.DEV) return err;

    throw err;
  }
});
changeset-bot[bot] commented 10 months ago

🦋 Changeset detected

Latest commit: 3c1073b47ec22e459d807f078038b492f8e13324

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | --------------------- | ----- | | @hono/vite-dev-server | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

marbemac commented 10 months ago

updated to use @hono/node-server v1.7

yusukebe commented 10 months ago

Hi @marbemac

Nice! One thing. Could you run yarn changeset? This is a "minor bump".

marbemac commented 10 months ago

sure thing - all set

yusukebe commented 10 months ago

@marbemac

Thank! Let's go!