Repository: python-trio/trio

Issue:
Improve `start_soon` and `connection failed` error messages
Given …
```
Traceback (most recent call last):
  File "./test", line 210, in main
    tg.start_soon(s.run)
  File "/home/smurf/src/Mudlet/test/e2e/venv/lib/python3.8/site-packages/trio/_core/_run.py", line 983, in start_soon
    GLOBAL_RUN_CONTEXT.runner.spawn_impl(async_fn, args, self, name)
  File "/home/smurf/src/Mudlet/test/e2e/venv/lib/python3.8/site-packages/trio/_core/_run.py", line 1432, in spawn_impl
    coro = coroutine_or_error(async_fn, *args)
  File "/home/smurf/src/Mudlet/test/e2e/venv/lib/python3.8/site-packages/trio/_util.py", line 108, in coroutine_or_error
    coro = async_fn(*args)
TypeError: run() missing 1 required positional argument: 'evt'
```
… and the fact that I have approx 1337 classes (I exaggerate but slightly) whose `run` methods want an `evt` parameter, it'd be nice to see the qualified name of the thing. It'd be even nicer to see who tried to call it, but oh well.

Given the fact that my server connects to 4781 different remote systems (this however is an understatement),
```
Traceback (most recent call last):
  File "/home/smurf/src/Mudlet/test/e2e/venv/lib/python3.8/site-packages/trio/_highlevel_open_tcp_stream.py", line 332, in attempt_connect
    await sock.connect(sockaddr)
  File "/home/smurf/src/Mudlet/test/e2e/venv/lib/python3.8/site-packages/trio/_socket.py", line 682, in connect
    raise OSError(err, "Error in connect: " + os.strerror(err))
ConnectionRefusedError: [Errno 111] Error in connect: Connection refused
```
really should print the actual address it tried to talk to – instead of repeating itself.

Comments:

Patch:
diff --git a/newsfragments/1810.feature.rst b/newsfragments/1810.feature.rst
new file mode 100644
index 00000000..a2599d32
--- /dev/null
+++ b/newsfragments/1810.feature.rst
@@ -0,0 +1,1 @@
+`trio.socket.socket` now prints the address it tried to connect to upon failure.
diff --git a/trio/_socket.py b/trio/_socket.py
index bcff1ee9..8bfa6d26 100644
--- a/trio/_socket.py
+++ b/trio/_socket.py
@@ -688,7 +688,7 @@ class _SocketType(SocketType):
         # Okay, the connect finished, but it might have failed:
         err = self._sock.getsockopt(_stdlib_socket.SOL_SOCKET, _stdlib_socket.SO_ERROR)
         if err != 0:
-            raise OSError(err, "Error in connect: " + os.strerror(err))
+            raise OSError(err, f"Error connecting to {address}: {os.strerror(err)}")
 
     ################################################################
     # recv


Test patch:
diff --git a/trio/tests/test_socket.py b/trio/tests/test_socket.py
index 1fa3721f..ea5e48fe 100644
--- a/trio/tests/test_socket.py
+++ b/trio/tests/test_socket.py
@@ -723,6 +723,16 @@ async def test_SocketType_connect_paths():
             await sock.connect(("127.0.0.1", 2))
 
 
+# Fix issue #1810
+async def test_address_in_socket_error():
+    address = "127.0.0.1"
+    with tsocket.socket() as sock:
+        try:
+            await sock.connect((address, 2))
+        except OSError as e:
+            assert any(address in str(arg) for arg in e.args)
+
+
 async def test_resolve_address_exception_in_connect_closes_socket():
     # Here we are testing issue 247, any cancellation will leave the socket closed
     with _core.CancelScope() as cancel_scope: