Reputation: 31
I just starting working with golang and errors handling inside it. I working with gophercloud SDK and the error interface implemented in the way that it is providing all necessary data for me.
Here is my code:
vol, err := volumes.Get(client, volumeID).Extract()
if err != nil {
log.Printf("Error with getting volume from gophercloud/openstack: %s\n", err)
http.Error(w, err.Error(), 404)
return
}
and here is a data of error that I'm seeing in debug mode
As you can see on the screen there is an actual error code, is there a possibility to extract it from error variable to use in the http.Error() method?
Thank you.
Upvotes: 0
Views: 171
Reputation: 4471
You can use errors.As
(GoLang 1.13+):
type errorString struct {
msg string
val string
}
func (e *errorString) Error() string {
return e.msg
}
func newError(msg, val string) error {
return &errorString{msg, val}
}
func TestErrorAdditionalInfo(t *testing.T) {
err := newError("MSG", "SOME_VAL")
var es *errorString
if errors.As(err, &es) {
fmt.Printf("Err[msg: %s; val: %s]\n", es.msg, es.val)
}
}
Upvotes: 1