How do Thrift exceptions work?

I am extracting the IDL files, you can define your own exception types that the service method can throw:

exception SampleException {
    1: list<string> failed
}

      

What is the contract to view the correct exception message in the logs? I saw the log messages as follows: Unhandled exception in foo.bar.Service my.package.SomeException: null

. I guess where he says null

this should be the exception message?

Unfortunately, exceptions are not well documented. Should I add a field string message

for this? Is there another name for the conversion, such as a field name?

+3


source to share


1 answer


Shore Exceptions (General)

What is the contract to view the correct exception message in the logs?

The contract does not define how the message is written to any log. Thrift is designed to be protocol agnostic and transport agnostic. For example, if an HTTP server you create writes something to its log files, this is completely outside Thrift, this only applies to your HTTP server.

Do I have to add a textbox for this? Is there any other convention like the field name?

No, there are no conventions or restrictions other than what the language of your choice can impose. Technically exception

similar struct

. As an example, the following IDL is taken from the ThriftTest.thrift file .

exception Xception2 {
  1: i32 errorCode,
  2: Xtruct struct_thing
}

      

The one used above Xtruct

is the other struct

one nested within the exception. You see, you can practically do anything with Thrift exceptions, which are also possible with structs. This allows you to send a lot of error information to the client, rather than just the line "Something ugly happened on the server" or the like.

The difference struct

lies in the way the system treats them: Exceptions are serialized on the server and then re-displayed on the client. Unexpected exceptions are

  • either caught or re-fetched as shared TApplicationException

  • or not caught at all

The latter behavior is a language-specific thing that is about to change from "not caught" to "retroning as general TApplicationException

" soon .

Unfortunately, exceptions are not well documented



This is what the Thrift Whitepaper says about exceptions:

2.4 Exceptions

Exceptions are syntactically and functionally equivalent to structs, except that they are declared using the keyword exception

keyword struct

.

The generated objects inherit from the base class of the exception, depending on the situation in each target programming language, in order to integrate with the handling of individual exceptions in any language. Again, the focus of the project is on making the code familiar to the application developer.

How can I make sure the call is Exception(string)

CTOR?

I saw log messages like this:

Unhandled exception in foo.bar.Service my.package.SomeException: null 

      

I think where he says null

this should be the exception message? [...] for certain exceptions that are generated in the Scala / Java, which is the correct way to ensure that the constructor is called: public Exception(String message)

.

Similar to "never" or more accurate "no way". These are all CTORs that Thrift (trunk version) generates from the IDL above:

  public Xception() {
  }

  public Xception(
    int errorCode,
    String message)
  {
    this();
    this.errorCode = errorCode;
    setErrorCodeIsSet(true);
    this.message = message;
  }

  /**
   * Performs a deep copy on <i>other</i>.
   */
  public Xception(Xception other) {
    __isset_bitfield = other.__isset_bitfield;
    this.errorCode = other.errorCode;
    if (other.isSetMessage()) {
      this.message = other.message;
    }
  }

      

Since all exceptions thrown are exited TException

, they must call one of the parameterized CTORs to get message

and / or cause

to base exception

. But Xception

obviously it doesn't. The reason is probably because Thrift allows you to define anything as a member of an exception and guarantees (or accepts) nothing. In other words, you cannot be sure of what except for one field. Therefore, you always get an error null

.

Bottom line (type TL; DR)

I think that, generally speaking, you are correct and this behavior should be changed . At the moment, the only workaround I can see is to explicitly catch TException

before they go into the log and repackage them using a member function toString()

:

} catch (org.apache.thrift.TException te) {
  throw new Exception(te.toString());
}

      

+2


source







All Articles