fix error handling

This commit is contained in:
Andras Bacsai 2023-05-05 14:20:10 +02:00
parent 80e915c015
commit 631b61e11c
8 changed files with 84 additions and 53 deletions

View File

@ -15,6 +15,7 @@ class Add extends Component
public string $value; public string $value;
public bool $is_build_time = false; public bool $is_build_time = false;
protected $listeners = ['clearAddEnv' => 'clear'];
protected $rules = [ protected $rules = [
'key' => 'required|string', 'key' => 'required|string',
'value' => 'required|string', 'value' => 'required|string',
@ -27,25 +28,16 @@ class Add extends Component
public function submit() public function submit()
{ {
$this->validate(); $this->validate();
try { $this->emitUp('submit', [
$application_id = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail()->id; 'key' => $this->key,
EnvironmentVariable::create([ 'value' => $this->value,
'key' => $this->key, 'is_build_time' => $this->is_build_time,
'value' => $this->value, ]);
'is_build_time' => $this->is_build_time, }
'application_id' => $application_id, public function clear()
]); {
$this->emit('refreshEnvs'); $this->key = '';
$this->key = ''; $this->value = '';
$this->value = ''; $this->is_build_time = false;
} catch (mixed $e) {
dd('asdf');
if ($e instanceof QueryException) {
dd($e->errorInfo);
$this->emit('error', $e->errorInfo[2]);
} else {
$this->emit('error', $e);
}
}
} }
} }

View File

@ -3,14 +3,30 @@
namespace App\Http\Livewire\Project\Application\EnvironmentVariable; namespace App\Http\Livewire\Project\Application\EnvironmentVariable;
use App\Models\Application; use App\Models\Application;
use App\Models\EnvironmentVariable;
use Livewire\Component; use Livewire\Component;
class All extends Component class All extends Component
{ {
public Application $application; public Application $application;
protected $listeners = ['refreshEnvs']; protected $listeners = ['refreshEnvs', 'submit'];
public function refreshEnvs() public function refreshEnvs()
{ {
$this->application->refresh(); $this->application->refresh();
} }
public function submit($data)
{
try {
EnvironmentVariable::create([
'key' => $data['key'],
'value' => $data['value'],
'is_build_time' => $data['is_build_time'],
'application_id' => $this->application->id,
]);
$this->application->refresh();
$this->emit('clearAddEnv');
} catch (\Exception $e) {
return generalErrorHandlerLivewire($e, $this);
}
}
} }

View File

@ -14,6 +14,8 @@ class Add extends Component
public string $name; public string $name;
public string $mount_path; public string $mount_path;
public string|null $host_path = null; public string|null $host_path = null;
protected $listeners = ['clearAddStorage' => 'clear'];
protected $rules = [ protected $rules = [
'name' => 'required|string', 'name' => 'required|string',
'mount_path' => 'required|string', 'mount_path' => 'required|string',
@ -26,27 +28,16 @@ class Add extends Component
public function submit() public function submit()
{ {
$this->validate(); $this->validate();
try { $this->emitUp('submit', [
$application_id = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail()->id; 'name' => $this->name,
LocalPersistentVolume::create([ 'mount_path' => $this->mount_path,
'name' => $this->name, 'host_path' => $this->host_path,
'mount_path' => $this->mount_path, ]);
'host_path' => $this->host_path, }
'resource_id' => $application_id, public function clear()
'resource_type' => Application::class, {
]); $this->name = '';
$this->emit('refreshStorages'); $this->mount_path = '';
$this->name = ''; $this->host_path = null;
$this->mount_path = '';
$this->host_path = '';
} catch (mixed $e) {
dd('asdf');
if ($e instanceof QueryException) {
dd($e->errorInfo);
$this->emit('error', $e->errorInfo[2]);
} else {
$this->emit('error', $e);
}
}
} }
} }

View File

@ -3,14 +3,32 @@
namespace App\Http\Livewire\Project\Application\Storages; namespace App\Http\Livewire\Project\Application\Storages;
use App\Models\Application; use App\Models\Application;
use App\Models\LocalPersistentVolume;
use Illuminate\Database\QueryException;
use Livewire\Component; use Livewire\Component;
class All extends Component class All extends Component
{ {
public Application $application; public Application $application;
protected $listeners = ['refreshStorages']; protected $listeners = ['refreshStorages', 'submit'];
public function refreshStorages() public function refreshStorages()
{ {
$this->application->refresh(); $this->application->refresh();
} }
public function submit($data)
{
try {
LocalPersistentVolume::create([
'name' => $data['name'],
'mount_path' => $data['mount_path'],
'host_path' => $data['host_path'],
'resource_id' => $this->application->id,
'resource_type' => Application::class,
]);
$this->application->refresh();
$this->emit('clearAddStorage');
} catch (\Exception $e) {
return generalErrorHandlerLivewire($e, $this);
}
}
} }

