GoFrame 错误码特性-错误码接口

基本介绍

框架提供了默认的错误码组件​gcode​,错误码使用接口化设计,以实现高扩展性。

接口定义

// Code is universal error code interface definition.
type Code interface {
	// Code returns the integer number of current error code.
	Code() int

	// Message returns the brief message for current error code.
	Message() string

	// Detail returns the detailed information of current error code,
	// which is mainly designed as an extension field for error code.
	Detail() interface{}
}

默认实现

框架提供了默认实现​gcode.Code​的结构体,开发者可以直接通过​New/WithCode​方法创建错误码:

  • 格式:
// New creates and returns an error code.
// Note that it returns an interface object of Code.
func New(code int, message string, detail interface{}) Code

  • 示例:
func ExampleNew() {
	c := gcode.New(1, "custom error", "detailed description")
	fmt.Println(c.Code())
	fmt.Println(c.Message())
	fmt.Println(c.Detail())

	// Output:
	// 1
	// custom error
	// detailed description
}

如果开发者觉得框架默认实现​gcode.Code​的结构体不满足需求,可以自行定义,只需实现​gcode.Code​即可。

作者:admin,如若转载,请注明出处:https://www.web176.com/goframe/21089.html

(0)
打赏 支付宝 支付宝 微信 微信
adminadmin
上一篇 2023年5月18日
下一篇 2023年5月18日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注