summaryrefslogtreecommitdiff
path: root/classes/Errors.php
blob: aa626d017bfdda94a52093fa562752639e059967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
class Errors {
	const E_SUCCESS = "E_SUCCESS";
	const E_UNAUTHORIZED = "E_UNAUTHORIZED";
	const E_UNKNOWN_METHOD = "E_UNKNOWN_METHOD";
	const E_UNKNOWN_PLUGIN = "E_UNKNOWN_PLUGIN";
	const E_SCHEMA_MISMATCH = "E_SCHEMA_MISMATCH";
	const E_URL_SCHEME_MISMATCH = "E_URL_SCHEME_MISMATCH";

	/**
	 * @param Errors::E_* $code
	 * @param array<string, string> $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);
	}
}