Refactored translation of exception messages

This commit is contained in:
El RIDO 2025-11-19 09:36:08 +01:00
parent 3e6f1733f9
commit 3a23117ebf
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
24 changed files with 186 additions and 110 deletions

View file

@ -15,6 +15,7 @@ use Exception;
use PDO;
use PDOException;
use PrivateBin\Controller;
use PrivateBin\Exception\JsonException;
use PrivateBin\Json;
/**
@ -179,18 +180,24 @@ class Database extends AbstractData
'SELECT * FROM "' . $this->_sanitizeIdentifier('paste') .
'" WHERE "dataid" = ?', array($pasteid), true
);
} catch (Exception $e) {
} catch (PDOException $e) {
$row = false;
}
if ($row === false) {
return false;
}
// create array
$paste = Json::decode($row['data']);
try {
$paste = Json::decode($row['data']);
} catch (JsonException $e) {
error_log('Error while reading a paste from the database: ' . $e->getMessage());
$paste = array();
}
try {
$paste['meta'] = Json::decode($row['meta']);
} catch (Exception $e) {
} catch (JsonException $e) {
error_log('Error while reading a paste from the database: ' . $e->getMessage());
$paste['meta'] = array();
}
$expire_date = (int) $row['expiredate'];
@ -233,7 +240,7 @@ class Database extends AbstractData
'SELECT "dataid" FROM "' . $this->_sanitizeIdentifier('paste') .
'" WHERE "dataid" = ?', array($pasteid), true
);
} catch (Exception $e) {
} catch (PDOException $e) {
return false;
}
return (bool) $row;
@ -253,7 +260,7 @@ class Database extends AbstractData
{
try {
$data = Json::encode($comment);
} catch (Exception $e) {
} catch (JsonException $e) {
error_log('Error while attempting to insert a comment into the database: ' . $e->getMessage());
return false;
}
@ -274,7 +281,7 @@ class Database extends AbstractData
$meta['created'],
)
);
} catch (Exception $e) {
} catch (PDOException $e) {
error_log('Error while attempting to insert a comment into the database: ' . $e->getMessage());
return false;
}
@ -298,8 +305,14 @@ class Database extends AbstractData
$comments = array();
if (count($rows)) {
foreach ($rows as $row) {
try {
$data = Json::decode($row['data']);
} catch (JsonException $e) {
error_log('Error while reading a comment from the database: ' . $e->getMessage());
$data = array();
}
$i = $this->getOpenSlot($comments, (int) $row['postdate']);
$comments[$i] = Json::decode($row['data']);
$comments[$i] = $data;
$comments[$i]['id'] = $row['dataid'];
$comments[$i]['parentid'] = $row['parentid'];
$comments[$i]['meta'] = array('created' => (int) $row['postdate']);
@ -329,7 +342,7 @@ class Database extends AbstractData
'" WHERE "pasteid" = ? AND "parentid" = ? AND "dataid" = ?',
array($pasteid, $parentid, $commentid), true
);
} catch (Exception $e) {
} catch (PDOException $e) {
return false;
}
}
@ -349,7 +362,8 @@ class Database extends AbstractData
$this->_last_cache[$key] = $value;
try {
$value = Json::encode($this->_last_cache);
} catch (Exception $e) {
} catch (JsonException $e) {
error_log('Error encoding JSON for table "config", row "traffic_limiter": ' . $e->getMessage());
return false;
}
}
@ -393,7 +407,8 @@ class Database extends AbstractData
if ($value && $namespace === 'traffic_limiter') {
try {
$this->_last_cache = Json::decode($value);
} catch (Exception $e) {
} catch (JsonException $e) {
error_log('Error decoding JSON from table "config", row "traffic_limiter": ' . $e->getMessage());
$this->_last_cache = array();
}
if (array_key_exists($key, $this->_last_cache)) {
@ -412,13 +427,18 @@ class Database extends AbstractData
*/
protected function _getExpiredPastes($batchsize)
{
$statement = $this->_db->prepare(
'SELECT "dataid" FROM "' . $this->_sanitizeIdentifier('paste') .
'" WHERE "expiredate" < ? AND "expiredate" != ? ' .
($this->_type === 'oci' ? 'FETCH NEXT ? ROWS ONLY' : 'LIMIT ?')
);
$statement->execute(array(time(), 0, $batchsize));
return $statement->fetchAll(PDO::FETCH_COLUMN, 0);
try {
$statement = $this->_db->prepare(
'SELECT "dataid" FROM "' . $this->_sanitizeIdentifier('paste') .
'" WHERE "expiredate" < ? AND "expiredate" != ? ' .
($this->_type === 'oci' ? 'FETCH NEXT ? ROWS ONLY' : 'LIMIT ?')
);
$statement->execute(array(time(), 0, $batchsize));
return $statement->fetchAll(PDO::FETCH_COLUMN, 0);
} catch (PDOException $e) {
error_log('Error while attempting to find expired pastes in the database: ' . $e->getMessage());
return array();
}
}
/**
@ -552,6 +572,7 @@ class Database extends AbstractData
'" WHERE "id" = ?', array($key), true
);
} catch (PDOException $e) {
error_log('Error while attempting to fetch configuration key "' . $key . '" in the database: ' . $e->getMessage());
return '';
}
return $row ? $row['value'] : '';

View file

@ -11,8 +11,8 @@
namespace PrivateBin\Data;
use Exception;
use GlobIterator;
use PrivateBin\Exception\JsonException;
use PrivateBin\Json;
/**
@ -104,13 +104,10 @@ class Filesystem extends AbstractData
*/
public function read($pasteid)
{
if (
!$this->exists($pasteid) ||
!$paste = $this->_get($this->_dataid2path($pasteid) . $pasteid . '.php')
) {
return false;
if ($this->exists($pasteid)) {
return $this->_get($this->_dataid2path($pasteid) . $pasteid . '.php');
}
return $paste;
return false;
}
/**
@ -346,7 +343,12 @@ class Filesystem extends AbstractData
file_get_contents($filename),
strlen(self::PROTECTION_LINE . PHP_EOL)
);
return Json::decode($data);
try {
return Json::decode($data);
} catch (JsonException $e) {
error_log('Error decoding JSON from "' . $filename . '": ' . $e->getMessage());
return false;
}
}
/**
@ -450,7 +452,7 @@ class Filesystem extends AbstractData
$filename,
self::PROTECTION_LINE . PHP_EOL . Json::encode($data)
);
} catch (Exception $e) {
} catch (JsonException $e) {
error_log('Error while trying to store data to the filesystem at path "' . $filename . '": ' . $e->getMessage());
return false;
}

View file

@ -15,6 +15,7 @@ use Exception;
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Storage\Bucket;
use Google\Cloud\Storage\StorageClient;
use PrivateBin\Exception\JsonException;
use PrivateBin\Json;
class GoogleCloudStorage extends AbstractData
@ -219,7 +220,12 @@ class GoogleCloudStorage extends AbstractData
try {
foreach ($this->_bucket->objects(array('prefix' => $prefix)) as $key) {
$data = $this->_bucket->object($key->name())->downloadAsString();
$comment = Json::decode($data);
try {
$comment = Json::decode($data);
} catch (JsonException $e) {
error_log('failed to read comment from ' . $key->name() . ', ' . $e->getMessage());
$comment = array();
}
$comment['id'] = basename($key->name());
$slot = $this->getOpenSlot($comments, (int) $comment['meta']['created']);
$comments[$slot] = $comment;

View file

@ -37,6 +37,7 @@ namespace PrivateBin\Data;
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use PrivateBin\Exception\JsonException;
use PrivateBin\Json;
class S3Storage extends AbstractData
@ -177,12 +178,14 @@ class S3Storage extends AbstractData
'ContentType' => 'application/json',
'Metadata' => $metadata,
));
return true;
} catch (S3Exception $e) {
error_log('failed to upload ' . $key . ' to ' . $this->_bucket . ', ' .
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
return false;
} catch (JsonException $e) {
error_log('failed to JSON encode ' . $key . ', ' . $e->getMessage());
}
return true;
return false;
}
/**
@ -212,8 +215,10 @@ class S3Storage extends AbstractData
} catch (S3Exception $e) {
error_log('failed to read ' . $pasteid . ' from ' . $this->_bucket . ', ' .
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
return false;
} catch (JsonException $e) {
error_log('failed to JSON decode ' . $key . ', ' . $e->getMessage());
}
return false;
}
/**