2021-01-10 19:52:54 +00:00
|
|
|
package remote
|
2021-01-08 22:43:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-05-02 22:41:02 +00:00
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2021-01-08 22:43:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RequestErrors struct {
|
|
|
|
Errors []RequestError `json:"errors"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestError struct {
|
|
|
|
response *http.Response
|
|
|
|
Code string `json:"code"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Detail string `json:"detail"`
|
|
|
|
}
|
|
|
|
|
2021-05-02 22:41:02 +00:00
|
|
|
// IsRequestError checks if the given error is of the RequestError type.
|
2021-01-08 22:43:03 +00:00
|
|
|
func IsRequestError(err error) bool {
|
2021-05-02 22:41:02 +00:00
|
|
|
var rerr *RequestError
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return errors.As(err, &rerr)
|
|
|
|
}
|
2021-01-08 22:43:03 +00:00
|
|
|
|
2021-05-02 22:41:02 +00:00
|
|
|
// AsRequestError transforms the error into a RequestError if it is currently
|
|
|
|
// one, checking the wrap status from the other error handlers. If the error
|
|
|
|
// is not a RequestError nil is returned.
|
|
|
|
func AsRequestError(err error) *RequestError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var rerr *RequestError
|
|
|
|
if errors.As(err, &rerr) {
|
|
|
|
return rerr
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-08 22:43:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 22:41:02 +00:00
|
|
|
// Error returns the error response in a string form that can be more easily
|
|
|
|
// consumed.
|
2021-01-08 22:43:03 +00:00
|
|
|
func (re *RequestError) Error() string {
|
|
|
|
c := 0
|
|
|
|
if re.response != nil {
|
|
|
|
c = re.response.StatusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("Error response from Panel: %s: %s (HTTP/%d)", re.Code, re.Detail, c)
|
|
|
|
}
|
|
|
|
|
2021-05-02 22:41:02 +00:00
|
|
|
// StatusCode returns the status code of the response.
|
|
|
|
func (re *RequestError) StatusCode() int {
|
|
|
|
return re.response.StatusCode
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:37:56 +00:00
|
|
|
type SftpInvalidCredentialsError struct{}
|
2021-01-08 22:43:03 +00:00
|
|
|
|
2021-02-02 04:59:17 +00:00
|
|
|
func (ice SftpInvalidCredentialsError) Error() string {
|
2021-01-08 22:43:03 +00:00
|
|
|
return "the credentials provided were invalid"
|
|
|
|
}
|