microsoft / microsoft/typespec
[hsjs] Avoid calling error handlers twice in the same request
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
There are some cases where through composition of components, the JS server generator can try to respond twice in error conditions, for example:
- During body deserialization, client data is malformed, so the body deserializer responds with a 400.
- Body deserialization rejects, causing the route dispatch function to throw.
- The router catches the error, and tries to respond with a 5xx internal server error.
- The Node server runtime sees the response has already ended, and throws an error from the onInternalError handler.
- The router is out of `catch`es when the error-catching code itself errors, so the error bubbles up.
- The process gets an uncaught error and dies.
I think there are two things to do here:
1. Even if the error-handling code throws, the process shouldn't be killed.
2. Make all the error handlers run-once in a group. I.e. if you call `onInternalError` after `onInvalidRequest`, log a warning saying the `onInternalError` is ignored and don't try to respond again.
Contributor guide
Assessment
This issue has not been assessed yet.