Handle API errors
This example shows how to handle specific errors (e.g., 404) by type-asserting the returned error and inspecting its fields.
package main
import (
"fmt"
"log"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/session"
"github.com/softlayer/softlayer-go/sl"
)
var invalidId = 0
func main() {
sess := session.New()
_, err := services.GetVirtualGuestService(sess).
Id(invalidId).
DeleteObject()
if err != nil {
if apiErr, ok := err.(sl.Error); ok && apiErr.StatusCode == 404 {
fmt.Println("Virtual Guest not found (already deleted?)")
} else {
log.Fatal("Unhandled error:", err)
}
}
}