Skip to content

Add error handling for default get-messages request and message parsing #180

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

nickfujita
Copy link
Contributor

@nickfujita nickfujita commented Apr 16, 2025

Fixes issue where app crashes upon receiving a 40x response from default request to /get-messages.
Came across this while testing authentication with the agent, using the method prescribed in hono-agents

// or with authentication
app.use(
  "*",
  agentsMiddleware({
    options: {
      // onBeforeConnect: async (req) => { // original
      onBeforeRequest: async (req) => { // modified
        const token = req.headers.get("authorization");
        // validate token
        if (!token) return new Response("Unauthorized", { status: 401 });
      },
    },
  })
);
  • Adds error handling to default request to /get-messages, setting the error message on the hook return object, and calling the onError callback option if provided.
  • Adds the same error handling as the above in the places marked as TODO, to keep things consistent.

Copy link

changeset-bot bot commented Apr 16, 2025

🦋 Changeset detected

Latest commit: 80eee3e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
agents Patch
hono-agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Collaborator

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for starting the PR! What do you think of my recommendation?

credentials: options.credentials,
});
return response.json<Message[]>();
} catch (e) {
Copy link
Collaborator

@threepointone threepointone Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to actually throw the error here, and expect the user to add an ErrorBoundary

Copy link
Contributor Author

@nickfujita nickfujita Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that might throw off many users, as it did me, because it doesn't seem to be the expected behavior for a hook to throw an error upon receiving a 40x response. For example, when using tanstack-query the standard behavior isn't to throw an error if the request fails, but rather to communicate the issue in the form of an error property on the return object, and leaving data as undefined (which maybe we should do here instead of returning an empty array 🤔).
I don't think the underlying userPartySocket requires an ErrorBoundary either, but rather communitates errors via the onerror property in it's return object.

If there was a way to provide a typescript error when using this hook without a ErrorBoundary, that would probably make it more feasible, but I'm not currently aware of a way to add that.

Maybe adding a very clear note in the documentation that there needs to be an ErrorBoundary and updating the agent template repo might prevent users from running into this, but I just can't help but feel that there would be more people filing issues for this same thing in the future 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants