1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
<?php
namespace SMW\SPARQLStore;
use Exception;
use Onoi\HttpRequest\HttpRequest;
use SMW\SPARQLStore\Exception\BadHttpEndpointResponseException;
use SMW\SPARQLStore\Exception\HttpEndpointConnectionException;
/**
* Post-processing for a bad inbound responses
*
* @ingroup Sparql
*
* @license GNU GPL v2+
* @since 2.0
*
* @author Markus Krötzsch
* @author mwjames
*/
class HttpResponseErrorMapper {
private $httpRequest = null;
/**
* @since 2.0
*
* @param HttpRequest $httpRequest
*/
public function __construct( HttpRequest $httpRequest ) {
$this->httpRequest = $httpRequest;
}
/**
* Either throw a suitable exception or fall through if the error should be
* handled gracefully. It is attempted to throw exceptions for all errors that
* can generally be prevented by proper coding or configuration (e.g. query
* syntax errors), and to be silent on all errors that might be caused by
* network issues or temporary overloading of the server. In this case, calling
* methods rather return something that helps to make the best out of the situation.
*
* @since 2.0
*
* @param $endpoint string URL of endpoint that was used
* @param $sparql string query that caused the problem
*
* @throws Exception
* @throws SparqlDatabaseException
*/
public function mapErrorResponse( $endpoint, $sparql ) {
$error = $this->httpRequest->getLastErrorCode();
switch ( $error ) {
case 22: // equals CURLE_HTTP_RETURNED_ERROR but this constant is not defined in PHP
$this->createResponseToHttpError( $this->httpRequest->getLastTransferInfo( CURLINFO_HTTP_CODE ), $endpoint, $sparql );
break;
case 52:
case CURLE_GOT_NOTHING:
break; // happens when 4Store crashes, do not bother the wiki
case CURLE_COULDNT_CONNECT:
break; // fail gracefully if backend is down
default:
throw new HttpEndpointConnectionException(
$endpoint,
$error,
$this->httpRequest->getLastError()
);
}
}
private function createResponseToHttpError( $httpCode, $endpoint, $sparql ) {
/// TODO We are guessing the meaning of HTTP codes here -- the SPARQL 1.1 spec does not yet provide this information for updates (April 15 2011)
if ( $httpCode == 400 ) { // malformed query
throw new BadHttpEndpointResponseException( BadHttpEndpointResponseException::ERROR_MALFORMED, $sparql, $endpoint, $httpCode );
} elseif ( $httpCode == 500 ) { // query refused; maybe fail gracefully here (depending on how stores use this)
throw new BadHttpEndpointResponseException( BadHttpEndpointResponseException::ERROR_REFUSED, $sparql, $endpoint, $httpCode );
} elseif ( $httpCode == 404 ) {
return; // endpoint not found, maybe down; fail gracefully
}
throw new BadHttpEndpointResponseException( BadHttpEndpointResponseException::ERROR_OTHER, $sparql, $endpoint, $httpCode );
}
}
|