This commit is contained in:
2021-02-08 16:52:27 +08:00
parent da35f038c5
commit 0e31371651
3 changed files with 64 additions and 31 deletions
+22 -2
View File
@@ -10,7 +10,9 @@ declare(strict_types=1);
namespace Snowflake\Core;
use Error;
use Exception;
use Throwable;
/**
* Class JSON
@@ -19,10 +21,10 @@ use Exception;
class Json
{
/**
* @param $data
* @return false|string
* @throws Exception
*/
public static function encode($data): bool|string
{
@@ -49,6 +51,7 @@ class Json
return json_decode($data, $asArray);
}
/**
* @param $code
* @param string $message
@@ -85,6 +88,24 @@ class Json
return static::encode($params);
}
/**
* @param Throwable|Error $throwable
* @return bool|string
* @throws Exception
*/
public static function error(Throwable|Error $throwable): bool|string
{
$array['code'] = $throwable->getCode() == 0 ? 500 : $throwable->getCode();
$array['message'] = $throwable->getMessage();
$array['param'] = [
'file' => $throwable->getFile(),
'line' => $throwable->getLine()
];
return Json::encode($array);
}
/**
* @param $state
* @param $body
@@ -98,5 +119,4 @@ class Json
return static::encode($params);
}
}