-
-
Notifications
You must be signed in to change notification settings - Fork 249
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
Local Error example in docs does not work #966
Comments
The documentation is incorrect.
The code here is using return which is an intended HTTP error to return a client. So it's not technically an The code should be using The documentation should be fixed with this commit. However, it can be proposed that this behavior be changed in the future. |
@SaltyAom Thanks for the explanation. From trying it out, it seems that the As a result, if I do this: throw error("I'm a teapot", { message: "meow!" }); Without setting HTTP/1.1 418 I'm a Teapot
content-type: text/plain;charset=utf-8
Date: Wed, 25 Dec 2024 09:38:44 GMT
Content-Length: 15
[object Object] I expected a JSON-stringified response, but instead of that I get It seems that Elysia doesn’t have default handling when Update: I created a proposal to make this idiomatic: |
This is a bug which should have been fixed with b94dab2, published under 1.2.7 |
@dtinth starting from 1.2.7, Which mean this works: new Elysia()
.onError(({ error, code }) => {
switch(code) {
case 418:
console.log(error)
}
})
.get('/', () => {
throw error("I'm a teapot")
})
.listen(3000) While this is not: new Elysia()
.onError(({ error, code }) => {
switch(code) {
case 418:
console.log(error)
}
})
.get('/', () => {
return error("I'm a teapot")
})
.listen(3000) Alternatively, we can use
|
@SaltyAom Thanks, I confirm that it works now, but now plain import { Elysia } from "elysia";
new Elysia()
.get("/", async () => {
throw new Error("nyan");
})
.listen(3000); curl -D- localhost:3000 HTTP/1.1 200 OK
content-type: text/plain;charset=utf-8
Date: Fri, 27 Dec 2024 12:59:27 GMT
Content-Length: 4
nyan |
thx for new Error('a') |
@SaltyAom Thanks, I confirm that the status is indeed fixed to be 500 now. However, there are 2 behavior differences between these 2 versions:
I’m okay either way, just want to make sure that you intend that not to call I think your project may benefit from having more snapshot testing that tests Elysia from a user’s (i.e. an outsider) perspective. I see that you use In my Elysia by Example project, I have a collection of example code and maintain a set of curl command and their expected output. That’s how I detected this issue right after you release a new version. Feel free to take the code if you’d like, although I’m still improving the examples and snapshot format. Here’s the snapshot diff between v1.2.6 and v1.2.8: |
This behaviour is preventing me from using version 1.2 at this stage. I can't intercept the response in an |
@awilderink Can you share your example case? I'm curious to see how you use it. |
What version of Elysia is running?
1.2.2
What platform is your computer?
Darwin 24.1.0 arm64 arm
What steps can reproduce the bug?
Run the example from https://elysiajs.com/essential/life-cycle.html#local-error
The if clause is removed to simulate an error case.
What is the expected behavior?
"Handled"
What do you see instead?
The
error
hook is not called, got "Unauthorized" instead.Additional information
No response
Have you try removing the
node_modules
andbun.lockb
and try again yet?No response
The text was updated successfully, but these errors were encountered: