Skip to main content

Error Handling & Best Practices

Learn how to handle errors gracefully, implement robust reconnection strategies, and follow production-ready best practices for gRPC streaming in Python.

gRPC Status Codes

gRPC uses standard status codes to indicate errors. Understanding these codes is essential for proper error handling in Python.

Common Status Codes

CodeNameDescriptionAction
0OKSuccessContinue processing
1CANCELLEDOperation canceledClean up resources
3INVALID_ARGUMENTInvalid request parametersFix request and retry. See Request Parameters for details.
4DEADLINE_EXCEEDEDOperation timeoutRetry with backoff
7PERMISSION_DENIEDInsufficient permissionsCheck account permissions
14UNAVAILABLEService unavailableReconnect with backoff
16UNAUTHENTICATEDAuthentication failedRefresh token and retry

Checking Error Codes in Python

import grpc

try:
    for response in response_stream:
        # Process response
        pass

except grpc.RpcError as e:
    status_code = e.code()

    if status_code == grpc.StatusCode.UNAUTHENTICATED:
        print("Authentication failed")
    elif status_code == grpc.StatusCode.UNAVAILABLE:
        print("Service unavailable")
    elif status_code == grpc.StatusCode.INVALID_ARGUMENT:
        print("Invalid request")
    else:
        print(f"Error: {status_code} - {e.details()}")

Common Errors and Solutions

1. UNAUTHENTICATED - Authentication Failed

Causes:
  • Invalid access token
  • Expired access token
  • Missing authorization metadata

2. UNAVAILABLE - Service Unavailable

Causes:
  • Network connectivity issues
  • Server temporarily unavailable
  • Firewall blocking connection

3. INVALID_ARGUMENT - Bad Request

Causes:
  • Invalid symbol
  • Invalid depth value
  • Malformed request

Next Steps