Skip to content

Replace HTTP status string parsing with structured errors #79

Description

@Faerkeren

Problem

REST error handling depends on parsing exception message text.

src/haclient/infra/rest_aiohttp.py:131-133 converts every non-401 HTTP error into a generic HAClientError string. get_state() then detects missing entities by checking whether "HTTP 404" appears in str(err) at src/haclient/infra/rest_aiohttp.py:186-190.

That is brittle. If the message format changes, or another error body happens to include that text, get_state() behavior changes.

Suggested Fix

Introduce a structured HTTP exception, for example HTTPError(status: int, method: str, path: str, body: str), derived from HAClientError.

Then:

  • Raise HTTPError from _request() for HTTP failures.
  • Check err.status == 404 in get_state().
  • Keep authentication mapped to AuthenticationError.
  • Add tests that get_state() returns None only for real 404 responses.

Rationale

Status codes are structured data. Encoding them into strings and parsing them back out is avoidable fragility in a reliability-sensitive client.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:reliabilityReliability, error handling, and graceful degradationseverity:medMedium severitytype:bugBug fix or correctness issue

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions