Skip to content

Conversation

@TaprootFreak
Copy link
Contributor

@TaprootFreak TaprootFreak commented Jan 2, 2026

Summary

Replace plain ApiError objects with ApiException class that extends Error, so error handlers can properly display the message.

Problem

The previous implementation threw plain objects {statusCode, message}. When these objects were caught by error handlers (webpack-dev-server overlay, browser console), they were displayed as [object Object] instead of the actual error message.

Solution

1. ApiException class

export class ApiException extends Error implements ApiError {
  constructor(
    public readonly statusCode: number,
    message: string,
  ) {
    super(message);
    this.name = 'ApiException';
    Object.setPrototypeOf(this, ApiException.prototype);
  }
}

Features:

  • Extends Error → proper .message, .stack, toString()
  • Implements ApiError → backward compatible
  • Object.setPrototypeOf → ES5 prototype chain fix

2. Network error handling

.catch((error: unknown) => {
  const message = error instanceof Error ? error.message : String(error);
  throw new ApiException(0, `Network error: ${message}`);
})

3. API error handling with fallbacks

return response.json()
  .catch(() => null)
  .then((body: Partial<ApiError> | null) => {
    throw new ApiException(
      body?.statusCode ?? response.status,
      body?.message ?? response.statusText ?? 'Unknown error',
    );
  });

Edge cases handled:

Server Response statusCode message
{"statusCode": 400, "message": "Error"} 400 "Error"
{"error": "Bad"} response.status response.statusText
["error"] response.status response.statusText
"string" response.status response.statusText
null response.status response.statusText
HTML/invalid JSON response.status response.statusText

Test Plan

  • Network error → Network error: Failed to fetch
  • API error → actual error message
  • Non-JSON response → HTTP status text
  • error.statusCode works
  • error.message works
  • error instanceof Errortrue
  • error instanceof ApiExceptiontrue

@TaprootFreak TaprootFreak force-pushed the feature/api-exception-class branch 2 times, most recently from dca0833 to 45adbe8 Compare January 2, 2026 07:28
Replace plain objects with Error subclass so that error handlers
(webpack-dev-server overlay, browser console, logging tools) can
properly display the error message instead of [object Object].

ApiException extends Error and implements ApiError, maintaining
backward compatibility with existing error handling code.

Changes:
- Add ApiException class extending Error
- Add Object.setPrototypeOf fix for ES5 compatibility
- Wrap network errors in ApiException
- Wrap API error responses in ApiException with fallback handling
@TaprootFreak TaprootFreak force-pushed the feature/api-exception-class branch from 45adbe8 to e14b6c5 Compare January 2, 2026 07:30
@TaprootFreak TaprootFreak merged commit 42a7a53 into develop Jan 2, 2026
1 check passed
@TaprootFreak TaprootFreak deleted the feature/api-exception-class branch January 2, 2026 07:34
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.

2 participants