|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant PHP |
|
7
|
|
|
* Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
declare(strict_types=1); |
|
33
|
|
|
|
|
34
|
|
|
namespace Platine\Framework\Handler\Error; |
|
35
|
|
|
|
|
36
|
|
|
use Platine\Framework\Http\Exception\HttpMethodNotAllowedException; |
|
37
|
|
|
use Platine\Framework\Http\Response\RestResponse; |
|
38
|
|
|
use Platine\Http\ResponseInterface; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @class RestErrorHandler |
|
42
|
|
|
* @package Platine\Framework\Handler\Error |
|
43
|
|
|
*/ |
|
44
|
|
|
class RestErrorHandler extends ErrorHandler |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritodc} |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function errorResponse(): ResponseInterface |
|
50
|
|
|
{ |
|
51
|
|
|
$extras = []; |
|
52
|
|
|
if ($this->detail) { |
|
53
|
|
|
$extras['exception'] = [ |
|
54
|
|
|
'type' => get_class($this->exception), |
|
55
|
|
|
'code' => $this->exception->getCode(), |
|
56
|
|
|
'message' => $this->exception->getMessage(), |
|
57
|
|
|
'file' => $this->exception->getFile(), |
|
58
|
|
|
'line' => $this->exception->getLine(), |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
$response = new RestResponse( |
|
62
|
|
|
[], |
|
63
|
|
|
$extras, |
|
64
|
|
|
false, |
|
65
|
|
|
$this->exception->getCode(), |
|
66
|
|
|
$this->exception->getMessage(), |
|
67
|
|
|
$this->statusCode |
|
68
|
|
|
); |
|
69
|
|
|
if ( |
|
70
|
|
|
!empty($this->contentType) |
|
71
|
|
|
&& array_key_exists($this->contentType, $this->renderers) |
|
72
|
|
|
) { |
|
73
|
|
|
$response = $response->withHeader('Content-Type', $this->contentType); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($this->exception instanceof HttpMethodNotAllowedException) { |
|
77
|
|
|
$allowMethods = implode(', ', $this->exception->getAllowedMethods()); |
|
|
|
|
|
|
78
|
|
|
$response = $response->withHeader('Allow', $allowMethods); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $response; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|