thriftrw / thriftrw-python

A Thrift encoding library for Python
MIT License
36 stars 10 forks source link

Message envelope and uncaught exceptions #104

Open abhinav opened 8 years ago

abhinav commented 8 years ago

We need a way to handle uncaught exceptions. Thrift uses the following shape for internal exceptions.

enum ExceptionType {
  UNKNOWN = 0
  UNKNOWN_METHOD = 1
  INVALID_MESSAGE_TYPE = 2
  WRONG_METHOD_NAME = 3
  BAD_SEQUENCE_ID = 4
  MISSING_RESULT = 5
  INTERNAL_ERROR = 6
  PROTOCOL_ERROR = 7
  INVALID_TRANSFORM = 8
  INVALID_PROTOCOL = 9
  UNSUPPORTED_CLIENT_TYPE = 10
}

struct TApplicationException {
  1: optional string message
  2: optional ExceptionType type
}

We need to be able to send messages with that as the payload and EXCEPTION as the message type.

abhinav commented 8 years ago

Proposal: We call it InternalApplicationError or something similar that is more obvious than TApplicationException. It is simple enough to make message.dumps and message.loads support it. (Right now, message.loads treats this as an unknown exception and raises an exception with the Value-level representation).

import thriftrw
from thriftrw.something import InternalApplicationError  # something or just top-level TBD

myservice = thriftrw.load(..)
payload = myservice.dumps.message(InternalApplicationError(..), seqid=42)

message = myservice.loads.message(myservice.SomeService, payload)
assert message.message_type == mtype.EXCEPTION
raise message.body

The Client/Server API in #103 will need to support this too.

abhinav commented 8 years ago

Actually, the EXCEPTION message still needs the method name.