Skip to content

Error Handling

All Barta errors throw BartaException.

use Larament\Barta\Exceptions\BartaException;
try {
Barta::to('01712345678')
->message('Hello!')
->send();
} catch (BartaException $e) {
logger()->error('SMS failed: ' . $e->getMessage());
}
ExceptionCause
invalidNumber()Invalid phone format
missingRecipient()No ->to() called
missingMessage()No ->message() called
Config errorsMissing credentials
Gateway errorsAPI returned error
function sendWithFallback($phone, $msg): bool
{
foreach (['ssl', 'esms'] as $driver) {
try {
$r = Barta::driver($driver)->to($phone)->message($msg)->send();
if ($r->success) return true;
} catch (BartaException $e) {
continue;
}
}
return false;
}