Skip to content

fix: replace ascii codec with safe UTF-8 decode for JSON responses (#1298)#1429

Draft
jayy-77 wants to merge 1 commit intoCrowdStrike:devfrom
jayy-77:fix/ascii-codec-decode-error-1298
Draft

fix: replace ascii codec with safe UTF-8 decode for JSON responses (#1298)#1429
jayy-77 wants to merge 1 commit intoCrowdStrike:devfrom
jayy-77:fix/ascii-codec-decode-error-1298

Conversation

@jayy-77
Copy link

@jayy-77 jayy-77 commented Feb 11, 2026

What is the purpose of this change?

Fixes #1298'ascii' codec can't decode byte 0xc3 error when the API returns JSON with non-ASCII characters (accented names like Ángel, Müller).

Root cause: calc_content_return() used resp.content.decode("ascii") as a JSON fallback, which crashes on any byte > 127 (common in UTF-8 multi-byte sequences). The UnicodeDecodeError propagated to the catch-all handler and was masked as a generic SDKError with code 500.

What does this change do?

  • Adds _safe_decode_content() helper (UTF-8 → latin-1 fallback)
  • Replaces decode("ascii") with safe decode in calc_content_return()
  • Adds explicit UnicodeDecodeError handler with ContentDecodingError
  • Adds ContentDecodingError exception with encoding/position diagnostics
  • Adds encoding context to NoContentWarning
  • Adds _safe_decode_body() to BaseResult for encoding-aware parsing
  • Makes Uber class error extraction robust against non-dict errors

Files changed (6 source + 1 test)

  • src/falconpy/_util/_functions.py
  • src/falconpy/_error/_exceptions.py
  • src/falconpy/_error/__init__.py
  • src/falconpy/_error/_warnings.py
  • src/falconpy/_result/_result.py
  • src/falconpy/api_complete/_legacy.py
  • tests/test_ascii_decode_fix.py (28 tests)

…rowdStrike#1298)

When the CrowdStrike API returns a JSON response containing non-ASCII
characters (e.g. accented names like Ángel, Müller, François), the SDK
called resp.content.decode('ascii') which crashed with:

  'ascii' codec can't decode byte 0xc3

The UnicodeDecodeError propagated to the catch-all handler and was
wrapped as a generic SDKError with code 500, masking the actual
response data.

Modified source files (5):
- src/falconpy/_util/_functions.py: Add _safe_decode_content() helper
  that tries UTF-8 then latin-1; replace decode('ascii'); add explicit
  UnicodeDecodeError handler with ContentDecodingError
- src/falconpy/_error/_exceptions.py: Add ContentDecodingError with
  encoding/position diagnostics
- src/falconpy/_error/__init__.py: Export ContentDecodingError
- src/falconpy/_error/_warnings.py: Add encoding property to
  NoContentWarning for decode failure context
- src/falconpy/_result/_result.py: Add _safe_decode_body() for
  encoding-aware body parsing in Result construction
- src/falconpy/api_complete/_legacy.py: Robust error extraction in
  Uber class authenticate()

Tests: 28 edge-case unit tests in tests/test_ascii_decode_fix.py
@jshcodes jshcodes assigned jshcodes and jayy-77 and unassigned jshcodes Feb 13, 2026
@jshcodes jshcodes added unit testing Pull requests that include unit testing updates python Pull requests that update Python code or general Python related questions API usage General API usage issues and questions labels Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API usage General API usage issues and questions python Pull requests that update Python code or general Python related questions unit testing Pull requests that include unit testing updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants