Yuxuan Wang created THRIFT-5326:
-----------------------------------
Summary: Expand TException interface in go library
Key: THRIFT-5326
URL: https://issues.apache.org/jira/browse/THRIFT-5326
Project: Thrift
Issue Type: Task
Components: Go - Compiler, Go - Library
Reporter: Yuxuan Wang
This is an issue unique to go, because of go's duck typing interface system:
There's no way to distinguish TException from other errors.
Currently TException is defined to be identical to error
([link|https://github.com/apache/thrift/blob/3b9259d88b6ceb13bb6b8c6afe676fed707dcd4e/lib/go/thrift/exception.go#L27-L29]):
{code:go}
type TException interface {
error
}
{code}
Which means when you try to do
{code:go}
var texception thrift.TException
if errors.As(err, &texception) {
}
{code}
It's always true.
TTransportException, TApplicationException, and TProtocolException are
identifiable, but for exceptions generated from the compiler (that's defined in
thrift files), TExecption is the only "common" type they share and it's very
hard to write library code to handle them collectively.
My proposal is to add a new function to TException interface (which also need
to be added to TTransportException, TApplicationException, TProtocolException,
and all compiler generated exceptions). There are 2 ways to define this new
function:
1. TExceptionType() TExceptionType, where TExceptionType is an enum type, with
values of TTransportException, TApplicationException, TProtocolException,
TIDLException (name tbd, open to suggestions, this is the one intended to be
used by compiler)
2. TExceptionName() string, which for TTransportException the string would just
be "TTransportException", etc. For compiler generated exceptions, I think the
name should be the name of the exception with the thrift filename as the
namespace. For example, for:
{code}
exception MyError {
...
}
{code}
that's defined in my_service.thrift, the compiler generated name would be
"my_service.MyError". Note that the namespace could be different from the
actual go import namespace, as in if the thrift file have the line of
"namespace go foo.bar.my_service_2", the go namespace would be "my_service_2"
instead of "my_service"
I would slightly prefer the enum approach as it's simpler (there's no ambiguity
on the namespace part of the string approach). Either way this requires
compiler change to add the new function to generated exception types.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)