From 865ecc87963dc3b26e66296616eef2a1cc41ac3f Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 25 Oct 2023 12:55:09 +0300 Subject: move to psr-4 autoloader --- classes/Errors.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 classes/Errors.php (limited to 'classes/Errors.php') diff --git a/classes/Errors.php b/classes/Errors.php new file mode 100644 index 000000000..aa626d017 --- /dev/null +++ b/classes/Errors.php @@ -0,0 +1,40 @@ + $params + */ + static function to_json(string $code, array $params = []): string { + return json_encode(["error" => ["code" => $code, "params" => $params]]); + } + + static function libxml_last_error() : string { + $error = libxml_get_last_error(); + $error_formatted = ""; + + if ($error) { + foreach (libxml_get_errors() as $error) { + if ($error->level == LIBXML_ERR_FATAL) { + // currently only the first error is reported + $error_formatted = self::format_libxml_error($error); + break; + } + } + } + + return UConverter::transcode($error_formatted, 'UTF-8', 'UTF-8'); + } + + static function format_libxml_error(LibXMLError $error) : string { + return sprintf("LibXML error %s at line %d (column %d): %s", + $error->code, $error->line, $error->column, + $error->message); + } +} -- cgit v1.2.3-54-g00ecf