Exception 如何在Apache Thrift的基础上实现golang服务(有例外)?

Exception 如何在Apache Thrift的基础上实现golang服务(有例外)?,exception,go,exception-handling,thrift,Exception,Go,Exception Handling,Thrift,我有一个服务节俭界面,用于结果方法,如下所示: exception SomeException { 1: string message; } string result( 1: string token, 2: string identifier ) throws ( 1: SomeException ex, ); 我如何在golang中正确实现这一点?我希望为这个节俭服务的客户正确抛出异常。这里开始使用。本教程由以下部分组成: 如果客户端以这种方式将计算操作

我有一个服务节俭界面,用于
结果
方法,如下所示:

exception SomeException {
    1: string message;
}

string result(
    1: string token,
    2: string identifier
) throws (
    1: SomeException ex,
);
我如何在golang中正确实现这一点?我希望为这个节俭服务的客户正确抛出异常。

这里开始使用。本教程由以下部分组成:

如果客户端以这种方式将计算操作传递给服务器:

work := tutorial.NewWork()
work.Op = tutorial.Operation_DIVIDE
work.Num1 = 1
work.Num2 = 0
quotient, err := client.Calculate(1, work)
if err != nil {
    switch v := err.(type) {
    case *tutorial.InvalidOperation:
        fmt.Println("Invalid operation:", v)
    default:
        fmt.Println("Error during operation:", err)
    }
    return err
} else {
    fmt.Println("Whoa we can divide by 0 with new value:", quotient)
}
服务器应该抛出一个异常,如下所示。当传递
w.Op
的某个未知值时,也会发生类似的情况:

func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32, err error) {

switch w.Op {
    case tutorial.Operation_DIVIDE:
        if w.Num2 == 0 {
            ouch := tutorial.NewInvalidOperation()
            ouch.WhatOp = int32(w.Op)
            ouch.Why = "Cannot divide by 0"
            err = ouch
            return
        }
        val = w.Num1 / w.Num2
        break

    // other cases omitted

    default:
        ouch := tutorial.NewInvalidOperation()
        ouch.WhatOp = int32(w.Op)
        ouch.Why = "Unknown operation"
        err = ouch
        return
    }

    // more stuff omitted
}

简而言之,Thrift接口实现了
error
error()string
函数)。因此可以像任何go
error
一样返回,通常像
return nil,err

func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32, err error) {

switch w.Op {
    case tutorial.Operation_DIVIDE:
        if w.Num2 == 0 {
            ouch := tutorial.NewInvalidOperation()
            ouch.WhatOp = int32(w.Op)
            ouch.Why = "Cannot divide by 0"
            err = ouch
            return
        }
        val = w.Num1 / w.Num2
        break

    // other cases omitted

    default:
        ouch := tutorial.NewInvalidOperation()
        ouch.WhatOp = int32(w.Op)
        ouch.Why = "Unknown operation"
        err = ouch
        return
    }

    // more stuff omitted
}