1
0
Fork 0
golang-github-nicholas-fedo.../pkg/util/jsonclient/error.go
Daniel Baumann c0c4addb85
Adding upstream version 0.8.9.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-05-22 10:16:14 +02:00

37 lines
704 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package jsonclient
import (
"errors"
"fmt"
)
// Error contains additional HTTP/JSON details.
type Error struct {
StatusCode int
Body string
err error
}
// Error returns the string representation of the error.
func (je Error) Error() string {
return je.String()
}
// String provides a human-readable description of the error.
func (je Error) String() string {
if je.err == nil {
return fmt.Sprintf("unknown error (HTTP %v)", je.StatusCode)
}
return je.err.Error()
}
// ErrorBody extracts the request body from an error if its a jsonclient.Error.
func ErrorBody(e error) string {
var jsonError Error
if errors.As(e, &jsonError) {
return jsonError.Body
}
return ""
}