A class to register com.twitter.finatra.thrift.exceptions.ExceptionMappers and handle exceptions
An ExceptionMapper converts a T
-typed throwable to a Future[Rep].
A generic com.twitter.finatra.thrift.exceptions.ExceptionMapper over the java.lang.Exception exception type.
A generic com.twitter.finatra.thrift.exceptions.ExceptionMapper over the
java.lang.Exception exception type. This mapper attempts to translate other
exceptions to declared finatra-thrift/finatra_thrift_exception.thrift
exceptions.
If you include the finatra-thrift/finatra_thrift_exception.thrift
file in your
thrift service IDL and declare any of your service methods to throw one or more of the defined
thrift exceptions, i.e.,
finatra_thrift_exceptions.ClientError finatra_thrift_exceptions.NoClientIdError finatra_thrift_exceptions.ServerError finatra_thrift_exceptions.UnknownClientIdError
using the FinatraThriftExceptionMapper will translate any Throwable into one of the finatra_thrift_exceptions where appropriate for responding to the client.
Exceptions returned from a thrift service which are not declared in the thrift
IDL are returned as a generic TApplicationException
.
In your myservice.thrift
, include the finatra-thrift/finatra_thrift_exception.thrift
thrift IDL:
namespace java com.my_org.myservice.thriftjava
#@namespace scala com.my_org.myservice.thriftscala
include "finatra-thrift/finatra_thrift_exceptions.thrift"
Then in your MyService definition, declare methods to throw finatra_thrift_exceptions:
service MyService { string function1( 1: string msg ) throws ( 1: finatra_thrift_exceptions.ServerError serverError, 2: finatra_thrift_exceptions.UnknownClientIdError unknownClientIdError 3: finatra_thrift_exceptions.NoClientIdError noClientIdError )
Adding this ExceptionMapper into your server will thus translate any of the handled exceptions into a form that will serialize appropriately to the client.
Note, you can also mix usage with your own declared exception(s), e.g.,
exception MyServiceException {
1: string message
}
Then in your MyService definition,
service MyService { string function1( 1: string msg ) throws ( 1: finatra_thrift_exceptions.ServerError serverError, 2: finatra_thrift_exceptions.UnknownClientIdError unknownClientIdError 3: finatra_thrift_exceptions.NoClientIdError noClientIdError 4: MyServiceException myServiceException )
You could then register an additional ExceptionMapper to translate Throwables
into MyServiceException
as desired or return the exception directly from implemented
methods.
This is only applicable in scala since the FinatraThriftExceptionMapper uses
scala generated classes for the exceptions. We recommend for users including the
finatra-thrift/finatra_thrift_exception.thrift
register this mapper in their
scala Servers.
src/main/thrift/finatra-thrift/finatra_thrift_exception.thrift
A class to register com.twitter.finatra.thrift.exceptions.ExceptionMappers and handle exceptions
Given an exception, the ExceptionManager will find an com.twitter.finatra.thrift.exceptions.ExceptionMapper to handle that particular class of exceptions. If the mapper for that exception isn't registered, the ExceptionManager will try the exception's parent class, until it reaches a Throwable class. If no Throwable class exception mapper is found, it won't handle the exception. Users are free to register their own ExceptionMapper[Throwable] which will be the root exception mapper.