refactor: enhance handleAction method to include error route parameter for improved error handling
This commit is contained in:
parent
5b553b2c26
commit
e92a4cfbcf
@ -16,20 +16,20 @@ public function __construct(
|
|||||||
public function pay(Payroll $payroll): RedirectResponse
|
public function pay(Payroll $payroll): RedirectResponse
|
||||||
{
|
{
|
||||||
return $this->handleAction(
|
return $this->handleAction(
|
||||||
fn () => $this->service->pay($payroll),
|
fn() => $this->service->pay($payroll),
|
||||||
'Gaji berhasil dibayar.',
|
'Gaji berhasil dibayar.',
|
||||||
'admin.finance.payroll-periods.show',
|
'admin.finance.payroll-periods.show',
|
||||||
['payroll_period' => $payroll->payroll_period_id]
|
parameters: ['payroll_period' => $payroll->payroll_period_id]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function cancel(Payroll $payroll): RedirectResponse
|
public function cancel(Payroll $payroll): RedirectResponse
|
||||||
{
|
{
|
||||||
return $this->handleAction(
|
return $this->handleAction(
|
||||||
fn () => $this->service->cancel($payroll),
|
fn() => $this->service->cancel($payroll),
|
||||||
'Gaji berhasil dibatalkan.',
|
'Gaji berhasil dibatalkan.',
|
||||||
'admin.finance.payroll-periods.show',
|
'admin.finance.payroll-periods.show',
|
||||||
['payroll_period' => $payroll->payroll_period_id]
|
parameters: ['payroll_period' => $payroll->payroll_period_id]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,18 +8,19 @@
|
|||||||
|
|
||||||
abstract class Controller
|
abstract class Controller
|
||||||
{
|
{
|
||||||
protected function handleAction(callable $action, string $successMessage, string $redirectRoute, array $parameters = []): RedirectResponse
|
protected function handleAction(callable $action, string $successMessage, string $redirectRoute, ?string $errorRoute = null, array $parameters = []): RedirectResponse
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$action();
|
$action();
|
||||||
Inertia::flash('toast', ['type' => 'success', 'message' => $successMessage]);
|
Inertia::flash('toast', ['type' => 'success', 'message' => $successMessage]);
|
||||||
|
return to_route($redirectRoute, $parameters);
|
||||||
} catch (ValidationException $e) {
|
} catch (ValidationException $e) {
|
||||||
$firstError = collect($e->errors())->flatten()->first();
|
$firstError = collect($e->errors())->flatten()->first();
|
||||||
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
||||||
|
return to_route($errorRoute ?? $redirectRoute, $parameters);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
||||||
|
return to_route($errorRoute ?? $redirectRoute, $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
return to_route($redirectRoute, $parameters);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user