<commit_before><?php

namespace Fuzz\ApiServer;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Response;
use Fuzz\ApiServer\Exception\HttpException;
use Illuminate\Contracts\Support\Arrayable;

class Responder
{
	/**
	 * Send a response.
	 *
	 * @param mixed $data
	 * @param int   $status_code
	 * @param array $headers
	 * @param array $context
	 * @return \Illuminate\Http\JsonResponse
	 */
	final public function send($data, $status_code, $headers, $context)
	{
		if ($data instanceof Arrayable) {
			$data = $data->toArray();
		}

		return Response::json(array_merge(compact('data'), $context), $status_code, $headers);
	}

	/**
	 * Notify the caller of failure.
	 *
	 * @param \Exception $exception
	 * @return \Illuminate\Http\JsonResponse
	 */
	final public function sendException(\Exception $exception)
	{
		/**
		 * Handle known HTTP exceptions RESTfully.
		 */
		if ($exception instanceof HttpException) {
			$error = $exception->getMessage();

			return $this->send(
				$exception->getData(),
				$exception::STATUS_CODE,
				$exception->getHeaders(),
				compact('error')
			);
		}

		/**
		 * Contextualize response with verbose information outside production.
		 *
		 * Report only "unknown" errors in production.
		 */
		if (Config::get('app.debug')) {
			$error = [
				'message' => $exception->getMessage(),
				'class' => get_class($exception),
				'file' => $exception->getFile(),
				'line' => $exception->getLine(),
			];
		} else {
			$error = 'E_UNKNOWN';
		}

		return $this->send(
			null,
			HttpException::STATUS_CODE,
			[],
			compact('error')
		);
	}
}<commit_msg>Refactor responder to be sensitive to League OAuthException<commit_after><?php

namespace Fuzz\ApiServer;

use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Config;
use Illuminate\Contracts\Support\Arrayable;
use Symfony\Component\HttpFoundation\Response;
use League\OAuth2\Server\Exception\OAuthException;
use Fuzz\ApiServer\Exception\OAuthException as FuzzOAuthException;

class Responder
{
	/**
	 * Send a response.
	 *
	 * @param mixed $data
	 * @param int   $status_code
	 * @param array $headers
	 * @return \Illuminate\Http\JsonResponse
	 */
	final public function send($data, $status_code, $headers)
	{
		if ($data instanceof Arrayable) {
			$data = $data->toArray();
		}

		return new JsonResponse($data, $status_code, $headers);
	}

	/**
	 * Notify the caller of failure.
	 *
	 * @param \Exception $exception
	 * @return \Illuminate\Http\JsonResponse
	 */
	final public function sendException(\Exception $exception)
	{
		/**
		 * Handle known HTTP exceptions RESTfully.
		 */
		if ($exception instanceof OAuthException) {
			$error             = $exception->errorType;
			$error_description = $exception->getMessage();
			$status_code       = $exception->httpStatusCode;
			$headers           = $exception->getHttpHeaders();

			$error_data = ($exception instanceof FuzzOAuthException) ? $exception->errorData : null;
		} else {
			/**
			 * Contextualize response with verbose information outside production.
			 *
			 * Report only "unknown" errors in production.
			 */
			$error = 'unknown';

			if (Config::get('app.debug')) {
				$error_description = [
					'message' => $exception->getMessage(),
					'class'   => get_class($exception),
					'file'    => $exception->getFile(),
					'line'    => $exception->getLine(),
				];
			}

			$status_code = Response::HTTP_INTERNAL_SERVER_ERROR;

			$headers = [];
		}

		return $this->send(
			compact('error', 'error_description', 'error_data'), $status_code, $headers
		);
	}
}
{'fraction_non_alphanumeric': 0.14325685506435368, 'fraction_numerical': 0.0002797985450475658, 'mean_word_length': 3.571611253196931, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '7104164', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1082, 'n_words': 244}