Skip to content

relay.py: include error detail + URL in on_error print#1

Open
bitcoin3us wants to merge 1 commit intoMicroPythonOS:mainfrom
bitcoin3us:diagnostics/relay-on-error-detail
Open

relay.py: include error detail + URL in on_error print#1
bitcoin3us wants to merge 1 commit intoMicroPythonOS:mainfrom
bitcoin3us:diagnostics/relay-on-error-detail

Conversation

@bitcoin3us
Copy link
Copy Markdown

Summary

The _on_error callback in nostr/relay.py currently prints a bare relay.py got error with no exception info and no relay URL. When a WebSocket connection fails (TLS handshake, DNS, connection reset, socket exhaustion, etc.) there's nothing in the log to tell you what went wrong or which relay in a multi-relay configuration.

def _on_error(self, class_obj, error):
    print("relay.py got error")       # ← no detail, no URL
    self.connected = False
    ...

Discovery

Encountered while debugging NWC connect failures on an ESP32 running MicroPythonOS. The device kept reporting "Could not connect to any Nostr Wallet Connect relays" with no explanation — only relay.py got error in serial. Had to patch this file locally to surface the exception before the real issue (socket-pool exhaustion from a stale LNBits WebSocket) became visible.

Fix

print("relay.py got error for {}: {!r}".format(self.url, error))

repr() on the exception gives both the type and the args/message, which is what you want for diagnostics. No behaviour change — same signature, same control flow, reconnect logic untouched.

Example output

Before:

relay.py got error

After (illustrative):

relay.py got error for wss://relay.coinos.io: OSError(118, 'Host is unreachable')
relay.py got error for wss://relay.damus.io: SSLError(-29312, 'MBEDTLS_ERR_…')
relay.py got error for wss://nos.lol: ValueError('unexpected close frame')

One line, enough signal to point at DNS vs TLS vs socket exhaustion vs protocol issues.

Test plan

  • Trigger a connection failure (block the relay's port in a firewall, stop the local relay mid-connect, etc.) — verify log includes the URL and exception repr
  • Trigger a clean disconnect — _on_close still fires (unchanged)
  • Trigger a successful connect — _on_open still fires (unchanged)

🤖 Generated with Claude Code

Previously the on_error callback printed a bare 'relay.py got error'
with no exception info and no relay URL. When a WebSocket connection
to a relay fails (TLS handshake, DNS, connection reset, socket
exhaustion, etc.) there's no way to tell from logs *what* went wrong
or *which* relay in a multi-relay configuration.

Encountered while debugging NWC connect failures on ESP32 — had to
patch the library locally to surface the exception type, only to
discover it was a socket-pool exhaustion issue that resolved after
the caller closed its old sockets. The diagnostic would have been
obvious from the log had `error` been printed.

Change:
  print("relay.py got error")
→
  print("relay.py got error for {}: {!r}".format(self.url, error))

repr() on the exception gives both the type and the args/message,
which is what you want for diagnostics. Backwards-compatible at the
callback level (same signature, same flow).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant