Skip to content

Exception Handling

MailBridge uses typed exceptions so application code can react differently to validation issues, provider/API failures, and retryable transient failures.

Exception Hierarchy

MailbridgeException is the base package exception.

MailbridgeValidationException extends MailbridgeException and represents validation/configuration issues.

Specialized validation exceptions include:

  • MissingTransactionalRecipientException
  • TemplatePayloadConflictException
  • MissingTransactionalContentException
  • MissingFromAddressException
  • MissingTemplateMappingException
  • UnknownProviderException
  • UnknownDriverException

ProviderTransientException extends MailbridgeException and signals retryable provider failures.

Common Triggers

ExceptionTrigger
MissingTransactionalRecipientExceptionTransactional send without any to() recipient.
TemplatePayloadConflictExceptionBoth template() and templateId() are set, or template send is combined with a Laravel Mailable.
MissingTransactionalContentExceptionNon-template send has no html(), no text(), and no mailable content.
MissingFromAddressExceptionNo sender resolved from explicit from(), mailbridge.providers.<provider>.from.address, and mailbridge.from.address.
MissingTemplateMappingExceptionMissing or empty mapping for mailbridge.templates.<alias>.<provider>.
UnknownProviderExceptionProvider key is not defined in mailbridge.providers.
UnknownDriverExceptionProvider config has an unsupported driver.
ProviderTransientExceptionRetryable network/rate-limit/provider errors while calling provider SDKs.

Catch specialized exceptions when you want targeted handling:

php
use Ashraful19\LaravelMailbridge\Exceptions\MissingTemplateMappingException;
use Ashraful19\LaravelMailbridge\Exceptions\ProviderTransientException;

try {
    MailBridge::transactional()
        ->template('welcome')
        ->to($user->email)
        ->data(['name' => $user->name])
        ->send();
} catch (MissingTemplateMappingException $e) {
    // Alert config issue or fallback to a raw template ID path.
} catch (ProviderTransientException $e) {
    // Queue retry, trigger fallback workflow, or return a temporary failure response.
}

Catch MailbridgeValidationException for broad validation handling without listing every subtype:

php
use Ashraful19\LaravelMailbridge\Exceptions\MailbridgeValidationException;

try {
    // MailBridge operation...
} catch (MailbridgeValidationException $e) {
    // Handle invalid input/config in one place.
}

Backward Compatibility

Existing code that catches MailbridgeValidationException does not need to change. Specialized exceptions are subclasses of MailbridgeValidationException.

Released under the MIT License.