Handle API errors
mainErrors in linodego provide structured information about the failure:
- Single Entity Errors (e.g.,
Get): If an entity is not found or you lack permission, the returned object will beniland the error will contain the status code and message. - List Errors: If a list request fails (e.g., due to an invalid filter), the returned slice will be empty (
[]) and the error will contain the failure details. - Write Errors: When performing
POSTorPUTrequests, multiple field validation errors are aggregated into a single error string in the format:[400] [field1] problem; [field2] problem.
Error Fields:
err.Error(): String representation (e.g.,[404] Not Found).err.Code: The HTTP status code as a string (e.g.,404).err.Message: The error message (e.g.,Not Found).
// Example: 404 Error
linode, err := linodego.GetInstance(context.Background(), 555)
// linode == nil
// err.Error() == "[404] Not Found"
// err.Code == "404"
// err.Message == "Not Found"
// Example: Invalid Filter Error
linodes, err := linodego.ListInstances(context.Background(), linodego.NewListOptions(0, "{\"foo\":bar}"))
// linodes == []
// err.Error() == "[400] [X-Filter] Cannot filter on foo"