mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2025-09-18 19:54:42 -04:00
Added tests for JSON errors, should help us figure out the cause of the problem in #11
This commit is contained in:
parent
6144e73405
commit
6cb7454d07
3 changed files with 84 additions and 3 deletions
48
lib/Json.php
Normal file
48
lib/Json.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
/**
|
||||
* 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
|
||||
* @version 0.22
|
||||
*/
|
||||
|
||||
namespace PrivateBin;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Json
|
||||
*
|
||||
* Provides JSON functions in an object oriented way.
|
||||
*/
|
||||
class Json
|
||||
{
|
||||
/**
|
||||
* Returns a string containing the JSON representation of the given input
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param mixed $input
|
||||
* @throws Exception
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($input)
|
||||
{
|
||||
$jsonString = json_encode($input);
|
||||
$errorCode = json_last_error();
|
||||
if ($errorCode === JSON_ERROR_NONE) {
|
||||
return $jsonString;
|
||||
}
|
||||
|
||||
$message = 'A JSON error occurred';
|
||||
if (function_exists('json_last_error_msg')) {
|
||||
$message .= ': ' . json_last_error_msg();
|
||||
}
|
||||
$message .= ' (' . $errorCode . ')';
|
||||
throw new Exception($message, 90);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue