You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@thrift.apache.org by GitBox <gi...@apache.org> on 2020/12/22 21:50:44 UTC

[GitHub] [thrift] fishy commented on a change in pull request #2298: THRIFT-5326: Expand TException interface in go library

fishy commented on a change in pull request #2298:
URL: https://github.com/apache/thrift/pull/2298#discussion_r547523241



##########
File path: compiler/cpp/src/thrift/generate/t_go_generator.cc
##########
@@ -2700,8 +2707,8 @@ void t_go_generator::generate_service_server(t_service* tservice) {
     f_types_ << indent() << "func (p *" << serviceName
                << "Processor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err "
                   "thrift.TException) {" << endl;
-    f_types_ << indent() << "  name, _, seqId, err := iprot.ReadMessageBegin(ctx)" << endl;
-    f_types_ << indent() << "  if err != nil { return false, err }" << endl;
+    f_types_ << indent() << "  name, _, seqId, err2 := iprot.ReadMessageBegin(ctx)" << endl;

Review comment:
       Note: all the compiler changes starting from this line are because in Process function we specified the returned error type to be `TException` instead of just `error`. I'm not sure why is that, if we can make another breaking change to `TProcessor` and `TProcessorFunction` these changes can be avoided.

##########
File path: lib/go/thrift/exception.go
##########
@@ -26,19 +26,86 @@ import (
 // Generic Thrift exception
 type TException interface {
 	error
+
+	TExceptionType() TExceptionType
 }
 
 // Prepends additional information to an error without losing the Thrift exception interface
 func PrependError(prepend string, err error) error {
-	if t, ok := err.(TTransportException); ok {
-		return NewTTransportException(t.TypeId(), prepend+t.Error())
+	msg := prepend + err.Error()
+
+	if te, ok := err.(TException); ok {
+		switch te.TExceptionType() {
+		case TExceptionTypeTransport:
+			if t, ok := err.(TTransportException); ok {
+				return NewTTransportException(t.TypeId(), msg)
+			}
+		case TExceptionTypeProtocol:
+			if t, ok := err.(TProtocolException); ok {
+				return NewTProtocolExceptionWithType(t.TypeId(), errors.New(msg))
+			}
+		case TExceptionTypeApplication:
+			if t, ok := err.(TApplicationException); ok {
+				return NewTApplicationException(t.TypeId(), msg)
+			}
+		}
+
+		return wrappedTException{
+			err:            errors.New(msg),
+			tExceptionType: te.TExceptionType(),
+		}
+	}
+
+	return errors.New(msg)
+}
+
+// TExceptionType is an enum type to categorize different "subclasses" of TExceptions.
+type TExceptionType byte
+
+// TExceptionType values
+const (
+	TExceptionTypeUnknown     TExceptionType = iota
+	TExceptionTypeCompiled                   // TExceptions defined in thrift files and generated by thrift compiler

Review comment:
       I'm open to suggestions on this name :)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org