Files
influx-cli/internal/api/error.go
Daniel Moran 73dc5ef63b feat(api): improve codegen outputs (#41)
* Use `[]byte` for generated request bodies when the source schema has `format: byte`
* Gzip-compress request bodies when `Content-Encoding: gzip` is set
* Require that all models returned as error conditions in our API implement the `error` interface
* Move the implementation for `api.HealthCheck` out of `ping.go` and into `api/error.go`


Co-authored-by: William Baker <55118525+wbaker85@users.noreply.github.com>
2021-04-26 10:10:45 -04:00

38 lines
815 B
Go

package api
import (
"fmt"
"strings"
)
// Extensions to let our API error types be used as "standard" errors.
func (o *Error) Error() string {
if o.Message != "" && o.Err != nil {
var b strings.Builder
b.WriteString(o.Message)
b.WriteString(": ")
b.WriteString(*o.Err)
return b.String()
} else if o.Message != "" {
return o.Message
} else if o.Err != nil {
return *o.Err
}
return fmt.Sprintf("<%s>", o.Code)
}
func (o *HealthCheck) Error() string {
if o.Status == HEALTHCHECKSTATUS_PASS {
// Make sure we aren't misusing HealthCheck responses.
panic("successful healthcheck used as an error!")
}
var message string
if o.Message != nil {
message = *o.Message
} else {
message = fmt.Sprintf("check %s failed", o.Name)
}
return fmt.Sprintf("health check failed: %s", message)
}