Exemplo n.º 1
0
 public function testErrorHandler()
 {
     try {
         Frapi_Error::errorHandler(E_ERROR, 'This is a PHP error', 'ErrorFile.php', '99');
     } catch (Frapi_Error $e) {
         $error = $e->getErrorArray();
     }
     $this->assertEquals(400, $e->getCode());
     $this->assertEquals('Bad Request', $e->getReasonPhrase());
     $this->assertEquals('This is a PHP error (Error Number: 1), (File: ErrorFile.php at line 99)', $error['errors'][0]['message']);
     $this->assertEquals('PHP Fatal error', $error['errors'][0]['name']);
     $this->assertEquals('', $error['errors'][0]['at']);
 }
Exemplo n.º 2
0
    /**
     * Private function to get error (statically, APC, or DB).
     *
     * This function tries to locate the error in progressively
     * slower datastores (static class variable, APC, database)
     * and will store the loaded errors in the faster stores.
     *
     * @param string $error_name Name of error.
     * @param string $error_msg  The actual message of the error.
     * @param int    $http_code  This might be hard to grasp however, we are in a web
     *                           industry dealing with the web. The code you are sending
     *                           to your exception should really be represented by the
     *                           HTTP Code returned to your users.
     *
     * @return array An array with the content of the error.
     */
    private static function _get($error_name, $error_msg = false, $http_code = false)
    {
        if (!self::$_statically_loaded) {
            $errors = Frapi_Internal::getCached('Errors.user-defined');

            if ($errors) {
                self::$_errors = $errors;
            } elseif ($errors = self::_getErrorsFromDb()) {
                self::$_errors = $errors;
                Frapi_Internal::setCached('Errors.user-defined', $errors);
            }

            self::$_statically_loaded = true;
        }

        if (isset(self::$_errors[$error_name])) {
            $error = self::$_errors[$error_name];

            if ($error_msg !== false) {
                $error['message'] = $error_msg;
            }

            if ($http_code !== false) {
                $error['http_code'] = $http_code;
            }

            return $error;
        }

        return array(
            'name'      => $error_name,
            'message'   => $error_msg !== false ? $error_msg : $error_name,
            'http_code' => $http_code !== false  ? $http_code : '400',
        );
    }
Exemplo n.º 3
0
 /**
  * Private function to get error (statically, APC, or DB).
  *
  * This function tries to locate the error in progressively
  * slower datastores (static class variable, APC, database)
  * and will store the loaded errors in the faster stores.
  *
  * @param string $error_name Name of error.
  * @param string $error_msg   The actual message of the error.
  * @param int    $http_code   This might be hard to grasp however, we are in a web
  *                            industry dealing with the web. The code you are sending
  *                            to your exception should really be represented by the
  *                            HTTP Code returned to your users.
  * @param string $http_phrase The http phrase associated with the http_code
  *
  * @return array An array with the content of the error.
  */
 private static function _get($error_name, $error_msg = false, $http_code = false, $http_phrase = false)
 {
     if (!self::$_statically_loaded) {
         $errors = Frapi_Internal::getCached('Errors.user-defined');
         if ($errors) {
             self::$_errors = $errors;
         } elseif ($errors = self::_getErrorsFromDb()) {
             self::$_errors = $errors;
             Frapi_Internal::setCached('Errors.user-defined', $errors);
         }
         self::$_statically_loaded = true;
     }
     if (isset(self::$_errors[$error_name])) {
         $error = self::$_errors[$error_name];
         if ($error_msg !== false) {
             $error['message'] = $error_msg;
         }
         if ($http_code !== false) {
             $error['http_code'] = $http_code;
         }
         if ($http_phrase !== false) {
             $error['http_phrase'] = $http_phrase;
         } else {
             if (isset(Frapi_Response::$http_reason[$error['http_code']]) && !$error['http_phrase']) {
                 $error['http_phrase'] = Frapi_Response::$http_reason[$error['http_code']];
             } else {
                 if ($error['message'] !== false && !$error['http_phrase']) {
                     $error['http_phrase'] = $error['message'];
                 }
             }
         }
         return $error;
     }
     if ($http_phrase === false) {
         if (isset(Frapi_Response::$http_reason[$http_code])) {
             $http_phrase = Frapi_Response::$http_reason[$http_code];
         } else {
             if ($error_msg !== false) {
                 $http_phrase = $error_msg;
             }
         }
     }
     return array('name' => $error_name, 'message' => $error_msg !== false ? $error_msg : $error_name, 'http_code' => $http_code !== false ? $http_code : '400', 'http_phrase' => $http_phrase !== false ? $http_phrase : 'Bad Request');
 }