<commit_before>package main

import (
	"fmt"
	"github.com/alloy-d/goauth"
)

func Retweet(id uint64, o *oauth.OAuth) error {
	url := fmt.Sprintf(
		"https://api.twitter.com/1.1/statuses/retweet/%d.json",
		id,
	)
	response, err := o.Post(
		url,
		map[string]string{
			"trim_user": "true",
		})
	if err != nil {
		return err
	}
	if response.StatusCode != 200 {
		return fmt.Errorf("twitter error: %s", response.Status)
	}
	return nil
}
<commit_msg>Twitter sometimes refuses to retweet, with an obscure error.<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"github.com/alloy-d/goauth"
	"log"
)

type twitterError struct {
	// https://code.google.com/p/twitter-api/issues/detail?id=1333
	Errors string
}

func Retweet(id uint64, o *oauth.OAuth) error {
	url := fmt.Sprintf(
		"https://api.twitter.com/1.1/statuses/retweet/%d.json",
		id,
	)
	response, err := o.Post(
		url,
		map[string]string{
			"trim_user": "true",
		})
	if err != nil {
		return err
	}
	if response.StatusCode != 200 {
		var extra twitterError
		if response.Header.Get("Content-Type") == "application/json; charset=utf-8" {
			dec := json.NewDecoder(response.Body)
			_ = dec.Decode(&extra)
		}
		if response.StatusCode == 403 && extra.Errors == "sharing is not permissible for this status (Share validations failed)" {
			// this is not a temporary error, so stop trying
			log.Printf("twitter refused retweet: %d", id)
			return nil
		}
		return fmt.Errorf("twitter error: %s: %q", response.Status, extra.Errors)
	}
	return nil
}

{'fraction_non_alphanumeric': 0.15303430079155672, 'fraction_numerical': 0.014511873350923483, 'mean_word_length': 3.583081570996979, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, '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': '2253798', 'n_tokens_mistral': 607, 'n_tokens_neox': 555, 'n_words': 132}