Conversation
|
There was a problem hiding this comment.
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>
📝 WalkthroughWalkthroughPackage 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
package.jsonsrc/api-client.ts
| const responseText = await response.text(); | ||
| return Promise.reject(responseText); |
There was a problem hiding this comment.
🧩 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 -50Repository: 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 -20Repository: 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.
| 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).
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
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
Chores