| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\router\response;
|
|
|
18 |
|
|
|
19 |
use core\param;
|
|
|
20 |
use core\router\schema\objects\scalar_type;
|
|
|
21 |
use core\router\schema\objects\schema_object;
|
|
|
22 |
use core\router\schema\objects\stacktrace;
|
|
|
23 |
use core\router\schema\referenced_object;
|
|
|
24 |
use core\router\schema\response\content\payload_response_type;
|
|
|
25 |
use core\router\schema\response\payload_response;
|
|
|
26 |
use GuzzleHttp\Psr7\Response;
|
|
|
27 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* A standard response for user preferences.
|
|
|
31 |
*
|
|
|
32 |
* @package core
|
|
|
33 |
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
abstract class exception_response extends \core\router\schema\response\response implements
|
|
|
37 |
referenced_object
|
|
|
38 |
{
|
|
|
39 |
/**
|
|
|
40 |
* Constructor for a new exception-related response.
|
|
|
41 |
*/
|
|
|
42 |
public function __construct() {
|
|
|
43 |
parent::__construct(
|
|
|
44 |
statuscode: static::get_exception_status_code(),
|
|
|
45 |
description: static::get_response_description(),
|
|
|
46 |
content: new payload_response_type(
|
|
|
47 |
schema: static::get_response_schema(),
|
|
|
48 |
),
|
|
|
49 |
);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Get the response for the exception.
|
|
|
54 |
*
|
|
|
55 |
* @param ServerRequestInterface $request
|
|
|
56 |
* @param \Exception $exception
|
|
|
57 |
* @param mixed[] ...$extra
|
|
|
58 |
* @return payload_response
|
|
|
59 |
*/
|
|
|
60 |
public static function get_response(
|
|
|
61 |
ServerRequestInterface $request,
|
|
|
62 |
\Exception $exception,
|
|
|
63 |
...$extra,
|
|
|
64 |
): payload_response {
|
|
|
65 |
return new payload_response(
|
|
|
66 |
payload: static::get_payload_data($exception, ...$extra),
|
|
|
67 |
request: $request,
|
|
|
68 |
response: new Response(
|
|
|
69 |
status: static::get_exception_status_code(),
|
|
|
70 |
body: $exception->getMessage(),
|
|
|
71 |
reason: explode("\n", $exception->getMessage())[0],
|
|
|
72 |
),
|
|
|
73 |
);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Get the schema for the response.
|
|
|
78 |
*
|
|
|
79 |
* @return schema_object
|
|
|
80 |
*/
|
|
|
81 |
protected static function get_response_schema(): schema_object {
|
|
|
82 |
return new schema_object(
|
|
|
83 |
content: [
|
|
|
84 |
'message' => new scalar_type(
|
|
|
85 |
type: param::ALPHANUMEXT,
|
|
|
86 |
description: 'The message of the exception.',
|
|
|
87 |
),
|
|
|
88 |
'errorcode' => new scalar_type(
|
|
|
89 |
type: param::ALPHANUMEXT,
|
|
|
90 |
description: 'The error code of the exception.',
|
|
|
91 |
),
|
|
|
92 |
'stacktrace' => new stacktrace(),
|
|
|
93 |
],
|
|
|
94 |
);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* The status code that this exception should return.
|
|
|
99 |
*
|
|
|
100 |
* @return int
|
|
|
101 |
*/
|
|
|
102 |
protected static function get_exception_status_code(): int {
|
|
|
103 |
return 500;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Get the description of this response.
|
|
|
108 |
*
|
|
|
109 |
* @return string
|
|
|
110 |
*/
|
|
|
111 |
abstract protected static function get_response_description(): string;
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Get the response payload data.
|
|
|
115 |
*
|
|
|
116 |
* @param \Exception $exception
|
|
|
117 |
* @param mixed ...$extra
|
|
|
118 |
* @return array
|
|
|
119 |
*/
|
|
|
120 |
protected static function get_payload_data(
|
|
|
121 |
\Exception $exception,
|
|
|
122 |
...$extra,
|
|
|
123 |
): array {
|
|
|
124 |
$data = [
|
|
|
125 |
'message' => $exception->getMessage(),
|
|
|
126 |
'stacktrace' => array_map(
|
|
|
127 |
fn ($frame): array => array_filter($frame, fn ($key) => $key !== 'args', ARRAY_FILTER_USE_KEY),
|
|
|
128 |
$exception->getTrace(),
|
|
|
129 |
),
|
|
|
130 |
];
|
|
|
131 |
|
|
|
132 |
if (is_a($exception, \moodle_exception::class)) {
|
|
|
133 |
$data['errorcode'] = $exception->errorcode;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
return $data;
|
|
|
137 |
}
|
|
|
138 |
}
|