首页 > 后端开发 > Golang > 正文

Golang 中具体类型的错误片段

PHPz
发布: 2024-02-05 22:12:12
转载
1070人浏览过

golang 中具体类型的错误片段

问题内容

我正在 go 中尝试错误包装,并有一个返回包装的自定义错误类型的函数。我想做的是迭代预期错误列表并测试函数的输出是否包含这些预期错误。

我发现将自定义错误放入 []error 意味着自定义错误的类型将为 *fmt.wraperror,这意味着 errors.as() 几乎总是返回 true。

作为示例,请考虑以下代码:

package main

import (
    "errors"
    "fmt"
)

type anothererror struct {
}

func (e *anothererror) error() string {
    return "another error"
}

type missingattrerror struct {
    missingattr string
}

func (e *missingattrerror) error() string {
    return fmt.sprintf("missing attribute: %s", e.missingattr)
}

func dosomething() error {
    e := &missingattrerror{missingattr: "key"}
    return fmt.errorf("dosomething(): %w", e)
}

func main() {
    err := dosomething()
    expectederrone := &missingattrerror{}
    expectederrtwo := &anothererror{}
    expectederrs := []error{expectederrone, expectederrtwo}

    fmt.printf("is err '%v' type '%t'?: %t\n", err, expectederrone, errors.as(err, &expectederrone))
    fmt.printf("is err '%v' type '%t'?: %t\n", err, expectederrtwo, errors.as(err, &expectederrtwo))

    for i := range expectederrs {
        fmt.printf("is err '%v' type '%t'?: %t\n", err, expectederrs[i], errors.as(err, &expectederrs[i]))

    }
}
登录后复制

其输出为

is err 'dosomething(): missing attribute: key' type '*main.missingattrerror'?: true
is err 'dosomething(): missing attribute: key' type '*main.anothererror'?: false
is err 'dosomething(): missing attribute: key' type '*fmt.wraperror'?: true
is err 'dosomething(): missing attribute: key' type '*fmt.wraperror'?: true
登录后复制

理想情况下我希望输出是

立即学习go语言免费学习笔记(深入)”;

Is err 'DoSomething(): missing attribute: Key' type '*main.MissingAttrError'?: true
Is err 'DoSomething(): missing attribute: Key' type '*main.AnotherError'?: false
Is err 'DoSomething(): missing attribute: Key' type '*main.MissingAttrError'?: true
Is err 'DoSomething(): missing attribute: Key' type '*main.AnotherError'?: false
登录后复制

出现错误的原因是我希望能够为每个测试用例条目定义预期错误的列表。假设我知道为函数提供某些输入将使其沿着一条路径返回包含特定错误的错误。

挖错网
挖错网

一款支持文本、图片、视频纠错和AIGC检测的内容审核校对平台。

挖错网 28
查看详情 挖错网

如何将 *fmt.wraperror 类型从 []error 切片转换回原始类型,以便我可以将其与 error.as 一起使用?

我知道我可以使用 将其强制转换为特定类型。(anothererror),但为了在迭代切片时使其工作,我必须对函数可能返回的每个可能的错误执行此操作,不是吗?)


正确答案


您可以使用以下方法欺骗 errors.as

func main() {
    err := DoSomething()
    m := &MissingAttrError{}
    a := &AnotherError{}
    expected := []any{&m, &a}

    for i := range expected {
        fmt.Printf("Is err '%v' type '%T'?: %t\n", err, expected[i], errors.As(err, expected[i]))
    }
}
登录后复制

打印的类型不是您所期望的,但 errors.as 按其应有的方式工作。

您的示例不起作用的原因是您传递给 errors.as 的是 *error。因此,包装的错误值(即 err)被直接分配给目标值。在我的示例中,传递给 errors.as 的值是 **anothererror,并且 err 不能分配给 *anothererror

以上就是Golang 中具体类型的错误片段的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号