Skip to content

Commit

Permalink
🪞 make to be able to show status code
Browse files Browse the repository at this point in the history
  • Loading branch information
Omochice committed Sep 28, 2021
1 parent 9b85ea2 commit 4073271
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,33 @@ func Translate(Text string, setting Setting) ([]string, error) {
return results, err
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
var data map[string]interface{}
errors := map[int]string{
400: "Bad request. Please check error message and your parameters.",
403: "Authorization failed. Please supply a valid auth_key parameter.",
404: "The requested resource could not be found.",
413: "The request size exceeds the limit.",
414: "The request URL is too long. You can avoid this error by using a POST request instead of a GET request, and sending the parameters in the HTTP body.",
429: "Too many requests. Please wait and resend your request.",
456: "Quota exceeded. The character limit has been reached.",
503: "Resource currently unavailable. Try again later.",
529: "Too many requests. Please wait and resend your request.",
} // this from https://www.deepl.com/docs-api/accessing-the-api/error-handling/
e := json.NewDecoder(resp.Body).Decode(&data)
baseErrorText := fmt.Sprintf("Invalid response [%d %s]",
resp.StatusCode,
http.StatusText(resp.StatusCode))
if t, ok := errors[resp.StatusCode]; ok {
baseErrorText += fmt.Sprintf(" %s", t)
}
if e != nil {
return results, fmt.Errorf("%s", baseErrorText)
} else {
return results, fmt.Errorf("%s, %s", baseErrorText, data["message"])
}
}

translateResponse, err := ParseResponse(resp)
if err != nil {
return []string{}, err
Expand Down

0 comments on commit 4073271

Please sign in to comment.