MTrab / energidataservice

Fetches spot prices from Energi Data Service
GNU General Public License v3.0
194 stars 27 forks source link

[FR]: Handle http error code 500 from the energidataservice connector more user-friendly #542

Closed madsdyd closed 3 months ago

madsdyd commented 4 months ago

Describe the feature you wish to make a request for

If energidataservice (the site) fails to respond with a 200, some cases are handled in energidataservice (the HA integration). These include 400 and 411. However, 500 is not specifically handled, and falls through to a generic message.

I would like eds to write a more informative log message, when 500 is returned.

Additionally, it seems that the current "failure" to handle 500 in some specific way, means that eds will time out during HA startup. Perhaps this could also be done "differently", such that eds does not delay HA restarts when energidataservice (the site) is non-functional.

Example of current log message:

Denne fejl stammer fra en brugerdefineret integration.

Logger: custom_components.energidataservice.tariffs.energidataservice
Kilde: custom_components/energidataservice/tariffs/energidataservice/__init__.py:209
integration: Energi Data Service (dokumentation, problemer)
Første forekomst: 10.46.52 (14 forekomster)
Senest logget: 14.41.23

Error during API request: 500, message='Internal Server Error', url=URL('https://api.energidataservice.dk/dataset/DatahubPricelist?filter=%7B%22Note%22:%5B%22Elafgift%22,%22Systemtarif%22,%22Transmissions+nettarif%22%5D%7D&limit=500')
Error during API request: 500, message='Internal Server Error', url=URL('https://api.energidataservice.dk/dataset/DatahubPricelist?filter=%7B%22chargetypecode%22:+%5B%22DT_C_01%22%5D,%22gln_number%22:+%5B%225790000705689%22%5D,%22chargetype%22:+%5B%22D03%22%5D%7D&sort=ValidFrom+desc&limit=500')

If this error was changed to something like:

"Energidataservice API returned errorcode 500, which is an internal error at the energidataservice.dk site. This kind of error typically goes away in a couple of hours on weekdays, but can persist for some time during e.g. weekends. There is nothing this integration can do to work around this, so the sensor will be marked 'unknown' or 'unavailable' as long as this error code is returned. The sensor will return to normal eventually."

then it would be more userfriendly, and the user would know not to post e.g. bugs here on github or create posts on Facebook.

I think, but are unable to test, that this could be a simple change to https://github.com/MTrab/energidataservice/blob/43b1db334de0c4fb410dc2f20873a122106905d3/custom_components/energidataservice/connectors/energidataservice/__init__.py#L90 -- just adding a case for 500.

Describe alternatives you've considered

No response

Additional context

No response

madsdyd commented 4 months ago

So, the above is a bald lie! :-) -- I have waited for the integration to resolve the issue automatically, but it hasn't. Today I found out that the energidataservice site was running as expected, but my energidataservice integration did not work (still unavailable). Restarting the integration from HA (Genindlæs) resolved the matter. Which is great and all, but perhaps the scope of this bug should be extended to implement "automatic" restart of the integration or retrying or whatever. This is probably too involved for me at this point.

davidolrik commented 3 months ago

The issue is with the API, they are running out of database connections and that makes the API fail with an Internal Server Error.

I have written to their support, which seems to have worked although I haven't received any response yet.

When this error happens it blocks the startup sequence of home assistant as modules are started sequentially.

I suggest that the 500 error is acknowledged, and not retried immediately but rather just let the normal update frequency pick the result up later.

MortenHannemose commented 3 months ago

I'm on the latest version (1.5.9), but there is unfortunately still an API error in my HA logs.

Logger: custom_components.energidataservice.connectors.nordpool
Source: custom_components/energidataservice/connectors/nordpool/__init__.py:105
integration: Energi Data Service ([documentation](https://github.com/MTrab/energidataservice/blob/master/README.md), [issues](https://github.com/MTrab/energidataservice/issues))
First occurred: 16:42:31 (3 occurrences)
Last logged: 16:42:31

API returned error 502
MTrab commented 3 months ago

Will still be reported as errors (as it actually is) but shouldn't block anymore