<commit_before><?php

namespace Mangopixel\Responder\Traits;

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Http\JsonResponse;
use Mangopixel\Responder\Exceptions\ApiException;
use Mangopixel\Responder\Exceptions\ResourceNotFoundException;
use Mangopixel\Responder\Exceptions\UnauthorizedException;
use Mangopixel\Responder\Exceptions\ValidationFailedException;
use Symfony\Component\Console\Output\ConsoleOutput;

/**
 * You may apply this trait to your exceptions handler to give you access to methods
 * you may use to let the package catch and handle any API exceptions.
 *
 * @package Laravel Responder
 * @author  <NAME> <<EMAIL>>
 * @license The MIT License
 */
trait HandlesApiErrors
{
    /**
     * Checks if the exception extends from the package API exception.
     *
     * @param  Exception $e
     * @return bool
     */
    protected function isApiError( Exception $e ):bool
    {
        return $e instanceof ApiException;
    }

    /**
     * Transforms and renders api responses.
     *
     * @param  Exception $e
     * @return $this
     */
    protected function handleApiErrors( Exception $e )
    {
        $this->transformExceptions( $e );

        if ( $e instanceof ApiException ) {
            return $this->renderApiResponse( $e );
        } else {
            $this->renderConsoleResponse( $e );
        }

        return $this;
    }

    /**
     * Checks if the application is currently in testing mode.
     *
     * @return bool
     */
    protected function isRunningTests():bool
    {
        return app()->runningUnitTests();
    }

    /**
     * Renders readable responses for console, useful for testing.
     *
     * @param  Exception $e
     * @return $this
     */
    protected function handleTestErrors( Exception $e ):Handler
    {
        if ( ! $e instanceof ApiException && app()->runningInConsole() ) {
            $this->renderConsoleResponse( $e );
        }

        return $this;
    }

    /**
     * Transform Laravel exceptions into API exceptions.
     *
     * @param  Exception $e
     * @return void
     * @throws ResourceNotFoundException
     * @throws UnauthorizedException<commit_msg>Fix method names in handler<commit_after><?php

namespace Mangopixel\Responder\Traits;

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Http\JsonResponse;
use Mangopixel\Responder\Exceptions\ApiException;
use Mangopixel\Responder\Exceptions\ResourceNotFoundException;
use Mangopixel\Responder\Exceptions\UnauthorizedException;
use Mangopixel\Responder\Exceptions\ValidationFailedException;
use Symfony\Component\Console\Output\ConsoleOutput;

/**
 * You may apply this trait to your exceptions handler to give you access to methods
 * you may use to let the package catch and handle any API exceptions.
 *
 * @package Laravel Responder
 * @author  <NAME> <<EMAIL>>
 * @license The MIT License
 */
trait HandlesApiErrors
{
    /**
     * Checks if the exception extends from the package API exception.
     *
     * @param  Exception $e
     * @return bool
     */
    protected function isApiError( Exception $e ):bool
    {
        return $e instanceof ApiException;
    }

    /**
     * Transforms and renders api responses.
     *
     * @param  Exception $e
     * @return $this
     */
    protected function renderApiErrors( Exception $e )
    {
        $this->transformExceptions( $e );

        if ( $e instanceof ApiException ) {
            return $this->renderApiResponse( $e );
        } else {
            $this->renderConsoleResponse( $e );
        }

        return $this;
    }

    /**
     * Checks if the application is currently in testing mode.
     *
     * @return bool
     */
    protected function isRunningTests():bool
    {
        return app()->runningUnitTests();
    }

    /**
     * Renders readable responses for console, useful for testing.
     *
     * @param  Exception $e
     * @return $this
     */
    protected function renderTestErrors( Exception $e ):Handler
    {
        if ( ! $e instanceof ApiException && app()->runningInConsole() ) {
            $this->renderConsoleResponse( $e );
        }

        return $this;
    }

    /**
     * Transform Laravel exceptions into API exceptions.
     *
     * @param  Exception $e
     * @return void
     * @throws ResourceNotFoundException
     * @throws UnauthorizedException
{'fraction_non_alphanumeric': 0.09478260869565218, 'fraction_numerical': 0.0, 'mean_word_length': 2.6837469975980786, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '3199334', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1244, 'n_words': 355}