- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Go应用中优雅处理Error的技巧总结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
Go语言很强大并且现在也十分流行 — 许多项目都是用Go语言来实现的,如Kubernetes。Go语言的一个有趣特性是它的多值返回功能提供了一种与其他编程语言不同的错误处理方法。 Go将error视为具有预定义类型的值,其本身是一个interface类型。然而,编写多层体系结构应用程序并使用api暴露应用的特性需要有包含更多上下文信息的error处理,而不仅仅是一个值。 本文我们将探讨如何封装Go的error类型以在应用程序中带来更大的价值.
我们将重写的Go里自带的error类型,首先从一个自定义的错误类型开始,该错误类型将在程序中识别为error类型。因此,我们引入一个封装了Go的 error的新自定义Error类型.
1
2
3
|
type GoError
struct
{
error
}
|
当我们在Go中说error是一个值时,它是字符串值 - 任何实现了Error() string函数的类型都可以视作error类型。将字符串值视为error会使跨层的error处理复杂化,因此处理error字符串信息并不是正确的方法。所以我们可以把字符串和错误码解耦:
1
2
3
4
|
type GoError
struct
{
error
Code string
}
|
现在对error的处理将基于错误码Code字段而不是字符串。让我们通过上下文数据进一步对错误字符串进行解耦,在上下文数据中可以使用i18n包进行国际化.
1
2
3
4
5
|
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构建的模板化的错误字符串中.
error可能发生在任何一层,有必要为每一层提供处理error的选项,并在不丢失原始error值的情况下进一步使用附加的上下文信息对error进行包装。GoError结构体可以用Causes进一步封装,用来保存整个错误堆栈.
1
2
3
4
5
6
|
type GoError
struct
{
error
Code string
Data map[string]interface{}
Causes []error
}
|
如果必须保存多个error数据,则causes是一个数组类型,并将其设置为基本error类型,以便在程序中包含该原因的第三方错误.
标记层组件将有助于识别error发生在哪一层,并且可以避免不必要的error wrap。例如,如果service类型的error组件发生在服务层,则可能不需要wrap error。检查组件信息将有助于防止暴露给用户不应该通知的error,比如数据库error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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"
)
|
添加一个错误响应类型这样可以支持error分类,以便于了解什么错误类型。例如,可以根据响应类型(如NotFound)对error进行分类,像DbRecordNotFound、ResourceNotFound、UserNotFound等等的error都可以归类为 NotFound error。这在多层应用程序开发过程中非常有用,而且是可选的封装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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重试
1
2
3
4
5
6
7
8
9
10
|
type GoError
struct
{
error
Code string
Message string
Data map[string]interface{}
Causes []error
Component ErrComponent
ResponseType ResponseErrType
Retryable
bool
}
|
通过定义一个带有GoError实现的显式error接口,可以简化error检查
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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函数的可重用性 。
1
2
3
4
5
6
7
8
9
10
11
|
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对象而不是每次创建一个新的对象:
1
2
3
4
5
6
7
8
|
//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内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
参考资料 。
[1] 这里: https://gist.github.com/prathabk/744367cbfc70435c56956f650612d64b 。
原文链接:https://mp.weixin.qq.com/s/q2VrY8ksNsmYzvqhUNv72A 。
最后此篇关于Go应用中优雅处理Error的技巧总结的文章就讲到这里了,如果你想了解更多关于Go应用中优雅处理Error的技巧总结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在通过 labrepl 工作,我看到了一些遵循此模式的代码: ;; Pattern (apply #(apply f %&) coll) ;; Concrete example user=> (a
我从未向应用商店提交过应用,但我会在不久的将来提交。 到目前为止,我对为 iPhone 而非 iPad 进行设计感到很自在。 我了解,通过将通用PAID 应用放到应用商店,客户只需支付一次就可以同时使
我有一个应用程序,它使用不同的 Facebook 应用程序(2 个不同的 AppID)在 Facebook 上发布并显示它是“通过 iPhone”/“通过 iPad”。 当 Facebook 应用程序
我有一个要求,我们必须通过将网站源文件保存在本地 iOS 应用程序中来在 iOS 应用程序 Webview 中运行网站。 Angular 需要服务器来运行应用程序,但由于我们将文件保存在本地,我们无法
所以我有一个单页客户端应用程序。 正常流程: 应用程序 -> OAuth2 服务器 -> 应用程序 我们有自己的 OAuth2 服务器,因此人们可以登录应用程序并获取与用户实体关联的 access_t
假设我有一个安装在用户设备上的 Android 应用程序 A,我的应用程序有一个 AppWidget,我们可以让其他 Android 开发人员在其中以每次安装成本为基础发布他们的应用程序推广广告。因此
Secrets of the JavaScript Ninja中有一个例子它提供了以下代码来绕过 JavaScript 的 Math.min() 函数,该函数需要一个可变长度列表。 Example:
当我分别将数组和对象传递给 function.apply() 时,我得到 NaN 的 o/p,但是当我传递对象和数组时,我得到一个数字。为什么会发生这种情况? 由于数组也被视为对象,为什么我无法使用它
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章ASP转换格林威治时间函数DateDiff()应用由作者收集整理,如果你
我正在将列表传递给 map并且想要返回一个带有合并名称的 data.frame 对象。 例如: library(tidyverse) library(broom) mtcars %>% spl
我有一个非常基本的问题,但我不知道如何实现它:我有一个返回数据框,其中每个工具的返回值是按行排列的: tmp<-as.data.frame(t(data.frame(a=rnorm(250,0,1)
我正在使用我的 FB 应用创建群组并邀请用户加入我的应用群组,第一次一切正常。当我尝试创建另一个组时,出现以下错误: {"(OAuthException - #4009) (#4009) 在有更多用户
我们正在开发一款类似于“会说话的本”应用程序的 child 应用程序。它包含大量用于交互式动画的 JPEG 图像序列。 问题是动画在 iPad Air 上播放正常,但在 iPad 2 上播放缓慢或滞后
我关注 clojure 一段时间了,它的一些功能非常令人兴奋(持久数据结构、函数式方法、不可变状态)。然而,由于我仍在学习,我想了解如何在实际场景中应用,证明其好处,然后演化并应用于更复杂的问题。即,
我开发了一个仅使用挪威语的应用程序。该应用程序不使用本地化,因为它应该仅以一种语言(挪威语)显示。但是,我已在 Info.plist 文件中将“本地化 native 开发区域”设置为“no”。我还使用
读完 Anthony's response 后上a style-related parser question ,我试图说服自己编写单体解析器仍然可以相当紧凑。 所以而不是 reference ::
multicore 库中是否有类似 sapply 的东西?还是我必须 unlist(mclapply(..)) 才能实现这一点? 如果它不存在:推理是什么? 提前致谢,如果这是一个愚蠢的问题,我们深表
我喜欢在窗口中弹出结果,以便更容易查看和查找(例如,它们不会随着控制台继续滚动而丢失)。一种方法是使用 sink() 和 file.show()。例如: y <- rnorm(100); x <- r
我有一个如下所示的 spring mvc Controller @RequestMapping(value="/new", method=RequestMethod.POST) public Stri
我正在阅读 StructureMap关于依赖注入(inject),首先有两部分初始化映射,具体类类型的接口(interface),另一部分只是实例化(请求实例)。 第一部分需要配置和设置,这是在 Bo
我是一名优秀的程序员,十分优秀!