snicker / juicepassproxy

Proxy UDP requests to/from Juicebox EV chargers to MQTT discoverable by Home Assistant
25 stars 8 forks source link

Catch errors on IP resolve #45

Closed Snuffy2 closed 3 months ago

Snuffy2 commented 7 months ago

Wanted to catch DNS resolve errors. I caused it due to my router settings, but JPP will crash if it can't resolve the EnelX server IP with the external 1.1.1.1 IP.

juicepassproxy  | Traceback (most recent call last):
juicepassproxy  |   File "/juicepassproxy/juicepassproxy.py", line 661, in <module>
juicepassproxy  |     main()
juicepassproxy  |   File "/juicepassproxy/juicepassproxy.py", line 603, in main
juicepassproxy  |     elif enelx_server_ip := resolve_ip_external_dns(enelx_server):
juicepassproxy  |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
juicepassproxy  |   File "/juicepassproxy/juicepassproxy.py", line 395, in resolve_ip_external_dns
juicepassproxy  |     answers = res.resolve(address)
juicepassproxy  |               ^^^^^^^^^^^^^^^^^^^^
juicepassproxy  |   File "/usr/local/lib/python3.12/site-packages/dns/resolver.py", line 1321, in resolve
juicepassproxy  |     timeout = self._compute_timeout(start, lifetime, resolution.errors)
juicepassproxy  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
juicepassproxy  |   File "/usr/local/lib/python3.12/site-packages/dns/resolver.py", line 1075, in _compute_timeout
juicepassproxy  |     raise LifetimeTimeout(timeout=duration, errors=errors)
juicepassproxy  | dns.resolver.LifetimeTimeout: The resolution lifetime expired after 5.401 seconds: Server Do53:1.1.1.1@53 answered The DNS operation timed out.; Server Do53:1.1.1.1@53 answered The DNS operation timed out.; Server Do53:1.1.1.1@53 answered The DNS operation timed out.

Now it handles it as just a Warning and JPP will fallback to the Enel X IP in the Config or the Default one:

juicepassproxy  | 2023-12-11 07:57:14,477 WARNING    Unable to resolve juicenet-udp-prod3-usa.enelx.com: The resolution lifetime expired after 5.402 seconds: Server Do53:1.1.1.1@53 answered The DNS operation timed out.; Server Do53:1.1.1.1@53 answered The DNS operation timed out.; Server Do53:1.1.1.1@53 answered The DNS operation timed out.
snicker commented 3 months ago

@Snuffy2 let me know if you want to merge this one- thinking we have a couple things to target a release this week or next