Skip to content

Commit 711e60e

Browse files
authored
Merge pull request #154 from YoitoFes/add_exception
Add HttpTokenResponseException making error handling easy
2 parents cace980 + 096e753 commit 711e60e

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

HTTPClient.php

Lines changed: 5 additions & 2 deletions
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+
$msg . $http->error . ' [HTTP ' . $http->status . ']',
30+
$http->status,
31+
$http->error
32+
);
3033
}
3134

3235
return $http->resp_body;

HttpTokenResponseException.php

Lines changed: 53 additions & 0 deletions
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)