<commit_before><?php
/**
 * Exception class for Crunchmail classes
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015 (c) Oasiswork
 * @license   https://opensource.org/licenses/MIT MIT
 *
 * @link https://github.com/crunchmail/crunchmail-client-php
 */

namespace Crunchmail\Exception;

/**
 * ApiException class
 */
class ApiException extends \Exception
{
    /**
     * Custom ApiException constructor
     *
     * @param string $message
     * @param int $code
     * @param Exception $previous
     */
    public function __construct($message, $code = 0, $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }

    /**
     * Output exception
     */
    public function __toString()
    {
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
    }

    /**
     * Return the last error as an html string
     *
     * @param boolean $showErrorKey Show the key of each error
     * @return string
     */
    public function toHtml($params=[])
    {
        $body = $this->getDetail();

        if (false === $body)
        {
            return $body;
        }

        return self::formatResponseOutput($body, $params);
    }

    /**
     * Extract error details
     *
     * @return stdClass
     */
    public function getDetail()
    {
        // guzzle exception
        $previous = $this->getPrevious();

        // in case we have a response, we try to format it as a string
        if ($previous->hasResponse())
        {
            $response = $previous->getResponse();
            $msg      = json_decode($response->getBody());
        }

        // if body was empty, we need to return the exception message instead
        if (!isset($msg) || count((array) $msg) === 0)
        {
            $msg = new \stdClass();
            $msg->error = [$previous->getMessage()];
        }

        return $msg;
    }

    /**
     * Format a body response as a unique HTML string
     * This is mainly a debugging function, you should probably generate your
     * own HTML output.
     *
     * @param object $body Guzzle Response
     * @param boolean $showErrorKey show error keys in output
     * @return string
     *
     * @todo add string sanitize
     */
    public static function formatResponseOutput($body, $params=[])
    {
        $defaultParams = ['showErrorKey' => true];
        $params        = array_merge($defaultParams, $params);

        // invalid error, it's a string, we handle the error
        if (is_string($body))<commit_msg>clean: Clean code formating<commit_after><?php
/**
 * Exception class for Crunchmail classes
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015 (c) Oasiswork
 * @license   https://opensource.org/licenses/MIT MIT
 *
 * @link https://github.com/crunchmail/crunchmail-client-php
 */

namespace Crunchmail\Exception;

/**
 * ApiException class
 */
class ApiException extends \Exception
{
    /**
     * Custom ApiException constructor
     *
     * @param string $message
     * @param int $code
     * @param Exception $previous
     */
    public function __construct($message, $code = 0, $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }

    /**
     * Output exception
     */
    public function __toString()
    {
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
    }

    /**
     * Return the last error as an html string
     *
     * @param boolean $showErrorKey Show the key of each error
     * @return string
     */
    public function toHtml($params = [])
    {
        $body = $this->getDetail();

        if (false === $body)
        {
            return $body;
        }

        return self::formatResponseOutput($body, $params);
    }

    /**
     * Extract error details
     *
     * @return stdClass
     */
    public function getDetail()
    {
        // guzzle exception
        $previous = $this->getPrevious();

        // in case we have a response, we try to format it as a string
        if ($previous->hasResponse())
        {
            $response = $previous->getResponse();
            $msg      = json_decode($response->getBody());
        }

        // if body was empty, we need to return the exception message instead
        if (!isset($msg) || count((array) $msg) === 0)
        {
            $msg = new \stdClass();
            $msg->error = [$previous->getMessage()];
        }

        return $msg;
    }

    /**
     * Format a body response as a unique HTML string
     * This is mainly a debugging function, you should probably generate your
     * own HTML output.
     *
     * @param object $body Guzzle Response
     * @param boolean $showErrorKey show error keys in output
     * @return string
     *
     * @todo add string sanitize
     */
    public static function formatResponseOutput($body, $params = [])
    {
        $defaultParams = ['showErrorKey' => true];
        $params        = array_merge($defaultParams, $params);

        // invalid error, it's a string, we handle the error
        if (is_string($body))
{'fraction_non_alphanumeric': 0.12579239302694137, 'fraction_numerical': 0.002377179080824089, 'mean_word_length': 2.052599758162031, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 4, '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': '4914342', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1389, 'n_words': 420}