refactored exceptions in controller

- added missing exception doc blocks
- introduced exception type that translates message during construction
- catch explicit exception types where possible
This commit is contained in:
El RIDO
2025-11-17 17:28:13 +01:00
parent fdd2c67b49
commit 3e6f1733f9
10 changed files with 105 additions and 53 deletions
+13 -15
View File
@@ -11,9 +11,9 @@
namespace PrivateBin\Model;
use Exception;
use PrivateBin\Controller;
use PrivateBin\Persistence\ServerSalt;
use PrivateBin\TranslatedException;
/**
* Paste
@@ -47,14 +47,14 @@ class Paste extends AbstractModel
* Get paste data.
*
* @access public
* @throws Exception
* @throws TranslatedException
* @return array
*/
public function get()
{
$data = $this->_store->read($this->getId());
if ($data === false) {
throw new Exception(Controller::GENERIC_ERROR, 64);
throw new TranslatedException(Controller::GENERIC_ERROR, 64);
}
// check if paste has expired and delete it if necessary.
@@ -62,7 +62,7 @@ class Paste extends AbstractModel
$now = time();
if ($data['meta']['expire_date'] < $now) {
$this->delete();
throw new Exception(Controller::GENERIC_ERROR, 63);
throw new TranslatedException(Controller::GENERIC_ERROR, 63);
}
// We kindly provide the remaining time before expiration (in seconds)
$data['meta']['time_to_live'] = $data['meta']['expire_date'] - $now;
@@ -93,13 +93,13 @@ class Paste extends AbstractModel
* Store the paste's data.
*
* @access public
* @throws Exception
* @throws TranslatedException
*/
public function store()
{
// Check for improbable collision.
if ($this->exists()) {
throw new Exception('You are unlucky. Try again.', 75);
throw new TranslatedException(self::COLLISION_ERROR, 75);
}
$this->_data['meta']['salt'] = ServerSalt::generate();
@@ -111,7 +111,7 @@ class Paste extends AbstractModel
$this->_data
) === false
) {
throw new Exception('Error saving document. Sorry.', 76);
throw new TranslatedException('Error saving document. Sorry.', 76);
}
}
@@ -119,7 +119,6 @@ class Paste extends AbstractModel
* Delete the paste.
*
* @access public
* @throws Exception
*/
public function delete()
{
@@ -143,13 +142,13 @@ class Paste extends AbstractModel
* @access public
* @param string $parentId
* @param string $commentId
* @throws Exception
* @throws TranslatedException
* @return Comment
*/
public function getComment($parentId, $commentId = '')
{
if (!$this->exists()) {
throw new Exception('Invalid data.', 62);
throw new TranslatedException(self::INVALID_DATA_ERROR, 62);
}
$comment = new Comment($this->_conf, $this->_store);
$comment->setPaste($this);
@@ -201,7 +200,6 @@ class Paste extends AbstractModel
* Check if paste has discussions enabled.
*
* @access public
* @throws Exception
* @return bool
*/
public function isOpendiscussion()
@@ -240,13 +238,13 @@ class Paste extends AbstractModel
*
* @access protected
* @param array $data
* @throws Exception
* @throws TranslatedException
*/
protected function _validate(array &$data)
{
// reject invalid or disabled formatters
if (!array_key_exists($data['adata'][self::ADATA_FORMATTER], $this->_conf->getSection('formatter_options'))) {
throw new Exception('Invalid data.', 75);
throw new TranslatedException(self::INVALID_DATA_ERROR, 75);
}
// discussion requested, but disabled in config or burn after reading requested as well, or invalid integer
@@ -257,7 +255,7 @@ class Paste extends AbstractModel
)) ||
($data['adata'][self::ADATA_OPEN_DISCUSSION] !== 0 && $data['adata'][self::ADATA_OPEN_DISCUSSION] !== 1)
) {
throw new Exception('Invalid data.', 74);
throw new TranslatedException(self::INVALID_DATA_ERROR, 74);
}
// reject invalid burn after reading
@@ -265,7 +263,7 @@ class Paste extends AbstractModel
$data['adata'][self::ADATA_BURN_AFTER_READING] !== 0 &&
$data['adata'][self::ADATA_BURN_AFTER_READING] !== 1
) {
throw new Exception('Invalid data.', 73);
throw new TranslatedException(self::INVALID_DATA_ERROR, 73);
}
}
}