Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| RestExceptionHandler | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
462 | |
0.00% |
0 / 1 |
| sendHeader | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
462 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program 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 this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\helpers; |
| 23 | |
| 24 | use Exception; |
| 25 | |
| 26 | class RestExceptionHandler |
| 27 | { |
| 28 | /** |
| 29 | * Set response header according exception type |
| 30 | * @param Exception $exception |
| 31 | */ |
| 32 | public function sendHeader(Exception $exception) |
| 33 | { |
| 34 | switch (get_class($exception)) { |
| 35 | case \common_exception_BadRequest::class: |
| 36 | case \common_exception_MissingParameter::class: |
| 37 | case \common_exception_InvalidArgumentType::class: |
| 38 | case \common_exception_InconsistentData::class: |
| 39 | case \common_exception_ValidationFailed::class: |
| 40 | case \common_exception_RestApi::class: |
| 41 | header("HTTP/1.0 400 Bad Request"); |
| 42 | break; |
| 43 | |
| 44 | case \common_exception_Unauthorized::class: |
| 45 | header("HTTP/1.0 401 Unauthorized"); |
| 46 | break; |
| 47 | |
| 48 | case \common_exception_NotFound::class: |
| 49 | case \common_exception_ResourceNotFound::class: |
| 50 | header("HTTP/1.0 404 Not Found"); |
| 51 | break; |
| 52 | |
| 53 | case \common_exception_MethodNotAllowed::class: |
| 54 | header("HTTP/1.0 405 Method Not Allowed"); |
| 55 | if ($exception->getAllowedMethods()) { |
| 56 | header('Allow: ' . implode(', ', $exception->getAllowedMethods())); |
| 57 | } |
| 58 | break; |
| 59 | |
| 60 | case \common_exception_NotAcceptable::class: |
| 61 | header("HTTP/1.0 406 Not Acceptable"); |
| 62 | break; |
| 63 | |
| 64 | case "common_exception_TimeOut": |
| 65 | header("HTTP/1.0 408 Request Timeout"); |
| 66 | break; |
| 67 | |
| 68 | case "common_exception_Conflict": |
| 69 | header("HTTP/1.0 409 Conflict"); |
| 70 | break; |
| 71 | |
| 72 | case "common_exception_UnsupportedMediaType": |
| 73 | header("HTTP/1.0 415 Unsupported Media Type"); |
| 74 | break; |
| 75 | |
| 76 | case \common_exception_NotImplemented::class: |
| 77 | header("HTTP/1.0 501 Not Implemented"); |
| 78 | break; |
| 79 | |
| 80 | case \common_exception_PreConditionFailure::class: |
| 81 | header("HTTP/1.0 412 Precondition Failed"); |
| 82 | break; |
| 83 | |
| 84 | case \common_exception_NoContent::class: |
| 85 | header("HTTP/1.0 204 No Content"); |
| 86 | break; |
| 87 | |
| 88 | case "common_exception_teapotAprilFirst": |
| 89 | header("HTTP/1.0 418 I'm a teapot (RFC 2324)"); |
| 90 | break; |
| 91 | |
| 92 | default: |
| 93 | header("HTTP/1.0 500 Internal Server Error"); |
| 94 | } |
| 95 | } |
| 96 | } |