cloudflare / cloudflare/workers-rs

[BUG] Error::Internal does not provide information for debugging and causes panic

Open
#214 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
3.7k
Forks
429
Avg merge
20h 28m
Merged PRs (30d)
7

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### What version of `workers-rs` are you using?

0.0.10

### Describe the bug

For example, if we try to return status code 200 using `Response::error`, we will get the following error.
And the response is not returned in local mode (error page is showed if it is deployed).

```
Tue Aug 16 2022 22:42:14 GMT+0900 (日本標準時) - [/error], located at: (35.6918, 139.7002), within: Tokyo
unrecognized JavaScript object
panicked at 'unrecognized JavaScript object', src/lib.rs:15:1

Stack:

Error:
at __wbg_new_693216e109162396 (/Users/pddg/ghq/github.com/pddg/workers-rs-bug/build/worker/shim.mjs:375:17)
at null. (wasm://wasm/001a111a:1:136213)
at null. (wasm://wasm/001a111a:1:188695)
at null. (wasm://wasm/001a111a:1:151816)
at null. (wasm://wasm/001a111a:1:163719)
at null. (wasm://wasm/001a111a:1:183082)
at null. (wasm://wasm/001a111a:1:183552)
at null. (wasm://wasm/001a111a:1:181849)
at null. (wasm://wasm/001a111a:1:105036)
at null. (wasm://wasm/001a111a:1:161450)

[mf:err] Unhandled Promise Rejection: RuntimeError: unreachable
at null. (wasm://wasm/001a111a:1:151874)
at null. (wasm://wasm/001a111a:1:163719)
at null. (wasm://wasm/001a111a:1:183082)
at null. (wasm://wasm/001a111a:1:183552)
at null. (wasm://wasm/001a111a:1:181849)
at null. (wasm://wasm/001a111a:1:105036)
at null. (wasm://wasm/001a111a:1:161450)
at null. (wasm://wasm/001a111a:1:123938)
at null. (wasm://wasm/001a111a:1:160737)
at null. (wasm://wasm/001a111a:1:136594)
```

Showing following error string is expected, I think.
https://github.com/cloudflare/workers-rs/blob/afe02479cf9cef0428d0e1a84384ff316be056e3/worker/src/response.rs#L160-L162

However, the error string is not shown always since `Error::Internal` only shows `unrecognized JavaScript object`.
https://github.com/cloudflare/workers-rs/blob/afe02479cf9cef0428d0e1a84384ff316be056e3/worker/src/error.rs#L41

I see two problems here.

1. The worker is crashed when the `Error::Intenal` is returned. In local mode, it never returns a response.
2. The user cannot see the correct error message and cannot determine the cause until they reads the source code.

### Steps To Reproduce

```
❯ wrangler version
⛅️ wrangler 2.0.25
--------------------
```

The minimum source code I verified is as follows.

```
# create project
npm init cloudflare workers-rs-bug worker-rust
cd workers-rs-bug

# Fill name parameter
vim wrangler.toml

# Write codes
cat << 'EOF' > src/lib.rs
use worker::*;

mod utils;

fn log_request(req: &Request) {
console_log!(
"{} - [{}], located at: {:?}, within: {}",
Date::now().to_string(),
req.path(),
req.cf().coordinates().unwrap_or_default(),
req.cf().region().unwrap_or("unknown region".into())
);
}

#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result {
log_request(&req);
utils::set_panic_hook();
let router = Router::new();
router
.get("/", |_, _| Response::ok("Hello from Workers!"))
.get("/error", |_, _| {
Response::error("some error", 200)
})
.run(req, env)
.await
}
EOF
```

Run the worker in local mode.

```
wrangler dev -l
```

Then, try to curl.

```
curl http://localhost:8787/error
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.