Skip to content

Commit a04721a

Browse files
authored
[6.x] New exception handling (foxbytehq#121)
1 parent a1e42ed commit a04721a

23 files changed

+234
-33
lines changed

src/Controllers/AuthorizationCallbackController.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Routing\Controller;
88
use Illuminate\Support\Facades\Event;
99
use Illuminate\Support\Facades\Redirect;
10-
use Illuminate\Support\Str;
1110
use Webfox\Xero\Events\XeroAuthorized;
1211
use Webfox\Xero\Exceptions\OAuthException;
1312
use Webfox\Xero\Oauth2Provider;
@@ -29,15 +28,7 @@ public function __invoke(Request $request, OauthCredentialManager $oauth, Identi
2928
]);
3029

3130
if ($request->has('error')) {
32-
throw new OAuthException(
33-
Str::headline(
34-
sprintf(
35-
'%s: %s',
36-
$request->get('error'),
37-
$request->get('error_description')
38-
)
39-
)
40-
);
31+
throw OAuthException::make($request->get('error'), $request->get('error_description'));
4132
}
4233

4334
$accessToken = $provider->getAccessToken('authorization_code', $request->only('code'));

src/Exceptions/OAuthException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
namespace Webfox\Xero\Exceptions;
44

5+
use Illuminate\Support\Str;
6+
57
class OAuthException extends XeroException
68
{
9+
public static function make(string $error, string $errorDescription): self
10+
{
11+
return new static(Str::headline(
12+
sprintf(
13+
'%s: %s',
14+
$error,
15+
$errorDescription
16+
)
17+
));
18+
}
719
}

src/Exceptions/XeroCredentialsNotFound.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44

55
class XeroCredentialsNotFound extends XeroException
66
{
7+
public static function make(): self
8+
{
9+
return new static('Xero oauth credentials are missing');
10+
}
711
}

src/Exceptions/XeroException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function __construct($message, $code = 0, Throwable $previous = null)
1212
parent::__construct($message, $code, $previous);
1313
}
1414

15-
public function __toString()
15+
public function __toString(): string
1616
{
1717
return __CLASS__.": [{$this->code}]: {$this->message}\n";
1818
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Webfox\Xero\Exceptions;
4+
5+
class XeroFailedToDecodeWebhookEvent extends XeroException
6+
{
7+
public static function make(): self
8+
{
9+
return new static('The webhook payload could not be decoded: '.json_last_error_msg());
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Webfox\Xero\Exceptions;
4+
5+
class XeroFailedToWriteFile extends XeroException
6+
{
7+
public static function make(string $filePath): self
8+
{
9+
return new static('Failed to write file: '.$filePath);
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Webfox\Xero\Exceptions;
4+
5+
class XeroMalformedWebhook extends XeroException
6+
{
7+
public static function make(): self
8+
{
9+
return new static('The webhook payload was malformed');
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Webfox\Xero\Exceptions;
4+
5+
class XeroMalformedWebhookEvent extends XeroException
6+
{
7+
public static function make(): self
8+
{
9+
return new static('The event payload was malformed; missing required field');
10+
}
11+
}

src/Exceptions/XeroTenantNotFound.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44

55
class XeroTenantNotFound extends XeroException
66
{
7+
public static function make(): self
8+
{
9+
return new static('No such tenant exists');
10+
}
711
}

src/Exceptions/XeroUserNotAuthenticated.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44

55
class XeroUserNotAuthenticated extends XeroException
66
{
7+
public static function make(): self
8+
{
9+
return new static('User is not authenticated');
10+
}
711
}

0 commit comments

Comments
 (0)