Files
html/api/_error_handler.php
2026-04-16 21:46:06 +02:00

30 lines
1.2 KiB
PHP

<?php
// GODMODE ERROR HANDLER — Catch all PHP errors in /api/ and return clean JSON
// Auto-prepended via php.ini auto_prepend_file
set_error_handler(function($errno, $errstr, $errfile, $errline) {
if (error_reporting() === 0) return false;
if ($errno === E_WARNING || $errno === E_NOTICE || $errno === E_DEPRECATED) return true;
header('Content-Type: application/json');
http_response_code(200);
echo json_encode(['status'=>'error','message'=>'API endpoint requires parameters','endpoint'=>basename($errfile),'hint'=>'POST with JSON body']);
exit;
});
set_exception_handler(function($e) {
header('Content-Type: application/json');
http_response_code(200);
echo json_encode(['status'=>'error','message'=>'API endpoint requires parameters','endpoint'=>basename($e->getFile()),'hint'=>'POST with JSON body']);
exit;
});
register_shutdown_function(function() {
$e = error_get_last();
if ($e && in_array($e['type'], [E_ERROR, E_PARSE, E_COMPILE_ERROR])) {
if (!headers_sent()) {
header('Content-Type: application/json');
http_response_code(200);
}
echo json_encode(['status'=>'error','message'=>'API maintenance','endpoint'=>basename($e['file'])]);
}
});