mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2026-01-23 02:35:23 +00:00
Refactored translation of exception messages
This commit is contained in:
parent
3e6f1733f9
commit
3a23117ebf
24 changed files with 186 additions and 110 deletions
37
lib/Exception/JsonException.php
Normal file
37
lib/Exception/JsonException.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* PrivateBin
|
||||
*
|
||||
* a zero-knowledge paste bin
|
||||
*
|
||||
* @link https://github.com/PrivateBin/PrivateBin
|
||||
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
||||
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
||||
*/
|
||||
|
||||
namespace PrivateBin\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* JsonException
|
||||
*
|
||||
* An Exception representing JSON en- or decoding errors.
|
||||
*/
|
||||
class JsonException extends Exception
|
||||
{
|
||||
/**
|
||||
* Exception constructor with mandatory JSON error code.
|
||||
*
|
||||
* @access public
|
||||
* @param int $code
|
||||
*/
|
||||
public function __construct(int $code) {
|
||||
$message = 'A JSON error occurred';
|
||||
if (function_exists('json_last_error_msg')) {
|
||||
$message .= ': ' . json_last_error_msg();
|
||||
}
|
||||
$message .= ' (' . $code . ')';
|
||||
parent::__construct($message, 90);
|
||||
}
|
||||
}
|
||||
36
lib/Exception/TranslatedException.php
Normal file
36
lib/Exception/TranslatedException.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* PrivateBin
|
||||
*
|
||||
* a zero-knowledge paste bin
|
||||
*
|
||||
* @link https://github.com/PrivateBin/PrivateBin
|
||||
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
||||
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
||||
*/
|
||||
|
||||
namespace PrivateBin\Exception;
|
||||
|
||||
use Exception;
|
||||
use PrivateBin\I18n;
|
||||
|
||||
/**
|
||||
* TranslatedException
|
||||
*
|
||||
* An Exception that translates it's message.
|
||||
*/
|
||||
class TranslatedException extends Exception
|
||||
{
|
||||
/**
|
||||
* Translating exception constructor with mandatory messageId.
|
||||
*
|
||||
* @access public
|
||||
* @param string|array $messageId message ID or array of message ID and parameters
|
||||
* @param int $code
|
||||
* @param ?Throwable $previous
|
||||
*/
|
||||
public function __construct(string|array $messageId, int $code = 0, ?Throwable $previous = null) {
|
||||
$message = is_string($messageId) ? I18n::translate($messageId) : forward_static_call_array('PrivateBin\I18n::translate', $messageId);
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue