Untitled
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
)
type GenericOpenAPIError struct {
reason string
body io.Reader
// other fields
}
func (e GenericOpenAPIError) Error() string {
return e.reason
}
func (e GenericOpenAPIError) Body() io.Reader {
return e.body
}
func main() {
err := apiCall()
if err != nil {
fmt.Printf("Error: %s \n", err)
}
switch e := err.(type) {
case *GenericOpenAPIError:
details, _ := ioutil.ReadAll(e.Body())
fmt.Printf("Details: %s \n", details)
default:
// no error was returned
}
}
func apiCall() error {
if true { // smth happened
return &GenericOpenAPIError{
reason: "Houston, we have a problem",
body: bytes.NewReader([]byte(`{"message": "Internal error"}`)),
}
}
return nil
}
Go
INFO