View File

@ -4,6 +4,7 @@ use App\Actions\CoolifyTask\PrepareCoolifyTask;
use App\Data\CoolifyTaskArgs; use App\Data\CoolifyTaskArgs;
use App\Models\Server; use App\Models\Server;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -11,6 +12,20 @@ use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Spatie\Activitylog\Contracts\Activity; use Spatie\Activitylog\Contracts\Activity;
if (!function_exists('generalErrorHandlerLivewire')) {
function generalErrorHandlerLivewire(\Throwable $e, $that)
{
if ($e instanceof QueryException) {
if ($e->errorInfo[0] === '23505') {
$that->emit('error', 'Duplicate entry found.');
} else {
$that->emit('error', $e->errorInfo[3]);
}
} else {
$that->emit('error', $e);
}
}
}
if (!function_exists('remoteProcess')) { if (!function_exists('remoteProcess')) {
/** /**
* Run a Remote Process, which SSH's asynchronously into a machine to run the command(s). * Run a Remote Process, which SSH's asynchronously into a machine to run the command(s).

View File

@ -13,7 +13,8 @@
'flex flex-col' => $type !== 'checkbox', 'flex flex-col' => $type !== 'checkbox',
])> ])>
@if (!$noLabel) @if (!$noLabel)
<label for={{ $id }}> <label for={{ $id }} @if (!$noDirty) wire:dirty.class="text-amber-300" @endif
wire:target={{ $id }}>
@if ($label) @if ($label)
{{ $label }} {{ $label }}
@else @else
@ -29,8 +30,8 @@
required={{ $required }} type={{ $type }} id={{ $id }} wire:model.defer={{ $id }}></textarea> required={{ $required }} type={{ $type }} id={{ $id }} wire:model.defer={{ $id }}></textarea>
@else @else
<input {{ $attributes }} @if ($required) required @endif <input {{ $attributes }} @if ($required) required @endif
@if (!$noDirty) wire:dirty.class="text-black bg-amber-300" @endif type={{ $type }} @if (!$noDirty) wire:dirty.class="text-black bg-amber-300" @endif
id={{ $id }} type={{ $type }} id={{ $id }}
@if ($instantSave) wire:click='instantSave' wire:model.defer={{ $id }} @else wire:model.defer={{ $value ?? $id }} @endif /> @if ($instantSave) wire:click='instantSave' wire:model.defer={{ $id }} @else wire:model.defer={{ $value ?? $id }} @endif />
@endif @endif
@error($id) @error($id)

View File

@ -3,8 +3,7 @@
<x-inputs.input noDirty id="value" label="Value" required /> <x-inputs.input noDirty id="value" label="Value" required />
<div class="flex flex-col"> <div class="flex flex-col">
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<input type="checkbox" wire:model.defer="is_build_time" /> <x-inputs.input noDirty type="checkbox" id="is_build_time" label="Build Variable?" />
<label>Build Variable?</label>
</div> </div>
</div> </div>
<x-inputs.button type="submit"> <x-inputs.button type="submit">

View File

@ -1,11 +1,10 @@
<div x-data="{ deleteEnvironment: false }"> <div x-data="{ deleteEnvironment: false }">
<form wire:submit.prevent='submit' class="flex gap-2 px-2"> <form wire:submit.prevent='submit' class="flex gap-2 px-2">
<input type="text" wire:model.defer="env.key" wire:dirty.class="text-black bg-amber-300" /> <x-inputs.input id="env.key" noLabel />
<input type="text" wire:model.defer="env.value" wire:dirty.class="text-black bg-amber-300" /> <x-inputs.input id="env.value" noLabel />
<div class="flex flex-col"> <div class="flex flex-col">
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<input type="checkbox" wire:model.defer="env.is_build_time" /> <x-inputs.input type="checkbox" id="env.is_build_time" label="Build Variable?" />
<label wire:dirty.class="text-amber-300" wire:target="env.is_build_time">Build Variable?</label>
</div> </div>
</div> </div>
<x-inputs.button type="submit"> <x-inputs.button type="submit">