<commit_before><?php

namespace JumpCloud\Request;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request as HttpRequest;
use GuzzleHttp\Psr7\Response;
use JumpCloud\Response\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
 * Class RequestHandler
 *
 * @package JumpCloud
 */
class RequestHandler
{
    /** @var LoggerInterface */
    private $logger;

    /**
     * RequestHandler constructor.
     *
     * @param LoggerInterface|null $logger
     */
    public function __construct(LoggerInterface $logger = null)
    {
        $this->logger = $logger ?: new NullLogger();
    }

    /**
     * @param GuzzleClient $client
     * @param RequestInterface $request
     * @return ResponseInterface
     */
    public function handle(GuzzleClient $client, RequestInterface $request)
    {
        try {
            $response = $client->send(
                new HttpRequest(
                    $request->getMethod(),
                    $request->getUri(),
                    $request->getHeaders(),
                    \GuzzleHttp\json_encode($request->getBody())
                )
            );
        } catch (\Exception $e) {
            $response = new Response(500);

            if ($e instanceof ClientException) {
                $response = new Response(401);
            }
        }

        return $request->getResponseFactory()->create($response);
    }
}<commit_msg>feat: adds logging for exceptions in the request handler<commit_after><?php

namespace JumpCloud\Request;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request as HttpRequest;
use GuzzleHttp\Psr7\Response;
use JumpCloud\Response\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
 * Class RequestHandler
 *
 * @package JumpCloud
 */
class RequestHandler
{
    /** @var LoggerInterface */
    private $logger;

    /**
     * RequestHandler constructor.
     *
     * @param LoggerInterface|null $logger
     */
    public function __construct(LoggerInterface $logger = null)
    {
        $this->logger = $logger ?: new NullLogger();
    }

    /**
     * @param GuzzleClient $client
     * @param RequestInterface $request
     * @return ResponseInterface
     */
    public function handle(GuzzleClient $client, RequestInterface $request)
    {
        try {
            $response = $client->send(
                new HttpRequest(
                    $request->getMethod(),
                    $request->getUri(),
                    $request->getHeaders(),
                    \GuzzleHttp\json_encode($request->getBody())
                )
            );
        } catch (\Exception $e) {
            $this->logger->debug(
                sprintf('Exception thrown during Guzzle request'),
                [
                    'message' => $e->getMessage(),
                    'line' => $e->getLine(),
                    'file' => $e->getFile(),
                    'trace' => $e->getTraceAsString()
                ]
            );

            $response = new Response(500);

            if ($e instanceof ClientException) {
                $response = new Response(401);
            }
        }

        return $request->getResponseFactory()->create($response);
    }
}
{'fraction_non_alphanumeric': 0.11565034544908381, 'fraction_numerical': 0.004806248122559327, 'mean_word_length': 1.93909973521624, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '3420239', 'n_tokens_mistral': 911, 'n_tokens_neox': 852, 'n_words': 188}