From dfa23f37fed1a4e9246e51019049a9bdeef643bd Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:56:44 +0200 Subject: [PATCH] improve error exceptiosn for missing credentials --- src/Exceptions/MailcoachException.php | 16 ++++++++++++++++ src/MailcoachSdkServiceProvider.php | 7 ++++--- tests/MailcoachTest.php | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/Exceptions/MailcoachException.php diff --git a/src/Exceptions/MailcoachException.php b/src/Exceptions/MailcoachException.php new file mode 100644 index 0000000..6c20db8 --- /dev/null +++ b/src/Exceptions/MailcoachException.php @@ -0,0 +1,16 @@ +hasConfigFile(); } - public function registeringPackage() + public function registeringPackage(): void { $this->app->bind(Mailcoach::class, function () { if (config('mailcoach-sdk.api_token') === null) { - return null; + throw MailcoachException::missingApiToken(); } if (config('mailcoach-sdk.endpoint') === null) { - return null; + throw MailcoachException::missingEndpoint(); } return new Mailcoach( diff --git a/tests/MailcoachTest.php b/tests/MailcoachTest.php index 206d5f3..1d961b7 100644 --- a/tests/MailcoachTest.php +++ b/tests/MailcoachTest.php @@ -15,3 +15,25 @@ expect($token)->toBe('fake-token'); }); + +it('throws an exception when no api token is set', function () { + (new MailcoachSdkServiceProvider($this->app))->bootingPackage(); + + config()->set('mailcoach-sdk', [ + 'api_token' => null, + 'endpoint' => 'fake-endpoint', + ]); + + Mailcoach::apiToken(); +})->throws('No Mailcoach API token was provided. Please provide an API token in the `mailcoach-sdk.api_token` config key.'); + +it('throws an exception when no endpoint is set', function () { + (new MailcoachSdkServiceProvider($this->app))->bootingPackage(); + + config()->set('mailcoach-sdk', [ + 'api_token' => 'fake-token', + 'endpoint' => null, + ]); + + Mailcoach::apiToken(); +})->throws('No Mailcoach endpoint was provided. Please provide an endpoint in the `mailcoach-sdk.endpoint` config key.');