Skip to content

Add APIError class to preserve structured error data from handleResponse#51

Merged
0237h merged 2 commits into
mainfrom
copilot/fix-handle-response-error-structure
Mar 18, 2026
Merged

Add APIError class to preserve structured error data from handleResponse#51
0237h merged 2 commits into
mainfrom
copilot/fix-handle-response-error-structure

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 18, 2026

handleResponse was serializing the structured API error object (with status, code, message fields) into a plain Error message string, forcing consumers to re-parse JSON to distinguish error types.

Changes

  • New APIError class — exported, extends Error, carries status: number and code: string directly on the instance
  • Updated handleResponse — detects structured error shape (status/code/message) and throws APIError; falls back to plain Error for unstructured errors (preserving existing behavior)
  • Tests — added coverage for APIError construction, and structured 401/404/429 responses asserting correct instanceof, status, and code

Usage

try {
  await client.evm.tokens.getHolders({ ... });
} catch (e) {
  if (e instanceof APIError) {
    if (e.status === 429) { /* retry */ }
    if (e.status === 404) { /* not found */ }
  } else {
    // Network error — TypeError/Error from fetch (DNS, socket, timeout)
  }
}
Original prompt

This section details on the original issue you should resolve

<issue_title>handleResponse loses structured error data by stringifying into Error message</issue_title>
<issue_description>handleResponse currently does:

token-api-sdk/src/index.ts

Lines 226 to 234 in 62cfac5

function handleResponse<T>(data: T | undefined | null, error: unknown): T {
if (error) {
throw new Error(`API Error: ${JSON.stringify(error)}`);
}
if (data === undefined || data === null) {
throw new Error('API Error: No data returned');
}
return data;
}

The error object from openapi-fetch is the parsed JSON error body with structured fields (status, code, message), but this gets flattened into a string. Consumers who need to distinguish error types (e.g., retry on 429 but not on 404) have to parse JSON back out of the error message.

Suggestion

Throw a typed APIError that preserves the structured data:

export class APIError extends Error {
    status: number;
    code: string;

    constructor(error: { status: number; code: string; message: string }) {
        super(error.message);
        this.name = 'APIError';
        this.status = error.status;
        this.code = error.code;
    }
}

A single error class is enough — the SDK only catches API-level errors in handleResponse. Network errors (DNS, socket, timeout) from fetch already propagate as native TypeError/Error and don't go through handleResponse. Consumers can distinguish them with instanceof APIError.

try {
    await client.evm.tokens.getHolders({ ... });
} catch (e) {
    if (e instanceof APIError) {
        // API error — e.status, e.code available
        if (e.status === 429) { /* retry */ }
        if (e.status === 404) { /* not found */ }
    } else {
        // Network error — DNS, socket, timeout
    }
}
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Co-authored-by: 0237h <23462475+0237h@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix handleResponse to preserve structured error data Add APIError class to preserve structured error data from handleResponse Mar 18, 2026
Copilot AI requested a review from 0237h March 18, 2026 15:35
@0237h 0237h marked this pull request as ready for review March 18, 2026 15:43
@0237h 0237h merged commit 10a2f04 into main Mar 18, 2026
1 check passed
0237h added a commit that referenced this pull request Mar 18, 2026
…-error-structure

Add `APIError` class to preserve structured error data from `handleResponse`
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.

handleResponse loses structured error data by stringifying into Error message

2 participants