workers-rs icon indicating copy to clipboard operation
workers-rs copied to clipboard

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

Open pddg opened this issue 2 years ago • 0 comments

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.<anonymous> (wasm://wasm/001a111a:1:136213)
    at null.<anonymous> (wasm://wasm/001a111a:1:188695)
    at null.<anonymous> (wasm://wasm/001a111a:1:151816)
    at null.<anonymous> (wasm://wasm/001a111a:1:163719)
    at null.<anonymous> (wasm://wasm/001a111a:1:183082)
    at null.<anonymous> (wasm://wasm/001a111a:1:183552)
    at null.<anonymous> (wasm://wasm/001a111a:1:181849)
    at null.<anonymous> (wasm://wasm/001a111a:1:105036)
    at null.<anonymous> (wasm://wasm/001a111a:1:161450)


[mf:err] Unhandled Promise Rejection: RuntimeError: unreachable
    at null.<anonymous> (wasm://wasm/001a111a:1:151874)
    at null.<anonymous> (wasm://wasm/001a111a:1:163719)
    at null.<anonymous> (wasm://wasm/001a111a:1:183082)
    at null.<anonymous> (wasm://wasm/001a111a:1:183552)
    at null.<anonymous> (wasm://wasm/001a111a:1:181849)
    at null.<anonymous> (wasm://wasm/001a111a:1:105036)
    at null.<anonymous> (wasm://wasm/001a111a:1:161450)
    at null.<anonymous> (wasm://wasm/001a111a:1:123938)
    at null.<anonymous> (wasm://wasm/001a111a:1:160737)
    at null.<anonymous> (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<Response> {
    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

pddg avatar Aug 16 '22 14:08 pddg