Skip to content

[Fix] Custom fetch error message #13

Open
BrettCleary wants to merge 4 commits intostakekit:mainfrom
BrettCleary:fix/custom_fetch_error_message
Open

[Fix] Custom fetch error message #13
BrettCleary wants to merge 4 commits intostakekit:mainfrom
BrettCleary:fix/custom_fetch_error_message

Conversation

@BrettCleary
Copy link

@BrettCleary BrettCleary commented Nov 6, 2025

The response object is being returned on API errors instead of a human readable error message.

This means the calling code has to call await error.text() in the surrounding catch block, which is an anti-pattern since other functions in the try block could throw a different type.

If you call

console.log(`Error: ${error}`)

you get something like Error: [Response object].

With this change, this log code in the catch block now gives something readable like the following without having to assume the error object is a response object at runtime
{"statusCode":400,"timestamp":"2025-11-06T02:17:35.478Z","path":"/v1/actions/enter","message":"Value must be at least 0.1"}

Summary by CodeRabbit

Release Notes v0.0.7

  • Bug Fixes

    • Improved error handling for API requests with clearer error messages.
  • Chores

    • Version bumped to 0.0.7.

Copilot AI review requested due to automatic review settings November 6, 2025 03:29
@changeset-bot
Copy link

changeset-bot bot commented Nov 6, 2025

⚠️ No Changeset found

Latest commit: f85b547

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

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

Copy link

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 updates the SDK version to 0.0.5 and improves error handling in the API client by extracting response text from failed HTTP requests before rejecting the promise.

  • Modified error handling to read and return response text instead of the raw response object
  • Bumped package version from 0.0.4 to 0.0.5

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/api-client.ts Enhanced error handling to extract and return response text for failed requests
package.json Incremented version number to 0.0.5

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

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
jdomingos
jdomingos previously approved these changes Nov 6, 2025
@coderabbitai
Copy link

coderabbitai bot commented Mar 9, 2026

📝 Walkthrough

Walkthrough

Package version incremented from 0.0.6 to 0.0.7. API client error handling modified to reject with response text instead of the Response object, altering the rejection payload type from Response to string.

Changes

Cohort / File(s) Summary
Version Management
package.json
Version bumped from 0.0.6 to 0.0.7.
Error Handling
src/api-client.ts
Modified customFetch error path to reject with response text (awaited via response.text()) instead of the Response object itself.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A little hop, a version bump so small,
Errors now whisper as strings through the hall,
No more Response objects tangled in mess,
Just plain text rejections—much cleaner, much best!
From point-oh-six dancing to point-oh-seven's song! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title '[Fix] Custom fetch error message' accurately describes the main change: modifying error handling in the custom fetch function to return readable error messages instead of Response objects.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/api-client.ts`:
- Around line 44-45: The code currently rejects the promise with a bare string
(responseText); replace that with an Error instance that includes the body text
as the message and attach response metadata so callers can inspect
status/headers/body. In the function that reads responseText (the
customFetch/response handling code in src/api-client.ts), create const err = new
Error(responseText); then set (err as any).status = response.status; (err as
any).headers = Object.fromEntries(response.headers); (err as any).body =
responseText; and then throw err (or return Promise.reject(err)) instead of
Promise.reject(responseText).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d34e0eed-c248-4886-8c95-48d5762fde62

📥 Commits

Reviewing files that changed from the base of the PR and between 3e1a61d and f85b547.

📒 Files selected for processing (2)
  • package.json
  • src/api-client.ts

Comment on lines +44 to +45
const responseText = await response.text();
return Promise.reject(responseText);
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# Check src/api-client.ts lines 44-45
echo "=== src/api-client.ts lines 44-45 ==="
sed -n '40,50p' src/api-client.ts

# Check orval.config.ts to verify customFetch setup
echo -e "\n=== orval.config.ts (mutator config) ==="
cat src/orval.config.ts 2>/dev/null || cat orval.config.ts 2>/dev/null || echo "Not found at expected location"

# Check src/index.ts error handling
echo -e "\n=== src/index.ts lines 18-28 ==="
sed -n '18,28p' src/index.ts

# Look for customFetch definition
echo -e "\n=== customFetch definition ==="
grep -r "customFetch" --include="*.ts" -B 2 -A 10 | head -50

Repository: stakekit/sdk

Length of output: 4473


🏁 Script executed:

# Get full customFetch function
echo "=== Full customFetch function ==="
sed -n '/^export const customFetch/,/^};$/p' src/api-client.ts

# Check a generated API function to see how it handles errors
echo -e "\n=== Sample generated API function ==="
grep -A 15 "export const getYields" src/api/index.ts | head -20

Repository: stakekit/sdk

Length of output: 1990


Don't reject a bare string from the public client.

When an API request fails, this code rejects the promise with a plain string instead of an Error object. SDK consumers will catch a string directly, breaking standard JavaScript error handling: instanceof Error checks fail, .stack and .message properties don't exist, and response metadata (status, headers) is lost unless embedded in the body text. Since customFetch is wired directly as the Orval mutator and src/index.ts includes no error wrapper layer, that raw string escapes to every caller unchanged.

Throw an Error object that preserves both the readable body text and response metadata:

Suggested fix
-  const responseText = await response.text();
-  return Promise.reject(responseText);
+  const responseText = await response.text();
+  throw Object.assign(
+    new Error(responseText || `HTTP ${response.status}`),
+    {
+      name: "ApiError",
+      status: response.status,
+      response,
+      body: responseText,
+    },
+  );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const responseText = await response.text();
return Promise.reject(responseText);
const responseText = await response.text();
throw Object.assign(
new Error(responseText || `HTTP ${response.status}`),
{
name: "ApiError",
status: response.status,
response,
body: responseText,
},
);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/api-client.ts` around lines 44 - 45, The code currently rejects the
promise with a bare string (responseText); replace that with an Error instance
that includes the body text as the message and attach response metadata so
callers can inspect status/headers/body. In the function that reads responseText
(the customFetch/response handling code in src/api-client.ts), create const err
= new Error(responseText); then set (err as any).status = response.status; (err
as any).headers = Object.fromEntries(response.headers); (err as any).body =
responseText; and then throw err (or return Promise.reject(err)) instead of
Promise.reject(responseText).

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