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.


Discussion:
I'm not able to reproduce the first one:
```python
async def test_start_soon_error_message():
    def f(_parameter):
        ...

    async with _core.open_nursery() as nursery:
        nursery.start_soon(f)
```
gives
`TypeError: test_start_soon_error_message.<locals>.f() missing 1 required positional argument: '_parameter'`

But can confirm the second one.

Resolving patch:
diff --git a/trio/_socket.py b/trio/_socket.py
--- a/trio/_socket.py
+++ b/trio/_socket.py
@@ -688,7 +688,7 @@ async def connect(self, address):
         # 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
--- a/trio/tests/test_socket.py
+++ b/trio/tests/test_socket.py
@@ -723,6 +723,16 @@ def connect(self, *args, **kwargs):
             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: