<commit_before>package util;

import exception.BadRequestException;
import exception.FilestackIOException;
import exception.HandleNotFoundException;
import exception.PolicySignatureException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * Checks for failed responses from Filestack and throws an exception rather than returning the response.
 * Makes error handling simpler by centralizing this logic in one place.
 */
public class FailedResponseInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);

        // If it was successful we don't need to do anything else
        if (response.isSuccessful())
            return response;

        // We're only concerned with responses from Filestack domains
        if (!request.url().host().contains("filestack"))
            return response;

        // Throw a descriptive subclass of FilestackIOException
        switch (response.code()) {
            case 400:
                throw new BadRequestException();
            case 403:
                throw new PolicySignatureException();
            case 404:
                throw new HandleNotFoundException();
            default:
                String message = String.format("Request failed: %d %s", response.code(), response.message());
                throw new FilestackIOException(message);
        }
    }
}<commit_msg>Make failure interceptor generic<commit_after>package util;

import exception.FilestackIOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.IOException;

/**
 * Checks for failed responses from Filestack and throws an exception rather than returning the response.
 * Makes error handling simpler by centralizing this logic in one place.
 */
public class FailedResponseInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        ResponseBody body = response.body();

        // If it was successful we don't need to do anything else
        if (response.isSuccessful())
            return response;

        if (body != null)
            throw new FilestackIOException(body.string());
        else
            throw new FilestackIOException(request.url().host() + " " + response.code() + " " + response.message());
    }
}
{'fraction_non_alphanumeric': 0.0791586998087954, 'fraction_numerical': 0.006118546845124283, 'mean_word_length': 2.957639939485628, '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': '1209931', 'n_tokens_mistral': 632, 'n_tokens_neox': 597, 'n_words': 215}