Skip to content

Conversation

@CahidArda
Copy link
Collaborator

@CahidArda CahidArda commented Oct 23, 2025

after we made it possible to return a custom response in failure function, serve methods we wrote for nextjs-pages/express started to get errors if failure function was executed

This is because these handlers try to parse the response into a JSON before passing this body to their own respective response objects.

Our fix will be always passing a json parsable object to the response if failure function runs in the { result: <failureFunctionResonse>} format, where <failureFunctionResonse> is a string.

Error Handling

While checking this issue in express/nextjs-pages examples, I noticed that we weren't returning the correct response status (489) when non retryable error was thrown. 500 was being thrown instead.

I noticed that this was happening because instanceof didn't work as expected in some frameworks. So replaced the error instanceof SomeError usages with a helper I borrowed from here

@linear
Copy link

linear bot commented Oct 23, 2025

@CahidArda CahidArda requested a review from Copilot October 27, 2025 07:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a JSON parsing issue that occurred when failure functions returned custom responses in Next.js Pages Router and Express handlers. The changes ensure all responses are JSON-parsable by wrapping failure function results in a { result: <value> } format.

Key changes:

  • Replaced instanceof checks with a custom isInstanceOf helper function across the codebase
  • Modified failure callback response handling to always return JSON-parsable objects
  • Added test coverage for failure function responses (empty and string returns)

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/error.ts Added isInstanceOf helper function to handle instance checks across different contexts
src/serve/options.ts Wrapped failure callback results in JSON object format
src/serve/index.ts Wrapped error messages in JSON format and replaced instanceof check
src/workflow-requests.ts Replaced instanceof checks with isInstanceOf helper
src/context/auto-executor.ts Replaced instanceof checks with isInstanceOf helper
src/client/utils.ts Replaced instanceof checks with isInstanceOf helper
src/agents/agent.ts Replaced instanceof checks with isInstanceOf helper
src/agents/adapters.ts Replaced instanceof checks with isInstanceOf helper
platforms/nextjs.ts Extracted response.json() to separate variable before passing to res.json()
platforms/express.ts Extracted response.json() to separate variable before passing to res.json()
examples/nextjs-pages/src/pages/api/ci.ts Added failure function test implementation with headers and return scenarios
examples/nextjs-pages/ci.test.ts Added tests for empty and string failure function responses
examples/express/api/index.ts Added failure function test implementation with headers and return scenarios
examples/express/ci.test.ts Added tests for empty and string failure function responses

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@CahidArda
Copy link
Collaborator Author

There was a type error in @types/express@5.0.4 which is released only 3 days ago. Tests before this release were passing (see #139).

Pinning the versions to @types/express@5.0.3 fixed the issue. I will investigate in detail if it comes up in the future

@CahidArda CahidArda merged commit ba628b6 into main Oct 28, 2025
22 checks passed
@CahidArda CahidArda deleted the DX-2077-failure-function-json-response branch October 28, 2025 08:56
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.

3 participants