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?