douzhi7070 2016-08-24 10:06
浏览 48
已采纳

解释Golang错误代码

So most examples of go error handling I see just pass any errors back up the stack. At some point these need interpreting and this is what I am trying to do. Here's a snippet of my attempt:

    resp, err := http.Get(string(url))
    defer out_count.Dec()

    if err != nil {

           switch err {
            case http.ErrBodyReadAfterClose:
                    fmt.Println("Read after close error")
            case http.ErrMissingFile:
                    fmt.Println("Missing File")
            {some more cases here}
            case io.EOF:
                    fmt.Println("EOF error found")
            default:
                    fmt.Printf("Error type is %T
", err)
                    panic(err)
            }
            return

This isn't working though for my current case(edited to remove url}:

ERROR: Failed to crawl "http://{removed URL}"
Error type is *url.Error
panic: Get http://{removed url}: EOF

goroutine 658 [running]:
runtime.panic(0x201868, 0x106352c0)
        /usr/lib/go/src/pkg/runtime/panic.c:279 +0x1a0
github.com/cbehopkins/grab/grab.crawl(0x10606210, 0x27, 0x105184b0, 0x105184e0, 0x10500460)

I can't figure out a way to get the switch statement to catch this error since the text of the error changes every time and has no explicit value I can catch against. (as the URL changes all the time). Now maybe I could do some sort of regex match in the case statement or sub-slice the error string, but that feels like a very bad way to solve this problem.

Any suggestions? There must be an idiomatic way to catch errors such as this surely?

  • 写回答

2条回答 默认 最新

  • duanjuda5789 2016-08-24 10:29
    关注

    The simplest way would be to have package level error values in your code:

    var URLFetchError = errors.New("Cannot fetch URL")
    
    url := "http://www.google.com"
    res, err := http.Get(url)
    if err != nil {
        return URLFetchError
    }
    

    The switch then becomes:

    switch err {
    case http.ErrBodyReadAfterClose:
        fmt.Println("Read after close error")
    case URLFetchError:
        fmt.Println("Error fetching URL")
    

    If you want to pass more information with the error, you can create your own custom errors:

    type MyError struct {
        URL string
    }
    
    func (e MyError) Error() string {
        return fmt.Sprintf("Error getting: %v", e.URL)
    }
    

    Then, you can create this error whenever required. For example:

    url := "http://www.google.com"
    res, err := http.Get(url)
    if err != nil {
        return MyError{url}
    }
    

    Finally, in your error checking method, you can use type switches instead of simple switches to get the error:

    switch err.(type) {
    case MyError:
        fmt.Println("Error:", err)
    default:
        fmt.Println("No Error")
    }
    

    In your case, since you have a mix of regular error, you can include this check in a nested switch:

    switch err {
    case http.ErrBodyReadAfterClose:
        fmt.Println("Read after close error")
    case http.ErrMissingFile:
        fmt.Println("Missing File")
    case io.EOF:
        fmt.Println("EOF error found")
    default: // check for custom errors
        switch err.(type) {
        case MyError:
            fmt.Println("custom error:", err)
        default:
            panic(err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置