Skip to content

Commit

Permalink
chore: convert 403 to ErrUserNotSignedIn error
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Jan 31, 2023
1 parent f9c9473 commit 7d21927
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions leetcode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
ErrTooManyRequests = errors.New("you have submitted too frequently, please submit again later")
ErrQuestionNotFound = errors.New("no such question")
ErrContestNotStarted = errors.New("contest has not started")
ErrUserNotSignedIn = errors.New("user not signed in")
ErrUserNotSignedIn = errors.New("user not signed in, your cookies may have expired")
)

type unexpectedStatusCode struct {
Expand All @@ -38,29 +38,11 @@ type unexpectedStatusCode struct {
func (e unexpectedStatusCode) Error() string {
body := "<empty>"
if len(e.Body) > 0 {
body = string(e.Body)
body = string(e.Body)[:1024]
}
return fmt.Sprintf("unexpected status code: %d, body: %s", e.Code, body)
}

// nolint: unused
type errorResponse struct {
ErrorMsg *string `json:"error"`
}

// nolint: unused
func (e errorResponse) Ok() bool {
return e.ErrorMsg == nil
}

// nolint: unused
func (e errorResponse) Error() string {
if e.ErrorMsg != nil {
return *e.ErrorMsg
}
return ""
}

type Client interface {
BaseURI() string
Inspect(typ string) (map[string]any, error)
Expand Down Expand Up @@ -209,8 +191,11 @@ func (c *cnClient) send(req *http.Request, result any, failure any) (*http.Respo
if err != nil {
return resp, err
}
if resp.StatusCode == http.StatusTooManyRequests {
switch resp.StatusCode {
case http.StatusTooManyRequests:
return resp, ErrTooManyRequests
case http.StatusForbidden:
return resp, ErrUserNotSignedIn
}

if !(200 <= resp.StatusCode && resp.StatusCode <= 299) {
Expand Down

0 comments on commit 7d21927

Please sign in to comment.