From 8eefc2432182e016566c6bbd4e1cc442cf1576c0 Mon Sep 17 00:00:00 2001
From: xamgore <xamgore@ya.ru>
Date: Sat, 11 Jun 2016 00:17:24 +0300
Subject: [PATCH] Create new server-logic based error

---
 .../classes/Controller/Handler.php            | 13 +++++++
 .../system/classes/HTTP/Exception/422.php     |  3 ++
 .../classes/Kohana/HTTP/Exception/422.php     | 36 +++++++++++++++++++
 .../system/classes/Kohana/Response.php        |  1 +
 4 files changed, 53 insertions(+)
 create mode 100644 ~dev_rating/system/classes/HTTP/Exception/422.php
 create mode 100644 ~dev_rating/system/classes/Kohana/HTTP/Exception/422.php

diff --git a/~dev_rating/application/classes/Controller/Handler.php b/~dev_rating/application/classes/Controller/Handler.php
index f39efc26e..75f4b38ae 100644
--- a/~dev_rating/application/classes/Controller/Handler.php
+++ b/~dev_rating/application/classes/Controller/Handler.php
@@ -16,4 +16,17 @@ class Controller_Handler extends Controller
         $this->post = Validation::factory(Arr::map('trim', $_POST));
         $this->get = Validation::factory(Arr::map('trim', $_GET));
     }
+    
+    public function execute() {
+        $res = parent::execute();
+        
+        if (empty($res->body()))
+            $res->body(json_encode([]));
+        
+        return $res;
+    }
+    
+    protected function fail() {
+        throw HTTP_Exception::factory(422)->request($this->request);
+    }
 }
diff --git a/~dev_rating/system/classes/HTTP/Exception/422.php b/~dev_rating/system/classes/HTTP/Exception/422.php
new file mode 100644
index 000000000..3aa67f080
--- /dev/null
+++ b/~dev_rating/system/classes/HTTP/Exception/422.php
@@ -0,0 +1,3 @@
+<?php defined('SYSPATH') OR die('No direct script access.');
+
+class HTTP_Exception_422 extends Kohana_HTTP_Exception_422 {}
diff --git a/~dev_rating/system/classes/Kohana/HTTP/Exception/422.php b/~dev_rating/system/classes/Kohana/HTTP/Exception/422.php
new file mode 100644
index 000000000..a599b104d
--- /dev/null
+++ b/~dev_rating/system/classes/Kohana/HTTP/Exception/422.php
@@ -0,0 +1,36 @@
+<?php defined('SYSPATH') OR die('No direct script access.');
+
+class Kohana_HTTP_Exception_422 extends HTTP_Exception_Expected
+{
+    /** @var int Unprocessable Entity */
+    protected $_code = 422;
+
+    /**
+     * Specifies the list of allowed HTTP methods
+     *
+     * @param  array $methods List of allowed methods
+     * @return $this
+     */
+    public function allowed($methods) {
+        if (is_array($methods)) {
+            $methods = implode(',', $methods);
+        }
+
+        $this->headers('allow', $methods);
+
+        return $this;
+    }
+
+    /**
+     * Validate this exception contains everything needed to continue.
+     *
+     * @throws Kohana_Exception
+     * @return bool
+     */
+    public function check() {
+//        if ($location = $this->headers('allow') === null)
+//            throw new Kohana_Exception('A list of allowed methods must be specified');
+
+        return true;
+    }
+}
diff --git a/~dev_rating/system/classes/Kohana/Response.php b/~dev_rating/system/classes/Kohana/Response.php
index 686997915..67abf2d7a 100644
--- a/~dev_rating/system/classes/Kohana/Response.php
+++ b/~dev_rating/system/classes/Kohana/Response.php
@@ -75,6 +75,7 @@ class Kohana_Response implements HTTP_Response {
 		415 => 'Unsupported Media Type',
 		416 => 'Requested Range Not Satisfiable',
 		417 => 'Expectation Failed',
+        422 => 'Unprocessable Entity',
 
 		// Server Error 5xx
 		500 => 'Internal Server Error',
-- 
GitLab