Skip to content
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

Bun.serve throw Error log on async #1435

Closed
SaltyAom opened this issue Nov 1, 2022 · 7 comments · Fixed by #8169
Closed

Bun.serve throw Error log on async #1435

SaltyAom opened this issue Nov 1, 2022 · 7 comments · Fixed by #8169
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API

Comments

@SaltyAom
Copy link
Contributor

SaltyAom commented Nov 1, 2022

What version of Bun is running?

0.2.2

What platform is your computer?

Darwin 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct 9 20:15:09 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T6000 arm64 arm

What steps can reproduce the bug?

According to the doc, in the fetch function we can throw new Error("") then handle the error in error() method.

Bun.serve({
	port: 3000,
	fetch() {
		throw new Error('A')

		return new Response('A')
	},
	error() {
		return new Response('Handled')
	}
})

This works as expected, it produces no error log because it's handled via error().
Screenshot 2565-11-01 at 19 08 05

But when fetch is async, the request is finished but then there would be an error log.

Bun.serve({
	port: 3000,
	fetch: async () => {
		throw new Error('A')

		return new Response('A')
	},
	error() {
		return new Response('Handled')
	}
})

Screenshot 2565-11-01 at 19 07 29

This would have a problem with:

  1. Testing for custom error handling logic in a test environment using bun wiptest, the test will fail.
  2. Redundant logging for production usage.

How often does it reproduce? Is there a required condition?

This always happens when using fetch as async

What is the expected behavior?

Throwing error in Bun.serve should have the same behavior in both sync and async, and the test suite should not fail when an error is thrown when using async with error handler.

What do you see instead?

An error log.
Screenshot 2565-11-01 at 19 07 29

Additional information

No response

@SaltyAom SaltyAom added bug Something isn't working needs repro Needs an example to reproduce labels Nov 1, 2022
@Jarred-Sumner Jarred-Sumner removed the needs repro Needs an example to reproduce label Nov 3, 2022
@Electroid
Copy link
Contributor

@robobun

const server = Bun.serve({
  async fetch() {
    throw new Error("Oops!");
    return new Response("Unreachable");
  },
  error() {
    return new Response("Works!");
  }
});
const response = await server.fetch(new Request("http://localhost:3000/hello"));
console.log(await response.text());
server.stop();

@robobun
Copy link

robobun commented Nov 10, 2022

Here is the output of the code that was provided.

Code
const server = Bun.serve({
async fetch() {
throw new Error("Oops!");
return new Response("Unreachable");
},
error() {
return new Response("Works!");
}
});
const response = await server.fetch(new Request("http://localhost:3000/hello"));
console.log(await response.text());
server.stop();
1 | const server = Bun.serve({
2 | async fetch() {
3 | throw new Error("Oops!");
^
error: Oops!
at /tmp/bun-Mhl3Qw/15d9d3cc7dc3473f1da1f4401d79d210214e5d21a8426c25fd42a555ae544984.ts:3:10
at fetch (/tmp/bun-Mhl3Qw/15d9d3cc7dc3473f1da1f4401d79d210214e5d21a8426c25fd42a555ae544984.ts:2:16)
at /tmp/bun-Mhl3Qw/15d9d3cc7dc3473f1da1f4401d79d210214e5d21a8426c25fd42a555ae544984.ts:10:23

Code was run using the latest build of Bun.

@Jarred-Sumner
Copy link
Collaborator

we should check if this still happens

@SaltyAom
Copy link
Contributor Author

SaltyAom commented Mar 6, 2023

Thanks~ Doesn't happen on my end using Bun 0.5.7.

@alexdatsyuk
Copy link

Hey there! This issue is still reproducible on the 1.0 release. Please keep an eye on it. Thanks!

@danielstaleiny
Copy link

danielstaleiny commented Sep 19, 2023

bump, I can also reproduce the issue in 1.0.1 and 1.0.2 version of the bun.

@Electroid Electroid reopened this Sep 19, 2023
@Electroid Electroid added the bun.js Something to do with a Bun-specific API label Sep 19, 2023
@TiBianMod
Copy link

still reproducible on 1.0.8

any luck on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants