Improve egg import error handling (#703)

* make sure read & write are successful

* show exception message in notification
This commit is contained in:
Boy132 2024-11-07 23:15:47 +01:00 committed by GitHub
parent 8eebb82eba
commit a9b76a0f51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -142,6 +142,7 @@ class ListEggs extends ListRecords
} catch (Exception $exception) {
Notification::make()
->title('Import Failed')
->body($exception->getMessage())
->danger()
->send();
@ -158,6 +159,7 @@ class ListEggs extends ListRecords
} catch (Exception $exception) {
Notification::make()
->title('Import Failed')
->body($exception->getMessage())
->danger()
->send();

View File

@ -86,7 +86,9 @@ class EggImporterService
$tmpDir = TemporaryDirectory::make()->deleteWhenDestroyed();
$tmpPath = $tmpDir->path($info['basename']);
file_put_contents($tmpPath, file_get_contents($url));
if (!file_put_contents($tmpPath, file_get_contents($url))) {
throw new InvalidFileUploadException('Could not write temporary file.');
}
return $this->fromFile(new UploadedFile($tmpPath, $info['basename'], 'application/json'), $egg);
}
@ -94,7 +96,6 @@ class EggImporterService
/**
* Takes an uploaded file and parses out the egg configuration from within.
*
* @throws \JsonException
* @throws \App\Exceptions\Service\InvalidFileUploadException
*/
protected function parseFile(UploadedFile $file): array
@ -103,7 +104,11 @@ class EggImporterService
throw new InvalidFileUploadException('The selected file was not uploaded successfully');
}
$parsed = json_decode($file->getContent(), true, 512, JSON_THROW_ON_ERROR);
try {
$parsed = json_decode($file->getContent(), true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new InvalidFileUploadException('Could not read JSON file: ' . $exception->getMessage());
}
$version = $parsed['meta']['version'] ?? '';