Skip to content

feat: add raise_errors option with typed exceptions - #2

Merged
alexandrecuer merged 3 commits into
Open-Building-Management:mainfrom
davidski:feat/raise-errors
Nov 16, 2026
Merged

alexandrecuer merged 3 commits into
Open-Building-Management:mainfrom
davidski:feat/raise-errors

Conversation

@davidski

@davidski davidski commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1

Summary

Opt-in raise_errors=True makes the client raise typed exceptions instead of returning {"success": False, ...}. The default behaviour is unchanged.

EmoncmsError                  # any other non-200
├── EmoncmsAuthError          # HTTP 401 (invalid API key)
└── EmoncmsConnectionError    # aiohttp ClientError or timeout
 async_request(path)
   GET url
-  ClientError / timeout  -> return {"success": False, "message": ...}
-  401                    -> return {"success": False, "message": "error 401 ..."}
+  ClientError / timeout  -> raise EmoncmsConnectionError   (if raise_errors)
+  401                    -> raise EmoncmsAuthError         (if raise_errors)
+  other non-200          -> raise EmoncmsError             (if raise_errors)
+  otherwise              -> same dict as today

 async_input_post(data)
+  uses request_timeout (previously had no timeout)
+  same exception mapping when raise_errors
   non-200 without raise_errors -> RuntimeError (unchanged)

async_get_uuid, async_list_feeds and async_get_feed_fields call async_request, so they raise too. When raising, the library logs at debug instead of error, leaving error reporting to the caller. This avoids duplicate log lines in Home Assistant (log-when-unavailable).

Home Assistant will use this to tell ConfigEntryAuthFailed (reauth) apart from ConfigEntryNotReady (retry), as required by the test-before-setup, test-before-configure and reauthentication-flow quality-scale rules.

Evidence

  • Before (main): a bad key and a missing server look identical, and a bad key sends HA users to the "upgrade Emoncms" repair:
    bad key   -> None
    no server -> None
    
    After (raise_errors=True):
    bad key   -> EmoncmsAuthError error 401 unauthorized access
    no server -> EmoncmsConnectionError client error : Cannot connect to host ...
    
  • New tests, all 8 passing:
    test_raise_auth_error      401 -> EmoncmsAuthError (async_list_feeds, async_get_uuid)
    test_raise_timeout         timeout -> EmoncmsConnectionError
    test_raise_client_error    refused -> EmoncmsConnectionError (request + input_post), no ERROR logs
    test_input_post            200 ok; 401 -> EmoncmsAuthError; 500 -> EmoncmsError;
                               default mode still RuntimeError
    

friendly behaviour

Door: two-way

The flag is opt-in and defaults to False. Once released, the exception names become public API that the Home Assistant integration depends on.

Blast Radius: minimal

Existing callers keep the dict and None returns. The one change in default mode is that async_input_post now times out after request_timeout (20 s) instead of waiting indefinitely. The version is bumped to 0.1.4.

Opt-in raise_errors raises EmoncmsAuthError on HTTP 401,
EmoncmsConnectionError on client errors/timeouts and EmoncmsError
otherwise, from async_request and async_input_post.

Closes Open-Building-Management#1
Comment thread pyemoncms/emoncms_client.py Resolved
@alexandrecuer alexandrecuer added this to the v0.1.4 milestone Nov 16, 2026
@alexandrecuer
alexandrecuer merged commit a04780a into Open-Building-Management:main Nov 16, 2026
4 checks passed
@alexandrecuer

Copy link
Copy Markdown
Contributor

@davidski : many thanks

@alexandrecuer alexandrecuer added the enhancement New feature or request label Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Distinguish an invalid API key from connection errors

2 participants