<?php

if (!function_exists('ec_register_error_handlers')) {

    function ec_register_error_handlers(): void
    {
        set_exception_handler(function (Throwable $e) {

            http_response_code(500);

            header('Content-Type: text/plain; charset=UTF-8');

            echo "==============================\n";
            echo " EASYCONF DEBUG EXCEPTION\n";
            echo "==============================\n\n";

            echo "MESSAGE:\n";
            echo $e->getMessage() . "\n\n";

            echo "FILE:\n";
            echo $e->getFile() . "\n\n";

            echo "LINE:\n";
            echo $e->getLine() . "\n\n";

            echo "TRACE:\n";
            echo $e->getTraceAsString() . "\n";

            exit;
        });

        set_error_handler(function (
            $severity,
            $message,
            $file,
            $line
        ) {
            throw new ErrorException(
                $message,
                0,
                $severity,
                $file,
                $line
            );
        });
    }

}
