Skip to content

Runtime agnostic error handler #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
cctidal opened this issue Apr 10, 2025 · 2 comments · Fixed by #48
Closed
1 task done

Runtime agnostic error handler #46

cctidal opened this issue Apr 10, 2025 · 2 comments · Fixed by #48
Labels
enhancement New feature or request

Comments

@cctidal
Copy link

cctidal commented Apr 10, 2025

Describe the feature

Currently you can specify an error handler for deno and bun by using runtime specific options (as described in https://srvx.unjs.io/guide/options). The nodejs runtime doesn't have a similar option.

// bun
serve({
  bun: {
    error(error) {
      return new Response(`<pre>${error}\n${error.stack}</pre>`, {
        headers: { "Content-Type": "text/html" },
      });
    },
  },
  fetch: () => new Response("👋 Hello there!"),
});

// deno
serve({
  deno: {
    onError(error) {
      return new Response(`<pre>${error}\n${error.stack}</pre>`, {
        headers: { "Content-Type": "text/html" },
      });
    },
  },
  fetch: () => new Response("👋 Hello there!"),
});

// nodejs
const server = serve({
  fetch(_request) {
    try {
      return new Response('👋 Hello there!');
    } catch (error) {
      return new Response(`<pre>${error}\n${error.stack}</pre>`, {
        headers: { 'Content-Type': 'text/html' },
      });
    }
  },
});

On nodejs we have to manually catch the error and return a response, but it's not as concise as the other runtime.

  • Proposal 1: Add an onError or similar option to the serve api. This onError would be runtime agnostic and would work for deno, bun and nodejs.
serve({
  onError(error) {
    return new Response(`<pre>${error}\n${error.stack}</pre>`, {
      headers: { "Content-Type": "text/html" },
    });
  },
  fetch: () => new Response("👋 Hello there!"),
});
  • Proposal 2: Add an error hook as a plugin
export const errorHandler: ServerPlugin = (server) => {
  return {
    name: 'error-handler',
    error(error) {
      return new Response(`<pre>${error}\n${error.stack}</pre>`, {
        headers: { 'Content-Type': 'text/html' },
      });
    },
  };
};

serve({
  plugins: [errorHandler],
  fetch: () => new Response("👋 Hello there!"),
});

Additional information

  • Would you be willing to help implement this feature?
@cctidal cctidal added the enhancement New feature or request label Apr 10, 2025
@pi0
Copy link
Member

pi0 commented Apr 11, 2025

PR more than welcome! We can start from onError on top level serve

@danielpza
Copy link
Contributor

Created a PR #48

@pi0 pi0 closed this as completed in #48 Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants