Skip to content

Commit

Permalink
fix: Add more verbose error when uploading background image
Browse files Browse the repository at this point in the history
  • Loading branch information
keriati committed Dec 23, 2022
1 parent df70dcc commit dd4a32b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
49 changes: 30 additions & 19 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Setting;
use App\SettingGroup;
use Exception;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function edit(int $id)

return redirect($route)
->with([
'error' => __('app.alert.error.not_exist'),
'errors' => collect([__('app.alert.error.not_exist')]),
]);
}
}
Expand All @@ -69,36 +70,46 @@ public function update(Request $request, int $id): RedirectResponse
$setting = Setting::find($id);
$user = $this->user();

if (! is_null($setting)) {
$data = Setting::getInput($request);
try {
if (is_null($setting)) {
throw new Exception('not_exists');
}

if ($setting->type === 'image') {
if (!$request->hasFile('value')) {
throw new \Exception(
'file_too_big'
);
}

$setting_value = null;
$path = $request->file('value')->store('backgrounds');

if ($setting->type == 'image') {
if ($request->hasFile('value')) {
$path = $request->file('value')->store('backgrounds');
$setting_value = $path;
if ($path === null) {
throw new \Exception('file_not_stored');
}

$setting_value = $path;
} else {
$data = Setting::getInput($request);
$setting_value = $data->value;
}

$user->settings()->detach($setting->id);
$user->settings()->save($setting, ['uservalue' => $setting_value]);
$user->settings()->detach($setting->id);
$user->settings()->save($setting, ['uservalue' => $setting_value]);

$route = route('settings.index', []);
$route = route('settings.index', []);

return redirect($route)
->with([
'success' => __('app.alert.success.setting_updated'),
]);
} else {
return redirect($route)
->with([
'success' => __('app.alert.success.setting_updated'),
]);
} catch (Exception $e) {
$route = route('settings.index', []);

return redirect($route)
->with([
'error' => __('app.alert.error.not_exist'),
]);
->with([
'errors' => collect([__('app.alert.error.'.$e->getMessage())]),
]);
}
}

Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
'alert.success.tag_restored' => 'Tag restored successfully',
'alert.success.setting_updated' => 'You have successfully edited this setting',
'alert.error.not_exist' => 'This setting does not exist.',
'alert.error.file_too_big' => 'File is too big.',
'alert.error.file_not_stored' => 'File could not be stored.',
'alert.success.user_created' => 'User created successfully',
'alert.success.user_updated' => 'User updated successfully',
'alert.success.user_deleted' => 'User deleted successfully',
Expand Down

0 comments on commit dd4a32b

Please sign in to comment.