目录
  • 原因(causes)
  • 组件(component)
  • 响应类型(responsetype)
  • goerror 接口

前言

go语言很强大并且现在也十分流行 — 许多项目都是用go语言来实现的,如kubernetes。go语言的一个有趣特性是它的多值返回功能提供了一种与其他编程语言不同的错误处理方法。 go将error视为具有预定义类型的值,其本身是一个interface类型。然而,编写多层体系结构应用程序并使用api暴露应用的特性需要有包含更多上下文信息的error处理,而不仅仅是一个值。 本文我们将探讨如何封装go的error类型以在应用程序中带来更大的价值。

用户自定义类型

我们将重写的go里自带的error类型,首先从一个自定义的错误类型开始,该错误类型将在程序中识别为error类型。因此,我们引入一个封装了go的 error的新自定义error类型。

type goerror struct {
   error
}

上下文数据

当我们在go中说error是一个值时,它是字符串值 – 任何实现了error() string函数的类型都可以视作error类型。将字符串值视为error会使跨层的error处理复杂化,因此处理error字符串信息并不是正确的方法。所以我们可以把字符串和错误码解耦:

type goerror struct {
   error
   code    string
}

现在对error的处理将基于错误码code字段而不是字符串。让我们通过上下文数据进一步对错误字符串进行解耦,在上下文数据中可以使用i18n包进行国际化。

type goerror struct {
   error
   code    string
   data    map[string]interface{}
}

data包含用于构造错误字符串的上下文数据。错误字符串可以通过数据模板化:

//i18n def
“invalidparamvalue”: “invalid parameter value ‘{{.actual}}’, expected ‘{{.expected}}’ for ‘{{.name}}'”

在i18n定义文件中,错误码code将会映射到使用data构建的模板化的错误字符串中。

原因(causes)

error可能发生在任何一层,有必要为每一层提供处理error的选项,并在不丢失原始error值的情况下进一步使用附加的上下文信息对error进行包装。goerror结构体可以用causes进一步封装,用来保存整个错误堆栈。

type goerror struct {
   error
   code    string
   data    map[string]interface{}
   causes  []error
}

如果必须保存多个error数据,则causes是一个数组类型,并将其设置为基本error类型,以便在程序中包含该原因的第三方错误。

组件(component)

标记层组件将有助于识别error发生在哪一层,并且可以避免不必要的error wrap。例如,如果service类型的error组件发生在服务层,则可能不需要wrap error。检查组件信息将有助于防止暴露给用户不应该通知的error,比如数据库error:

type goerror struct {
   error
   code      string
   data      map[string]interface{}
   causes    []error
   component errcomponent
}

type errcomponent string
const (
   errservice  errcomponent = "service"
   errrepo     errcomponent = "repository"
   errlib      errcomponent = "library"
)

响应类型(responsetype)

添加一个错误响应类型这样可以支持error分类,以便于了解什么错误类型。例如,可以根据响应类型(如notfound)对error进行分类,像dbrecordnotfound、resourcenotfound、usernotfound等等的error都可以归类为 notfound error。这在多层应用程序开发过程中非常有用,而且是可选的封装:

type goerror struct {
   error
   code         string
   data         map[string]interface{}
   causes       []error
   component    errcomponent
   responsetype responseerrtype
}

type responseerrtype string

const (
   badrequest    responseerrtype = "badrequest"
   forbidden     responseerrtype = "forbidden"
   notfound      responseerrtype = "notfound"
   alreadyexists responseerrtype = "alreadyexists"
)

重试

在少数情况下,出现error会进行重试。retry字段可以通过设置retryable标记来决定是否要进行error重试:

type goerror struct {
   error
   code         string
   message      string
   data         map[string]interface{}
   causes       []error
   component    errcomponent
   responsetype responseerrtype
   retryable    bool
}

goerror 接口

通过定义一个带有goerror实现的显式error接口,可以简化error检查:

package goerr

type error interface {
   error

   code() string
   message() string
   cause() error
   causes() []error
   data() map[string]interface{}
   string() string
   responseerrtype() responseerrtype
   setresponsetype(r responseerrtype) error
   component() errcomponent
   setcomponent(c errcomponent) error
   retryable() bool
   setretryable() error
}

抽象error

有了上述的封装方式,更重要的是对error进行抽象,将这些封装保存在同一地方,并提供error函数的可重用性

func resourcenotfound(id, kind string, cause error) goerror {
   data := map[string]interface{}{"kind": kind, "id": id}
   return goerror{
      code:         "resourcenotfound",
      data:         data,
      causes:       []error{cause},
      component:    errservice,
      responsetype: notfound,
      retryable:    false,
   }
}

这个error函数抽象了resourcenotfound这个error,开发者可以使用这个函数来返回error对象而不是每次创建一个新的对象:

//userservice
user, err := u.repo.finduser(ctx, userid)
if err != nil {
   if err.responsetype == notfound {
      return resourcenotfound(useruid, "user", err)
   }
   return err
}

结论

我们演示了如何使用添加上下文数据的自定义go的error类型,从而使得error在多层应用程序中更有意义。你可以在这里[1]看到完整的代码实现和定义。

到此这篇关于go应用中优雅处理error的文章就介绍到这了,更多相关go优雅处理error内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

参考资料

[1] 这里: https://gist.github.com/prathabk/744367cbfc70435c56956f650612d64b