3 ms·
I actually rarely type that very snippet. In like 3/4 of the cases, I make the error more specific, like if err != nil { return nil, fmt.Errorf("co
by sacado2 8y ago
I actually rarely type that very snippet. In like 3/4 of the cases, I make the error more specific, like
if err != nil {
return nil, fmt.Errorf("could not parse file %s: %v", path, err)
}
the remainder being either the snippet you put above or more complex error management.
- erik_seaberg 8y agoYou can enrich err.Error(), but when you do you lose the type of the original error and any other fields it may have had. This is something the language can and should do more reliably (i.e., sugar could mutate err.Error() and prepend "MyStruct.myMethod failed: ", or better yet add a damn stack trace).