URL Requests change error response

Instead of just throwing exception on 401 auth, throw exception for any
error code from 400 on
This can be turned off with the option "http_errors" set to false

Also updaed the exception content to match 400 or 500 error type with
more information attached

General Exception error codes:
Cnnn: Curl errors (FAILURE)
Rnnn: general class errors (ERROR)
Hnnn: http response errors (ERROR)
This commit is contained in:
Clemens Schwaighofer
2024-11-06 12:48:01 +09:00
parent 5c8a2ef8da
commit c8bc0062ad
5 changed files with 88 additions and 81 deletions

View File

@@ -12,7 +12,7 @@ declare(strict_types=1);
/**
* build return json
*
* @param array $http_headers
* @param array<string,mixed> $http_headers
* @param string $body
* @return string
*/
@@ -34,15 +34,18 @@ $http_headers = array_filter($_SERVER, function ($value, $key) {
header("Content-Type: application/json; charset=UTF-8");
// if the header has Authorization and RunAuthTest then exit with 401
if (!empty($http_headers['HTTP_AUTHORIZATION']) && !empty($http_headers['HTTP_RUNAUTHTEST'])) {
header("HTTP/1.1 401 Unauthorized");
print buildContent($http_headers, '["code": 401, "content": {"Error" => "Not Authorized"}]');
print buildContent($http_headers, '{"code": 401, "content": {"Error": "Not Authorized"}}');
exit;
}
print buildContent(
$http_headers,
file_get_contents('php://input') ?: '["code": 500, "content": {"Error" => "file_get_contents failed"}]'
);
if (($file_get = file_get_contents('php://input')) === false) {
header("HTTP/1.1 404 Not Found");
print buildContent($http_headers, '{"code": 404, "content": {"Error": "file_get_contents failed"}}');
exit;
}
print buildContent($http_headers, $file_get);
// __END__