Repository navigation
Answer unknown API paths and uncaught errors with JSON - #438
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
Neither the backend app nor the gateway app set notFound or onError, so
both fell back to Hono's plain-text defaults: "404 Not Found" for a path
no route serves and "Internal Server Error" for an error no route
caught. Every other error is JSON, so a client reading the body as JSON
failed a second time on these.
installJsonErrorHandlers (src/http-errors.ts) gives both apps the same
last-resort answers: 404 {"error":"not_found"} and 500
{"error":"internal_error"}. The 500 is logged with the surface, method
and path. The path goes through the secret redactor first, since a
preview view path carries a signed capability. An HTTPException keeps
the response it carries, as with Hono's default handler. Routers mounted
with app.route() set no handler of their own, so their errors and
unknown paths reach these handlers too.
The API overview in the docs now states the error shape.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
By opening this pull request you agree to the Contributor License Agreement. On your first pull request a bot asks you to sign it with a one-line comment.
Fixes #215
Neither the backend app nor the gateway app set
notFoundoronError, so both fell back to Hono's plain-text defaults:404 Not Foundfor a path no route serves, andInternal Server Errorfor an error no route caught. Every other error is JSON, so a client that reads the body as JSON failed a second time on these.What changed
backend/src/http-errors.tswithinstallJsonErrorHandlers(app, surface):404 {"error":"not_found"}.500 {"error":"internal_error"}and is logged as[<surface>] <METHOD> <path> failed:followed by the error.HTTPException, which Hono middleware throws on purpose, keeps the response it carries, as Hono's default handler does.createSecretRedactor([])fromsecrets/redact.ts. A preview view path carries a signed capability (/api/port-proxy/:threadId/view/:capability/*), and that must not reach the logs. The redactor replaces it with<redacted>and keeps the rest of the path.src/index.tsinstalls it on the backend app (api), andsrc/gateway-app.tson the gateway app (gateway). Routers mounted withapp.route()set no handler of their own, so their errors and unknown paths reach these handlers too.docs-site/docs/api/index.mdx: a "JSON errors" line under Conventions.Tests
src/http-errors.test.ts, on a Hono app with the handlers installed:application/json, logged once as[test] GET /api/boom failed:with the error<redacted>HTTPExceptionkeeps its status and body, and nothing is loggedsrc/gateway-app.test.ts:createGatewayApp()answers an unknown path with a JSON 404.500 {"error":"internal_error"}, logged as[gateway] GET /api/test-only-crash failed:.test/api-errors.test.ts: through the real backend app and auth adapter, a signed-inGET /api/does-not-existandGET /api/runs/abc/no-such-viewanswer404withapplication/jsonand{"error":"not_found"}.The gateway and backend tests fail on
main, where the answers are plain text.Checks with Bun 1.3.14:
bun run typecheck: passesbun test scripts/agent-docs.test.ts: 5 passcd backend && bun run test: 3569 pass, 28 fail.src/engines/runtime-*,codex-*andsrc/provider-gateway/sandbox-config.test.ts. Those files fail the same way on an unmodifiedmaincheckout on my machine, because they probe local processes, ports and the filesystem.src/runs/thread-relationship.test.ts"singular pumps once on create...", counts everyconsole.errorwhile it runs, and a stray log from earlier background work landed in that window once. It passes 3 of 3 runs alone on both this branch andmain, and 2 of 2 runs after the files that ran before it in the suite.End to end
I ran the backend (
src/index.ts) and the gateway (src/gateway.ts) locally on Postgres 16 with pgvector, signed in, and called them with curl:The last call is a real unhandled error: the malformed id reaches a uuid column, which is #207. Before this change it answered plain-text
Internal Server Error. The backend log now names it:[api] PATCH /api/tasks/abc failed:followed by the database error. Routes that already catch their own errors keep their answers, for exampleGET /api/knowledge/documents/abcstill returns its own500 {"error":"get failed"}.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.