Skip to content

Conversation

alex1xu
Copy link

@alex1xu alex1xu commented Aug 12, 2025

Problem

The newAPIError function in api_client.go has improper error handling that can cause nil pointer dereference panics.

When the API returns valid JSON that doesn't match the expected responseWithError format (e.g., missing error field or error: null), the JSON unmarshaling still succeeds but leaves ErrorInfo as nil. The code then dereferences this nil pointer with *respWithError.ErrorInfo, causing a "invalid memory address or nil pointer dereference" panic.

Failure Cases

  1. API returns {"some_field": "value"} (valid JSON, no error field). JSON unmarshaling succeeds → ErrorInfo is nil. Code executes return *respWithError.ErrorInfo → nil pointer dereference panic
  2. API returns {"error": null, "other": "data"} (valid JSON, explicit null error). JSON unmarshaling succeeds → ErrorInfo is nil. Code executes return *respWithError.ErrorInfo → nil pointer dereference panic

Fix

Add explicit nil check before dereferencing ErrorInfo

The newAPIError function was causing nil pointer dereference panics when
the API returned valid JSON that didn't match the expected error format
(e.g., missing 'error' field or 'error: null'). This happened because
the code would unmarshal JSON successfully but then dereference a nil
ErrorInfo pointer.

Add explicit nil check before dereferencing ErrorInfo to prevent panics
and provide meaningful error messages for unexpected response formats.
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.

1 participant