Skip to content

Commit a7bd90b

Browse files
committed
fix
Signed-off-by: i2y <6240399+i2y@users.noreply.github.com>
1 parent 04b9821 commit a7bd90b

2 files changed

Lines changed: 11 additions & 36 deletions

File tree

docs/errors.md

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -98,49 +98,20 @@ async def create_user(self, request: CreateUserRequest, ctx: RequestContext) ->
9898
)
9999
```
100100

101-
Reading error details on the client:
101+
Reading error details on the client - error details are `google.protobuf.Any` messages that can be unpacked to their original types:
102102

103103
```python
104104
try:
105105
response = await client.some_method(request)
106106
except ConnectError as e:
107107
for detail in e.details:
108-
# Details are google.protobuf.Any messages
109-
unpacked = Struct()
110-
if detail.Unpack(unpacked):
111-
print(f"Error detail: {unpacked}")
112-
```
113-
114-
### Understanding google.protobuf.Any
115-
116-
The `google.protobuf.Any` type stores arbitrary protobuf messages along with their type information:
117-
118-
- `type_url`: A string identifying the message type (e.g., `type.googleapis.com/google.protobuf.Struct`)
119-
- `value`: The serialized bytes of the actual message
120-
121-
Debugging tips:
122-
123-
```python
124-
try:
125-
response = await client.some_method(request)
126-
except ConnectError as e:
127-
for detail in e.details:
128-
# Inspect the type URL
129-
print(f"Detail type: {detail.type_url}")
130-
131-
# Check type before unpacking
108+
# Check the type before unpacking
132109
if detail.Is(Struct.DESCRIPTOR):
133110
unpacked = Struct()
134111
detail.Unpack(unpacked)
135-
print(f"Struct detail: {unpacked}")
112+
print(f"Error detail: {unpacked}")
136113
```
137114

138-
Common issues:
139-
140-
1. **Type mismatch**: If `Unpack()` returns `False`, check the `type_url` to see the actual type
141-
2. **Missing imports**: Import the protobuf message classes for the error details you expect
142-
3. **Custom details**: Ensure both client and server have access to the same proto definitions
143-
144115
### Standard error detail types
145116

146117
With `googleapis-common-protos` installed, you can use standard types like:

docs/usage.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,14 @@ Interceptors can catch and transform errors. This is useful for adding context,
271271
return await call_next(request, ctx)
272272
except ConnectError as e:
273273
# Log the error with context
274-
print(f"Error in {ctx.method()}: {e.code} - {e.message}")
274+
method = ctx.method()
275+
print(f"Error in {method.service_name}/{method.name}: {e.code} - {e.message}")
275276
# Re-raise the error
276277
raise
277278
except Exception as e:
278279
# Convert unexpected errors to ConnectError
279-
print(f"Unexpected error in {ctx.method()}: {e}")
280+
method = ctx.method()
281+
print(f"Unexpected error in {method.service_name}/{method.name}: {e}")
280282
raise ConnectError(Code.INTERNAL, "An unexpected error occurred")
281283
```
282284

@@ -292,11 +294,13 @@ Interceptors can catch and transform errors. This is useful for adding context,
292294
return call_next(request, ctx)
293295
except ConnectError as e:
294296
# Log the error with context
295-
print(f"Error in {ctx.method()}: {e.code} - {e.message}")
297+
method = ctx.method()
298+
print(f"Error in {method.service_name}/{method.name}: {e.code} - {e.message}")
296299
# Re-raise the error
297300
raise
298301
except Exception as e:
299302
# Convert unexpected errors to ConnectError
300-
print(f"Unexpected error in {ctx.method()}: {e}")
303+
method = ctx.method()
304+
print(f"Unexpected error in {method.service_name}/{method.name}: {e}")
301305
raise ConnectError(Code.INTERNAL, "An unexpected error occurred")
302306
```

0 commit comments

Comments
 (0)