     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

    <?php

    final class CatapultApiException extends \Exception
    {
      public function __construct($result) {
        $this->result = $result;
        $code = 0;
        if (isset($result['error_code']) && is_int($result['error_code'])) {
          $code = $result['error_code'];
        }
        if (isset($result['error_description'])) {
          // OAuth 2.0 Draft 10 style
          $msg = $result['error_description'];
        } else if (isset($result['error']) && is_array($result['error'])) {
          // OAuth 2.0 Draft 00 style
          $msg = $result['error']['message'];
        } else if (isset($result['error_msg'])) {
          // Rest server style
          $msg = $result['error_msg'];
        } else {
          //$msg = 'Unknown Error. Check getResult()';
          $msg = $this->result;
        }

        parent::__construct($msg, $code);

        if (Catapult\Log::isOn()) {
          $trace = $this->getTrace();
          /** get the last line we got an error on, would be the user's file **/

          $line = $trace[sizeof($trace) - 1]['line'];
          Catapult\Log::write(time(), "line: " . $line, $this->result);
        }
      }

      public function getResult() {
        return $this->result;
      }

      public function getType() {
        if (isset($this->result['error'])) {
          $error = $this->result['error'];
          if (is_string($error)) {
            // OAuth 2.0 Draft 10 style
            return $error;
          } else if (is_array($error)) {
            // OAuth 2.0 Draft 00 style
            if (isset($error['type'])) {
              return $error['type'];
            }
          }
        }
        return 'Exception';
      }

      public function __toString() {
        $str = $this->getType() . ': ';
        if ($this->code != 0) {
          $str .= $this->code . ': ';
        }
        return $str . $this->message;
      }
    }

    /**
     * Catapult API Warning added 1/30/2015
     * to not stop execution when dealing
     * with minor errors. Things
     * that can arise like bad formed
     * input, this does not always need
     * to stop the program and should only
     * show as warning
     */
    class CatapultApiWarning {
        /** show output of what happened **/
        /** todo make CatapultApiWarning recognize global constants **/
        public function __construct($message) {
          $this->message = $message;
        }
        public function __toString() {
          return "CatapultWarning: " . $this->message . "\n";
        }
    }