How to implement golang service based on Apache Thrift with exceptions?

huangapple go评论79阅读模式
英文:

How to implement golang service based on Apache Thrift with exceptions?

问题

我有一个result方法的thrift接口,如下所示:

exception SomeException {
    1: string message;
}

string result(
    1: string token,
    2: string identifier
) throws (
    1: SomeException ex,
);

我该如何在golang中正确实现这个接口?我希望这个Thrift服务的客户端能够正确地抛出异常。

英文:

I have a service thrift interface for result method as follows:

exception SomeException {
    1: string message;
}

string result(
    1: string token,
    2: string identifier
) throws (
    1: SomeException ex,
);

How di I implement this properly in golang? I want exceptions to be thrown properly for clients of this Thrift service.

答案1

得分: 5

这里是Apache Thrift Go教程。教程包括一个小型服务

enum Operation {
	ADD = 1,
	SUBTRACT = 2,
	MULTIPLY = 3,
	DIVIDE = 4
}

struct Work {
	1: i32 num1 = 0,
	2: i32 num2,
	3: Operation op,
	4: optional string comment,
}

service Calculator extends shared.SharedService {
	i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),

	// 其他一些方法...
}

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

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("无效操作:", v)
	default:
		fmt.Println("操作期间出错:", err)
	}
	return err
} else {
	fmt.Println("哇,我们可以用新值除以0:", 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 = "不能除以0"
			err = ouch
			return
		}
		val = w.Num1 / w.Num2
		break
		
	// 其他情况省略
	
	default:
		ouch := tutorial.NewInvalidOperation()
		ouch.WhatOp = int32(w.Op)
		ouch.Why = "未知操作"
		err = ouch
		return
	}
	
	// 更多内容省略
}
英文:

Here the Apache Thrift tutorial for Go comes into play. The tutorial consists of a small service:

enum Operation {
	ADD = 1,
	SUBTRACT = 2,
	MULTIPLY = 3,
	DIVIDE = 4
}

struct Work {
	1: i32 num1 = 0,
	2: i32 num2,
	3: Operation op,
	4: optional string comment,
}

service Calculator extends shared.SharedService {
	i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),

	// some other methods ...
}

If the client passes a calculation operation to the server in this way:

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)
}

the server should throw an exception as shown below. A similar thing happens when some unknown value for the w.Op is passed:

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
}

答案2

得分: 0

简短回答是,Thrift接口实现了error接口(Error() string函数)。因此可以像任何Go的error一样返回,通常是return nil, err

英文:

The short answer is that Thrift Interfaces implement the error interface (Error() string function) . Thus can be returned just like any go error, usually like return nil, err.

huangapple
  • 本文由 发表于 2015年12月4日 23:15:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/34091508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定