Merge HTTPError into Error
This commit is contained in:
parent
9edacef768
commit
f01ac7b716
25
errors.go
25
errors.go
@ -1,25 +0,0 @@
|
||||
package lemmy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HTTPError represents an error caused by a non-200 HTTP status code
|
||||
type HTTPError struct {
|
||||
Code int
|
||||
}
|
||||
|
||||
func (he HTTPError) Error() string {
|
||||
return fmt.Sprintf("%d %s", he.Code, http.StatusText(he.Code))
|
||||
}
|
||||
|
||||
// Error represents an error returned by the Lemmy API
|
||||
type Error struct {
|
||||
ErrStr string
|
||||
Code int
|
||||
}
|
||||
|
||||
func (le Error) Error() string {
|
||||
return fmt.Sprintf("%d %s: %s", le.Code, http.StatusText(le.Code), le.ErrStr)
|
||||
}
|
17
lemmy.go
17
lemmy.go
@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@ -139,6 +140,20 @@ func (c *Client) getReq(ctx context.Context, method string, path string, data, r
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Error represents an error returned by the Lemmy API
|
||||
type Error struct {
|
||||
ErrStr string
|
||||
Code int
|
||||
}
|
||||
|
||||
func (le Error) Error() string {
|
||||
if le.ErrStr != "" {
|
||||
return fmt.Sprintf("%d %s: %s", le.Code, http.StatusText(le.Code), le.ErrStr)
|
||||
} else {
|
||||
return fmt.Sprintf("%d %s", le.Code, http.StatusText(le.Code))
|
||||
}
|
||||
}
|
||||
|
||||
// emptyResponse is a response without any fields.
|
||||
// It has an Error field to capture any errors.
|
||||
type emptyResponse struct {
|
||||
@ -153,7 +168,7 @@ func resError(res *http.Response, err Optional[string]) error {
|
||||
ErrStr: errstr,
|
||||
}
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
return HTTPError{
|
||||
return Error{
|
||||
Code: res.StatusCode,
|
||||
}
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user