Skip to content

Commit

Permalink
improve error exceptiosn for missing credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Oct 10, 2023
1 parent f4abf6f commit dfa23f3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/Exceptions/MailcoachException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Spatie\MailcoachSdk\Exceptions;

class MailcoachException extends \RuntimeException
{
public static function missingApiToken(): self
{
return new static('No Mailcoach API token was provided. Please provide an API token in the `mailcoach-sdk.api_token` config key.');
}

public static function missingEndpoint(): self
{
return new static('No Mailcoach endpoint was provided. Please provide an endpoint in the `mailcoach-sdk.endpoint` config key.');
}
}
7 changes: 4 additions & 3 deletions src/MailcoachSdkServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\MailcoachSdk\Exceptions\MailcoachException;

class MailcoachSdkServiceProvider extends PackageServiceProvider
{
Expand All @@ -14,15 +15,15 @@ public function configurePackage(Package $package): void
->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(
Expand Down
22 changes: 22 additions & 0 deletions tests/MailcoachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');

0 comments on commit dfa23f3

Please sign in to comment.