kwhitley / itty-durable

Cloudflare Durable Objects + Itty Router = shorter code
MIT License
251 stars 18 forks source link

Returning a Response from the durable with an error status code throws an exception #45

Open roy-armis opened 11 months ago

roy-armis commented 11 months ago

I'm returning from my durable object's function a response:

handleRequest:

    ...
     return new Response("Missing auth header", { status: 401 });
    ...

worker.js:

router
  .all("*", withDurables())
  .get("/counter/:counterId", async (request, env) => {
    const counter = Counter.get(id);
    return counter.handleRequest(request);
  });

export default {
  fetch: async (request, env, ctx) =>
    router
      .handle(request, env, ctx)
      .then(json)
      .catch(error)
};

I expect it the return as a message, but instead it throws an error: TypeError: Body has already been used. It can only be used once. Use tee() first if you need to read it twice.

seems like in proxy-durable.js/catchErrors the body is being used already, therefore i'm getting a 500 response