<commit_before>package phraseapp

import (
	"encoding/json"
	"fmt"
	"net/http"
)

func handleResponseStatus(resp *http.Response, expectedStatus int) error {
	switch resp.StatusCode {
	case expectedStatus:
		return nil
	case 400:
		e := new(ErrorResponse)
		err := json.NewDecoder(resp.Body).Decode(&e)
		if err != nil {
			return err
		}
		return e
	case 404:
		return fmt.Errorf("not found")
	case 422:
		e := new(ValidationErrorResponse)
		err := json.NewDecoder(resp.Body).Decode(&e)
		if err != nil {
			return err
		}
		return e
	case 429:
		e, err := NewRateLimitError(resp)
		if err != nil {
			return err
		}
		return e
	default:
		return fmt.Errorf("unexpected status code (%d) received; expected %d", resp.StatusCode, expectedStatus)
	}
}<commit_msg>adds nicer error messages<commit_after>package phraseapp

import (
	"encoding/json"
	"fmt"
	"net/http"
)

const docsURL = `http://docs.phraseapp.com/api/v2/`

func further() string {
	return fmt.Sprintf("\nFor further information see:\n%s", docsURL)
}

func handleResponseStatus(resp *http.Response, expectedStatus int) error {
	switch resp.StatusCode {
	case expectedStatus:
		return nil
	case 400:
		e := new(ErrorResponse)
		err := json.NewDecoder(resp.Body).Decode(&e)
		if err != nil {
			return err
		}
		return e
	case 401:
		return fmt.Errorf("401 - %s\nThe credentials you provided are invalid.%s", http.StatusText(resp.StatusCode), further())
	case 403:
		return fmt.Errorf("403 - %s\nYou are not authorized to perform the requested action on the requested resource. Check if your provided access_token has the correct scope.%s", http.StatusText(resp.StatusCode), further())
	case 404:
		return fmt.Errorf("404 - Resource Not Found\nThe resource you requested or referenced resources you required do either not exist or you do not have the authorization to request this resource.")
	case 422:
		e := new(ValidationErrorResponse)
		err := json.NewDecoder(resp.Body).Decode(&e)
		if err != nil {
			return err
		}
		return e
	case 429:
		e, err := NewRateLimitError(resp)
		if err != nil {
			return err
		}
		return e
	default:
		return fmt.Errorf("Unexpected HTTP Status Code (%d %s) received; expected %d %s.%s", resp.StatusCode, http.StatusText(resp.StatusCode), expectedStatus, http.StatusText(expectedStatus), further())
	}
}
{'fraction_non_alphanumeric': 0.11613745106568073, 'fraction_numerical': 0.017398869073510223, 'mean_word_length': 4.088495575221239, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5485432', 'n_tokens_mistral': 813, 'n_tokens_neox': 726, 'n_words': 230}