Skip to content
Snippets Groups Projects

Issue #3293269: Improve error logging

Merged Gabor Szanto requested to merge issue/s3fs-3293269:3293269-improve-error-logging into 8.x-3.x
All threads resolved!
Files
2
+ 62
4
@@ -2,6 +2,7 @@
namespace Drupal\s3fs;
use Aws\Exception\AwsException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\Exception\DirectoryNotReadyException;
@@ -12,6 +13,7 @@ use Drupal\Core\File\Exception\FileWriteException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\Utility\Error;
use Drupal\s3fs\Traits\S3fsPathsTrait;
use Psr\Log\LoggerInterface;
@@ -46,6 +48,13 @@ class S3fsFileService implements FileSystemInterface {
*/
protected $logger;
/**
* The s3fs logger channel.
*
* @var \Psr\Log\LoggerInterface
*/
protected $s3fsLogger;
/**
* The stream wrapper manager.
*
@@ -91,7 +100,7 @@ class S3fsFileService implements FileSystemInterface {
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
* StreamWrapper manager service.
* @param \Psr\Log\LoggerInterface $logger
* Logging service.
* File logging service.
* @param \Drupal\s3fs\S3fsServiceInterface $s3fs
* S3fs Service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
@@ -103,11 +112,19 @@ class S3fsFileService implements FileSystemInterface {
* Expected to implement
* \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface
* or \Symfony\Component\Mime\MimeTypeGuesserInterface.
* @param \Psr\Log\LoggerInterface|null $s3fsLogger
* S3fs logging channel.
*/
public function __construct(FileSystemInterface $decorated, StreamWrapperManagerInterface $stream_wrapper_manager, LoggerInterface $logger, S3fsServiceInterface $s3fs, ConfigFactoryInterface $configFactory, ModuleHandlerInterface $moduleHandler, $mimeGuesser) {
public function __construct(FileSystemInterface $decorated, StreamWrapperManagerInterface $stream_wrapper_manager, LoggerInterface $logger, S3fsServiceInterface $s3fs, ConfigFactoryInterface $configFactory, ModuleHandlerInterface $moduleHandler, $mimeGuesser, ?LoggerInterface $s3fsLogger = NULL) {
$this->decorated = $decorated;
$this->streamWrapperManager = $stream_wrapper_manager;
$this->logger = $logger;
if ($s3fsLogger == NULL) {
@trigger_error('Calling ' . __METHOD__ . ' without the $s3fsLogger argument is deprecated in s3fs:8.x-3.4 and will required before s3fs:2.0.0. See https://www.drupal.org/node/3406097', E_USER_DEPRECATED);
// @phpstan-ignore-next-line
$s3fsLogger = \Drupal::service('logger.channel.s3fs');
}
$this->s3fsLogger = $s3fsLogger;
$this->s3fs = $s3fs;
$this->moduleHandler = $moduleHandler;
$this->mimeGuesser = $mimeGuesser;
@@ -535,11 +552,32 @@ class S3fsFileService implements FileSystemInterface {
$this->moduleHandler->alter('s3fs_upload_params', $uploadParams);
$s3 = $this->s3fs->getAmazonS3Client($config);
try {
$s3 = $this->s3fs->getAmazonS3Client($config);
}
catch (S3fsException $e) {
$exception_variables = Error::decodeException($e);
$this->s3fsLogger->error('AmazonS3Client error: @message', $exception_variables);
return FALSE;
}
try {
$s3->putObject($uploadParams);
}
catch (AwsException $e) {
$exception_variables = Error::decodeException($e);
// In some cases the getAwsErrorMessage() method is returns an empty
// string. Like when we try to use a nonexistent bucket.
if (!empty($e->getAwsErrorMessage())) {
$exception_variables['message'] = $e->getAwsErrorMessage();
}
$exception_variables['@request_id'] = $e->getAwsRequestId();
$this->s3fsLogger->error('An error occurred when uploading a file: @message. Request ID: @request_id', $exception_variables);
return FALSE;
}
catch (\Exception $e) {
$exception_variables = Error::decodeException($e);
$this->s3fsLogger->error('An error occurred when uploading a file: @message.', $exception_variables);
return FALSE;
}
@@ -614,7 +652,14 @@ class S3fsFileService implements FileSystemInterface {
$contentType = $this->mimeGuesser->guess($key_path);
}
$s3 = $this->s3fs->getAmazonS3Client($config);
try {
$s3 = $this->s3fs->getAmazonS3Client($config);
}
catch (S3fsException $e) {
$exception_variables = Error::decodeException($e);
$this->s3fsLogger->error('AmazonS3Client error: @message', $exception_variables);
return FALSE;
}
$copyParams = [
'Bucket' => $config['bucket'],
@@ -649,7 +694,20 @@ class S3fsFileService implements FileSystemInterface {
try {
$s3->copyObject($copyParams);
}
catch (AwsException $e) {
$exception_variables = Error::decodeException($e);
// In some cases the getAwsErrorMessage() method is returns an empty
// string. Like when we try to use a nonexistent bucket.
if (!empty($e->getAwsErrorMessage())) {
$exception_variables['message'] = $e->getAwsErrorMessage();
}
$exception_variables['@request_id'] = $e->getAwsRequestId();
$this->s3fsLogger->error('An error occurred when uploading a file: @message. Request ID: @request_id', $exception_variables);
return FALSE;
}
catch (\Exception $e) {
$exception_variables = Error::decodeException($e);
$this->s3fsLogger->error('An error occurred when uploading a file: @message.', $exception_variables);
return FALSE;
}
Loading