Categories: GoFrame 教程

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

这个人很懒,什么都没有留下~

Share
Published by
admin

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago