Skip to content

Commit 89cd1a8

Browse files
Make error handling consistent with general Authsignal SDK conventions
1 parent cd998be commit 89cd1a8

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ Authsignal's server side signal API has five main calls `track`, `getAction`, `g
5353

5454
For more details on these api calls, refer to our [official PHP SDK docs](https://docs.authsignal.com/sdks/server/php#trackaction).
5555

56+
### Response & Error handling
57+
58+
Example:
59+
60+
```php
61+
$result = Authsignal::updateAction(
62+
userId: $userId,
63+
action: $action,
64+
idempotencyKey: "invalidKey",
65+
attributes: ['state' => 'CHALLENGE_FAILED']
66+
);
67+
68+
# PHP Fatal error: Uncaught AuthsignalNotFoundError: 404 - not_found
69+
```
70+
5671
## License
5772

5873
The library is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

lib/Authsignal/AuthsignalClient.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,39 @@ public static function apiUrl($path='')
1313
return $apiEndpoint.$path;
1414
}
1515

16-
public function handleApiError($response, $status)
16+
public function handleApiError($response, $statusCode)
1717
{
18-
$type = $response['error'] ?? null;
19-
$msg = $response['errorDescription'] ?? null;
20-
switch ($status) {
18+
$errorCode = $response['errorCode'] ?? null;
19+
$errorDescription = $response['errorDescription'] ?? null;
20+
switch ($statusCode) {
2121
case 400:
22-
throw new AuthsignalBadRequest($msg, $type, $status);
22+
throw new AuthsignalBadRequest($statusCode, $errorCode, $errorDescription);
2323
case 401:
24-
throw new AuthsignalUnauthorizedError($msg, $type, $status);
24+
throw new AuthsignalUnauthorizedError($statusCode, $errorCode, $errorDescription);
2525
case 403:
26-
throw new AuthsignalForbiddenError($msg, $type, $status);
26+
throw new AuthsignalForbiddenError($statusCode, $errorCode, $errorDescription);
2727
case 404:
28-
throw new AuthsignalNotFoundError($msg, $type, $status);
28+
throw new AuthsignalNotFoundError($statusCode, $errorCode, $errorDescription);
2929
case 422:
3030
// Handle subtype errors
31-
switch($type) {
31+
switch($errorCode) {
3232
case 'invalid_request_token':
33-
throw new AuthsignalInvalidRequestTokenError($msg, $type, $status);
33+
throw new AuthsignalInvalidRequestTokenError($statusCode, $errorCode, $errorDescription);
3434
default:
35-
throw new AuthsignalInvalidParametersError($msg, $type, $status);
35+
throw new AuthsignalInvalidParametersError($statusCode, $errorCode, $errorDescription);
3636
}
3737
default:
38-
throw new AuthsignalApiError($msg, $type, $status);
38+
throw new AuthsignalApiError($statusCode, $errorCode, $errorDescription);
3939
}
4040
}
4141

4242
public function handleRequestError($request)
4343
{
44-
throw new AuthsignalRequestError("$request->rError: $request->rMessage");
44+
$statusCode = $request->rStatus; // HTTP statusCode code
45+
$errorCode = $request->rError; // Error code
46+
$errorDescription = $request->rMessage; // Error message
47+
48+
throw new AuthsignalRequestError($statusCode, $errorCode, $errorDescription);
4549
}
4650

4751
public function handleResponse($request)

lib/Authsignal/Errors.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@
22

33
class AuthsignalError extends Exception
44
{
5-
5+
public function __construct($statusCode, $errorCode, $errorDescription = null, $previous = null)
6+
{
7+
$message = $this->formatMessage($statusCode, $errorCode, $errorDescription);
8+
parent::__construct($message, $statusCode, $previous);
9+
}
10+
11+
private function formatMessage($statusCode, $errorCode, $errorDescription = null)
12+
{
13+
return "$statusCode - " . $this->formatDescription($errorCode, $errorDescription);
14+
}
15+
16+
private function formatDescription($errorCode, $errorDescription = null)
17+
{
18+
return $errorDescription && strlen($errorDescription) > 0 ? $errorDescription : $errorCode;
19+
}
620
}
721

822
class AuthsignalRequestError extends AuthsignalError
@@ -22,12 +36,7 @@ class AuthsignalCurlOptionError extends AuthsignalError
2236

2337
class AuthsignalApiError extends AuthsignalError
2438
{
25-
public function __construct($msg, $type = null, $status = null)
26-
{
27-
parent::__construct($msg);
28-
$this->type = $type;
29-
$this->httpStatus = $status;
30-
}
39+
3140
}
3241

3342
class AuthsignalBadRequest extends AuthsignalApiError

0 commit comments

Comments
 (0)