Skip to content

Commit

Permalink
Log errors when saving/replacing assets
Browse files Browse the repository at this point in the history
resolves #2814
  • Loading branch information
brandonkelly committed Apr 26, 2018
1 parent 27eaf6e commit 68251ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed
- Fields’ translation icons now reveal the chosen Translation Method in their tooltip. ([#2808](https://github.com/craftcms/cms/issues/2808))
- Improved the error messages displayed when an Assets field has an invalid Upload Location setting. ([#2803](https://github.com/craftcms/cms/issues/2803))
- Craft now logs errors that occur when saving and replacing assets. ([#2814](https://github.com/craftcms/cms/issues/2814))

### Fixed
- Fixed an error that would occur on servers without the Phar PHP extension enabled.
Expand Down
14 changes: 9 additions & 5 deletions src/controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ public function actionSaveAsset(): Response
'filename' => $asset->filename,
'assetId' => $asset->id
]);
} catch (\Throwable $exception) {
return $this->asErrorJson($exception->getMessage());
} catch (\Throwable $e) {
Craft::error('An error occurred when saving an asset: '.$e->getMessage(), __METHOD__);
Craft::$app->getErrorHandler()->logException($e);
return $this->asErrorJson($e->getMessage());
}
}

Expand Down Expand Up @@ -229,8 +231,10 @@ public function actionReplaceFile(): Response
$assetId = $sourceAsset->id;
}
}
} catch (\Throwable $exception) {
return $this->asErrorJson($exception->getMessage());
} catch (\Throwable $e) {
Craft::error('An error occurred when replacing an asset: '.$e->getMessage(), __METHOD__);
Craft::$app->getErrorHandler()->logException($e);
return $this->asErrorJson($e->getMessage());
}

return $this->asJson(['success' => true, 'assetId' => $assetId]);
Expand Down Expand Up @@ -1006,7 +1010,7 @@ private function _getUploadedFileTempPath(UploadedFile $uploadedFile)
try {
$tempPath = $uploadedFile->saveAsTempFile();
} catch (ErrorException $e) {
throw new UploadFailedException(0);
throw new UploadFailedException(0, null, $e);
}

if ($tempPath === false) {
Expand Down
49 changes: 30 additions & 19 deletions src/errors/UploadFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,44 @@
*/
class UploadFailedException extends FileException
{
/**
* @var int Error code
*/
public $errorCode;

/**
* Constructor
*
* @param int $errorCode
* @param string|null $message
* @param \Throwable|null $previous
*/
public function __construct(int $errorCode)
public function __construct(int $errorCode = 0, string $message = null, \Throwable $previous = null)
{
switch ($errorCode) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$message = Craft::t('app', 'The uploaded file exceeds the maximum allowed size.');
break;
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
$message = Craft::t('app', 'The file failed to upload to the server properly.');
break;
case UPLOAD_ERR_NO_TMP_DIR:
$message = Craft::t('app', 'Could not write to the temporary upload folder.');
break;
case UPLOAD_ERR_CANT_WRITE:
$message = Craft::t('app', 'There was a problem with writing the file to the disk.');
break;
default:
$message = Craft::t('app', 'There was a problem with uploading the file.');
$this->errorCode = $errorCode;

if ($message === null) {
switch ($errorCode) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$message = Craft::t('app', 'The uploaded file exceeds the maximum allowed size.');
break;
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
$message = Craft::t('app', 'The file failed to upload to the server properly.');
break;
case UPLOAD_ERR_NO_TMP_DIR:
$message = Craft::t('app', 'Could not write to the temporary upload folder.');
break;
case UPLOAD_ERR_CANT_WRITE:
$message = Craft::t('app', 'There was a problem with writing the file to the disk.');
break;
default:
$message = Craft::t('app', 'There was a problem with uploading the file.');
}
}

parent::__construct($message);
parent::__construct($message, 0, $previous);
}

/**
Expand Down

0 comments on commit 68251ad

Please sign in to comment.