Overview

Namespaces

  • Crunchmail
    • Collections
    • Entities
    • Exception
    • PHPUnit
    • Resources

Classes

  • Crunchmail\Client
  • Crunchmail\Collections\GenericCollection
  • Crunchmail\Entities\AttachmentEntity
  • Crunchmail\Entities\ContactEntity
  • Crunchmail\Entities\ContactListEntity
  • Crunchmail\Entities\ContactQueueEntity
  • Crunchmail\Entities\DomainEntity
  • Crunchmail\Entities\GenericEntity
  • Crunchmail\Entities\MessageEntity
  • Crunchmail\Entities\RecipientEntity
  • Crunchmail\PHPUnit\IsEntityConstraint
  • Crunchmail\PHPUnit\IsGenericCollectionConstraint
  • Crunchmail\PHPUnit\IsGenericEntityConstraint
  • Crunchmail\PHPUnit\IsGenericResourceConstraint
  • Crunchmail\PHPUnit\IsResourceConstraint
  • Crunchmail\PHPUnit\TestCase
  • Crunchmail\Resources\DomainsResource
  • Crunchmail\Resources\GenericResource
  • Crunchmail\Resources\PreviewSendResource

Exceptions

  • Crunchmail\Exception\ApiException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Exception class for Crunchmail classes
  4:  *
  5:  * @author    Yannick Huerre <dev@sheoak.fr>
  6:  * @copyright 2015 (c) Oasiswork
  7:  * @license   https://opensource.org/licenses/MIT MIT
  8:  *
  9:  * @link https://github.com/crunchmail/crunchmail-client-php
 10:  */
 11: 
 12: namespace Crunchmail\Exception;
 13: 
 14: /**
 15:  * ApiException class
 16:  */
 17: class ApiException extends \Exception
 18: {
 19:     /**
 20:      * Custom ApiException constructor
 21:      *
 22:      * @param string $message
 23:      * @param int $code
 24:      * @param Exception $previous
 25:      */
 26:     public function __construct($message, $code = 0, $previous = null)
 27:     {
 28:         parent::__construct($message, $code, $previous);
 29:     }
 30: 
 31:     /**
 32:      * Output exception
 33:      */
 34:     public function __toString()
 35:     {
 36:         return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
 37:     }
 38: 
 39:     /**
 40:      * Return the last error as an html string
 41:      *
 42:      * @param boolean $showErrorKey Show the key of each error
 43:      * @return string
 44:      */
 45:     public function toHtml($params = [])
 46:     {
 47:         $body = $this->getDetail();
 48: 
 49:         if (false === $body)
 50:         {
 51:             return $body;
 52:         }
 53: 
 54:         return self::formatResponseOutput($body, $params);
 55:     }
 56: 
 57:     /**
 58:      * Extract error details
 59:      *
 60:      * @return stdClass
 61:      */
 62:     public function getDetail()
 63:     {
 64:         // guzzle exception
 65:         $previous = $this->getPrevious();
 66: 
 67:         // in case we have a response, we try to format it as a string
 68:         if ($previous->hasResponse())
 69:         {
 70:             $response = $previous->getResponse();
 71:             $msg      = json_decode($response->getBody());
 72:         }
 73: 
 74:         // if body was empty, we need to return the exception message instead
 75:         if (!isset($msg) || count((array) $msg) === 0)
 76:         {
 77:             $msg = new \stdClass();
 78:             $msg->error = [$previous->getMessage()];
 79:         }
 80: 
 81:         return $msg;
 82:     }
 83: 
 84:     /**
 85:      * Format a body response as a unique HTML string
 86:      * This is mainly a debugging function, you should probably generate your
 87:      * own HTML output.
 88:      *
 89:      * @param object $body Guzzle Response
 90:      * @param boolean $showErrorKey show error keys in output
 91:      * @return string
 92:      *
 93:      * @todo add string sanitize
 94:      */
 95:     public static function formatResponseOutput($body, $params = [])
 96:     {
 97:         $defaultParams = ['showErrorKey' => true];
 98:         $params        = array_merge($defaultParams, $params);
 99: 
100:         // invalid error, it's a string, we handle the error
101:         if (is_string($body))
102:         {
103:             return $body;
104:         }
105: 
106:         // build a string from the complex response
107:         $out = "";
108:         foreach ((array) $body as $k => $v)
109:         {
110:             // list of error fields with error messages
111:             $out .= '<p>';
112:             if (!is_numeric($k) && $params['showErrorKey'])
113:             {
114:                 $out .= $k . ' : ';
115:             }
116:             foreach ($v as $str)
117:             {
118:                 $out .= htmlentities($str) . "<br>";
119:             }
120:             $out .= '</p>';
121:         }
122: 
123:         $out = empty($out) ? 'Unknow error' : $out;
124: 
125:         return $out;
126:     }
127: }
128: 
API documentation generated by ApiGen