Dan Anglin
d52bb3fdf4
If an error occurs when sending a request to an instance, try and decode and print the error message back to the user.
36 lines
737 B
Go
36 lines
737 B
Go
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
//
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package client
|
|
|
|
import "fmt"
|
|
|
|
type ResponseError struct {
|
|
StatusCode int
|
|
Message string
|
|
MessageDecodeErr error
|
|
}
|
|
|
|
func (e ResponseError) Error() string {
|
|
if e.MessageDecodeErr != nil {
|
|
return fmt.Sprintf(
|
|
"received HTTP code %d from the instance but was unable to decode the error message: %v",
|
|
e.StatusCode,
|
|
e.MessageDecodeErr,
|
|
)
|
|
}
|
|
|
|
if e.Message == "" {
|
|
return fmt.Sprintf(
|
|
"received HTTP code %d from the instance but no error message was provided",
|
|
e.StatusCode,
|
|
)
|
|
}
|
|
|
|
return fmt.Sprintf(
|
|
"message received from the instance: (%d) %q",
|
|
e.StatusCode,
|
|
e.Message,
|
|
)
|
|
}
|