Skip to content

Commit b73d758

Browse files
committed
Add HttpTokenResponseException making error handling easy
Although http status code and http error message are helpful in error handling, TokenResponseException, throwed by oauth\HTTPClient if an error occured during the http request, does not have fields/methods which indicate them directly. Solve it by adding HttpTokenResponseException which has indicators of http status code and http error message, and throwing it instead of TokenResponseException. Signed-off-by: Naoto Kobayashi <naoto.kobayashi4c@gmail.com>
1 parent cace980 commit b73d758

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

HTTPClient.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use dokuwiki\HTTP\DokuHTTPClient;
66
use OAuth\Common\Http\Client\ClientInterface;
7-
use OAuth\Common\Http\Exception\TokenResponseException;
87
use OAuth\Common\Http\Uri\UriInterface;
98

109
/**
@@ -26,7 +25,11 @@ public function retrieveResponse(
2625
$ok = $http->sendRequest($endpoint->getAbsoluteUri(), $requestBody, $method);
2726
if (!$ok || $http->status < 200 || $http->status > 299) {
2827
$msg = "An error occured during the request to the oauth provider:\n";
29-
throw new TokenResponseException($msg . $http->error . ' [HTTP ' . $http->status . ']');
28+
throw new HttpTokenResponseException(
29+
message: $msg . $http->error . ' [HTTP ' . $http->status . ']',
30+
httpStatusCode: $http->status,
31+
httpErrorMessage: $http->error
32+
);
3033
}
3134

3235
return $http->resp_body;

HttpTokenResponseException.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\oauth;
4+
5+
use OAuth\Common\Http\Exception\TokenResponseException;
6+
7+
/**
8+
* Exception relating to http token response from service.
9+
*/
10+
class HttpTokenResponseException extends TokenResponseException
11+
{
12+
protected $httpStatusCode = 0;
13+
protected $httpErrorMessage = "";
14+
15+
/**
16+
* @param string $message
17+
* @param int $httpStatusCode
18+
* @param string httpErrorMessage
19+
* @param int $code
20+
* @param \Throwable|null $previous
21+
*/
22+
public function __construct(
23+
$message = "",
24+
$httpStatusCode = 0,
25+
$httpErrorMessage = "",
26+
$code = 0,
27+
\Throwable $previous = null
28+
) {
29+
parent::__construct($message, $code, $previous);
30+
$this->httpStatusCode = $httpStatusCode;
31+
$this->httpErrorMessage = $httpErrorMessage;
32+
}
33+
34+
/**
35+
* Get the HTTP status code
36+
*
37+
* @return int
38+
*/
39+
public function getHttpStatusCode()
40+
{
41+
return $this->httpStatusCode;
42+
}
43+
44+
/**
45+
* Get the HTTP error message
46+
*
47+
* @return string
48+
*/
49+
public function getHttpErrorMessage()
50+
{
51+
return $this->httpErrorMessage;
52+
}
53+
}

0 commit comments

Comments
 (0)