Skip to content

Commit

Permalink
parsing html json
Browse files Browse the repository at this point in the history
  • Loading branch information
nitesh3108 committed Feb 12, 2024
1 parent 450e3ca commit a74f758
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,21 +610,7 @@ func (c *client) GetReferer() string {

func parseJSONHTMLError(r *http.Response) error {
// check the content type of the response
contentType := r.Header.Get("Content-Type")
switch {
case strings.HasPrefix(contentType, "application/json"):
jsonErr := &JSONError{}
// decode JSON error response
err := json.NewDecoder(r.Body).Decode(jsonErr)
if err != nil {
return err
}
jsonErr.StatusCode = r.StatusCode
if len(jsonErr.Err) > 0 && jsonErr.Err[0].Message == "" {
jsonErr.Err[0].Message = r.Status
}
return jsonErr
case strings.HasPrefix(contentType, "text/html"):
if r.Header.Get("Content-Type") == "text/html" {
htmlError := &HTMLError{}
// decode HTML error response
doc, err := goquery.NewDocumentFromReader(r.Body)
Expand All @@ -641,9 +627,18 @@ func parseJSONHTMLError(r *http.Response) error {
htmlError.Message = doc.Find("title").Text()
}
return htmlError
default:
// Unexpected content type
return fmt.Errorf("unexpected content type: %s", contentType)
} else {

Check warning on line 630 in api/api.go

View workflow job for this annotation

GitHub Actions / golangci-lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
jsonErr := &JSONError{}
// decode JSON error response
err := json.NewDecoder(r.Body).Decode(jsonErr)
if err != nil {
return err
}
jsonErr.StatusCode = r.StatusCode
if len(jsonErr.Err) > 0 && jsonErr.Err[0].Message == "" {
jsonErr.Err[0].Message = r.Status
}
return jsonErr
}
}

Expand Down

0 comments on commit a74f758

Please sign in to comment.