Merge HTTPError into Error

This commit is contained in:
Elara 2023-10-04 21:08:55 -07:00
parent 9edacef768
commit f01ac7b716
2 changed files with 16 additions and 26 deletions

View File

@ -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)
}

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -139,6 +140,20 @@ func (c *Client) getReq(ctx context.Context, method string, path string, data, r
return res, nil 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. // emptyResponse is a response without any fields.
// It has an Error field to capture any errors. // It has an Error field to capture any errors.
type emptyResponse struct { type emptyResponse struct {
@ -153,7 +168,7 @@ func resError(res *http.Response, err Optional[string]) error {
ErrStr: errstr, ErrStr: errstr,
} }
} else if res.StatusCode != http.StatusOK { } else if res.StatusCode != http.StatusOK {
return HTTPError{ return Error{
Code: res.StatusCode, Code: res.StatusCode,
} }
} else { } else {