1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace POData\Common; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use POData\Writers\Json\JsonODataV2Writer; |
7
|
|
|
use POData\Writers\Atom\AtomODataWriter; |
8
|
|
|
use POData\HttpProcessUtility; |
9
|
|
|
use POData\IService; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ErrorHandler |
13
|
|
|
* @package POData\Common |
14
|
|
|
*/ |
15
|
|
|
class ErrorHandler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Common function to handle exceptions in the data service. |
19
|
|
|
* |
20
|
|
|
* @param \Exception $exception exception |
21
|
|
|
* @param IService $service service |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public static function handleException($exception, IService $service) |
26
|
|
|
{ |
27
|
|
|
$acceptTypesText = $service->getHost()->getRequestAccept(); |
28
|
|
|
$responseContentType = null; |
|
|
|
|
29
|
|
|
try { |
30
|
|
|
$responseContentType = HttpProcessUtility::selectMimeType( |
31
|
|
|
$acceptTypesText, |
32
|
|
|
array( |
33
|
|
|
MimeTypes::MIME_APPLICATION_XML, |
34
|
|
|
MimeTypes::MIME_APPLICATION_JSON |
35
|
|
|
) |
36
|
|
|
); |
37
|
|
|
} catch (HttpHeaderFailure $exception) { |
38
|
|
|
$exception = new ODataException( |
39
|
|
|
$exception->getMessage(), |
40
|
|
|
$exception->getStatusCode() |
41
|
|
|
); |
42
|
|
|
} catch (\Exception $exception) { |
43
|
|
|
// Never come here |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (is_null($responseContentType)) { |
|
|
|
|
47
|
|
|
$responseContentType = MimeTypes::MIME_APPLICATION_XML; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!($exception instanceof ODataException)) { |
51
|
|
|
$exception = new ODataException($exception->getMessage(), HttpStatus::CODE_INTERNAL_SERVER_ERROR); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$service->getHost()->setResponseVersion(ODataConstants::DATASERVICEVERSION_1_DOT_0 . ';'); |
55
|
|
|
|
56
|
|
|
// At this point all kind of exceptions will be converted |
57
|
|
|
//to 'ODataException' |
58
|
|
|
if ($exception->getStatusCode() == HttpStatus::CODE_NOT_MODIFIED) { |
59
|
|
|
$service->getHost()->setResponseStatusCode(HttpStatus::CODE_NOT_MODIFIED); |
60
|
|
|
} else { |
61
|
|
|
$service->getHost()->setResponseStatusCode($exception->getStatusCode()); |
62
|
|
|
$service->getHost()->setResponseContentType($responseContentType); |
63
|
|
|
$responseBody = null; |
64
|
|
|
if (strcasecmp($responseContentType, MimeTypes::MIME_APPLICATION_XML) == 0) { |
65
|
|
|
$responseBody = AtomODataWriter::serializeException($exception, true); |
66
|
|
|
} else { |
67
|
|
|
$responseBody = JsonODataV2Writer::serializeException($exception, true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$service->getHost()->getOperationContext()->outgoingResponse()->setStream($responseBody); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |