- Introduced `HandlesOperations` trait for reusable error handling with toasts and logging. - Added `StoreConnectionProviderForm` Livewire component with validation and slug uniqueness check. - Implemented `StoreConnectionProviderRequestData` DTO for structured data handling. - Updated `ConnectionProviderService` to include `create()` method using database transactions. - Registered new routes for creating connection providers. - Enhanced `ConnectionProvider` model with `Fillable` attribute for `name` and `slug`.
43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Concerns;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Mary\Traits\Toast;
|
|
use Throwable;
|
|
|
|
trait HandlesOperations
|
|
{
|
|
use Toast;
|
|
|
|
public function attempt(callable $action, ?callable $onError = null, string $successMessage = 'Success !', string $errorMessage = 'Something gone wrong!'): void
|
|
{
|
|
try {
|
|
// call the action callback
|
|
$action();
|
|
$this->success($successMessage);
|
|
} catch (ValidationException $exception) {
|
|
|
|
/**
|
|
* We are catching this exception intentionally so that, generic throwable catch block
|
|
* doesn't catch this, which will lead to close the modal.
|
|
*
|
|
* But, we are rethrowing this so that validation errors can be handled by others.
|
|
*/
|
|
|
|
throw $exception;
|
|
} catch (Throwable $exception) {
|
|
if (null !== $onError) {
|
|
// call the callback
|
|
$onError();
|
|
}
|
|
$this->error($errorMessage);
|
|
Log::error($exception->getMessage());
|
|
Log::error($exception->getTraceAsString());
|
|
}
|
|
}
|
|
}
|