question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error handling x middleware regression since 2.1.3

See original GitHub issue

👋 In versions 2.1.3 and beyond, there is a regression in how errors interact with middleware…

In version 2.1.2 and prior, if an error occurred in a route and app.onError was invoked, middleware would still be processed for the error response. This would allow middleware such as the built-in logger and built-in cors to still process the error response, logging the response or injecting CORS headers.

In version 2.1.3 and since, if an error occurs and app.onError responds, middleware does not have a chance to process the response, so middleware such as logger cannot log the response, and cors cannot inject any CORS headers.

const { Hono } = require('hono');
const { logger } = require('hono/logger');
const { cors } = require('hono/cors');

const app = new Hono();
app.use('*', logger());
app.use('*', cors({ origin: '*' }));

app.get('/error', () => {
  throw new Error('Test error');
});

app.onError((err, ctx) => {
  return ctx.json({
    error: true,
    status: 500,
    message: err.message,
  });
});

const main = async () => {
  const res = await app.request('http://localhost/error');
  console.log([ ...res.headers.entries() ]);
};

main();

RunKit demo: hono@latest

RunKit demo: hono@2.1.2

This is pretty breaking for my project, as I would like to have error responses logged, and I’d like them to contain CORS headers like any other response.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
yusukebecommented, Oct 4, 2022

Hi @MattIPv4 !

Sorry! Fixed it and release a new version, “v2.2.5”. Please try it and if you still have problems, tell us again!

0reactions
MattIPv4commented, Oct 3, 2022

Hey! It looks like it’s been fixed for synchronous routes 🎉, but the issue still remains for any route that is asynchronous 😢

2.1.2

2.2.4

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core Web API exception handling - Stack Overflow
Quick and Easy Exception Handling. Simply add this middleware before ASP.NET routing into your middleware registrations. app.UseExceptionHandler(c => c.
Read more >
Handle errors in ASP.NET Core | Microsoft Learn
To configure a custom error handling page for the Production environment, call UseExceptionHandler. This exception handling middleware:.
Read more >
Global Error Handling in ASP.NET Core Web API - Code Maze
In this article, we are going to handle errors by using a try-catch block first and then rewrite our code by using built-in...
Read more >
connect/HISTORY.md at master · senchalabs/connect - GitHub
Connect is a middleware layer for Node.js. ... deps: debug@~2.1.3 ... Prevent default 404 handler after response sent; dep: debug@0.8.1 ...
Read more >
Changes — Werkzeug Documentation (2.2.x)
The debugger shows enhanced error locations in tracebacks in Python 3.11. ... Resolved a regression with status code handling in the integrated development ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found