---
title: "错误处理-错误码扩展"
url: "https://goframe.org/docs/core/gerror-code-extension"
markdown_url: "https://goframe.org/docs/core/gerror-code-extension.md"
locale: "zh-Hans"
version: "2.10.x(Latest)"
section: "核心组件"
category: "核心组件 / 错误处理 / 错误处理-错误码特性"
published: "2024-10-21"
updated: "2026-02-11"
description: "详细讲解`GoFrame`框架`gcode`组件的错误码扩展功能，通过`Detail`参数实现复杂业务场景下的错误码自定义。示例展示如何定义包含用户信息的`BizCode`结构体，使用`gcode.WithCode`方法创建带扩展数据的错误码。在中间件中获取错误码详情进行日志记录，并将错误信息返回给客户端，实现灵活的错误处理和日志管理。"
keywords: ["GoFrame", "GoFrame框架", "错误处理", "错误码扩展", "业务逻辑", "中间件", "自定义扩展", "错误码定义", "WithCode方法", "gcode"]
---

# 错误处理-错误码扩展

当业务需要复杂的错误码定义时，我们推荐灵活使用错误码的`Detail`参数来扩展错误码功能。

我们来看个示例，该示例主要实现以下几个功能：
- 使用一个中间件识别路由函数的执行结果，并获取路由函数返回的错误码信息，将该错误码信息作为返回结果的一部分返回给调用端。
- 如果产生了错误，会记录产生该错误的当前用户信息到日志中。该功能使用`Detail`扩展参数实现。

## 业务错误码

### 错误码定义

```go
type BizCode struct {
    User User
    // ...
}

type User struct {
    Id   int
    Name string
    // ...
}
```

### 错误码使用

扩展错误码大多数场景下需要使用 `gcode.WithCode` 方法：

```go
// WithCode creates and returns a new error code based on given Code.
// The code and message is from given `code`, but the detail if from given `detail`.
func WithCode(code Code, detail interface{}) Code
```

因此上面我们的自定义扩展可以这么使用：

```go
code := gcode.WithCode(gcode.CodeNotFound, BizCode{
    User: User{
        Id:   1,
        Name: "John",
    },
})
fmt.Println(code)
```

即在错误码中我们可以根据业务场景注入一些自定义的错误码扩展数据，以方便上层获取错误码后做进一步处理。

## 改写中间件

我们将上面自定义的错误码应用到请求返回中间件中，顶层业务逻辑也可以获取到错误码对应的详情再进一步做相关的业务处理。中间件返回的数据结构也可以自定义重写，例如其中返回的 `code` 字段也不一定是整形数值，可以完全自定义。

```go
func ResponseHandler(r *ghttp.Request) {
    r.Middleware.Next()
    // There's custom buffer content, it then exits current handler.
    if r.Response.BufferLength() > 0 {
        return
    }
    var (
        err  = r.GetError()
        res  = r.GetHandlerResponse()
        code = gerror.Code(err)
    )
    if code == gcode.CodeNil && err != nil {
        code = gcode.CodeInternalError
    }
    if detail, ok := code.Detail().(BizCode); ok {
        g.Log().Errorf(r.Context(), `error caused by user "%+v"`, detail.User)
    }
    r.Response.WriteJson(ghttp.DefaultHandlerResponse{
        Code:    gcode.CodeOK.Code(),
        Message: gcode.CodeOK.Message(),
        Data:    res,
    })
}
```
:::tip
在框架`Server`默认的日志中会自动打印`Detail`数据。
:::
