From 84a6debe9e84acbb179c021ec7675faf6fe9c7c3 Mon Sep 17 00:00:00 2001 From: Roman Steinberg <roman.accs@gmail.com> Date: Tue, 16 Aug 2016 15:41:55 +0300 Subject: [PATCH] ADD: http api exceptions implementation --- .../classes/HTTP/API/Exception.php | 33 +++++++++++++++++++ .../application/views/errors/http.twig | 1 - .../application/views/errors/http_api.twig | 4 +++ .../system/classes/HTTP/API/Exception/400.php | 5 +++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 ~dev_rating/application/classes/HTTP/API/Exception.php create mode 100644 ~dev_rating/application/views/errors/http_api.twig create mode 100644 ~dev_rating/system/classes/HTTP/API/Exception/400.php diff --git a/~dev_rating/application/classes/HTTP/API/Exception.php b/~dev_rating/application/classes/HTTP/API/Exception.php new file mode 100644 index 000000000..48f8231e5 --- /dev/null +++ b/~dev_rating/application/classes/HTTP/API/Exception.php @@ -0,0 +1,33 @@ +<?php defined('SYSPATH') or die('No direct script access.'); + +class HTTP_API_Exception extends HTTP_Exception +{ + /** + * Creates an HTTP_Exception of the specified type. + * + * @param integer $code the http status code + * @param string $message status message, custom content to display with error + * @param array $variables translation variables + * @return HTTP_Exception + */ + public static function factory($code, $message = NULL, array $variables = NULL, Exception $previous = NULL) { + $class = 'HTTP_API_Exception_' . $code; + return new $class($message, $variables, $previous); + } + + public function get_response() { + // Lets log the Exception, Just in case it's important! + Kohana_Exception::log($this); + + $twig = Twig::factory('errors/http_api'); + + $twig->code = $this->getCode(); + $twig->message = $this->getMessage(); + $twig->link = URL::site(''); + $response = Response::factory() + ->status($this->getCode()) + ->body($twig); + + return $response; + } +} \ No newline at end of file diff --git a/~dev_rating/application/views/errors/http.twig b/~dev_rating/application/views/errors/http.twig index 0fce6ba49..66f2ffd1e 100644 --- a/~dev_rating/application/views/errors/http.twig +++ b/~dev_rating/application/views/errors/http.twig @@ -18,7 +18,6 @@ <div class="logotype alignLeft"> {{ HTML.anchor('/', System.Title, {'title': 'Перейти на главную'})|raw }} </div> - <div class="faculty alignLeft">{{ User.FacultyName }}</div> <div class="navigation"> Ooops! </div> diff --git a/~dev_rating/application/views/errors/http_api.twig b/~dev_rating/application/views/errors/http_api.twig new file mode 100644 index 000000000..b88069e70 --- /dev/null +++ b/~dev_rating/application/views/errors/http_api.twig @@ -0,0 +1,4 @@ +{ + "code": "{{ code }}", + "message": "{{ message }}" +} \ No newline at end of file diff --git a/~dev_rating/system/classes/HTTP/API/Exception/400.php b/~dev_rating/system/classes/HTTP/API/Exception/400.php new file mode 100644 index 000000000..23c73e85e --- /dev/null +++ b/~dev_rating/system/classes/HTTP/API/Exception/400.php @@ -0,0 +1,5 @@ +<?php defined('SYSPATH') OR die('No direct script access.'); + +class HTTP_API_Exception_400 extends HTTP_API_Exception { + protected $_code = 400; +} -- GitLab