Browse Source

rename DebugMiddleware to ProfilerMiddleware and improve error handling

michelphp 1 week ago
parent
commit
6b1fb00950

+ 7 - 5
src/BaseKernel.php

@@ -84,9 +84,11 @@ abstract class BaseKernel
 
             $requestHandler = new RequestHandler($this->container, $this->middlewareCollection);
             $response =  $requestHandler->handle($request);
-            if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
-                throw new HttpException($response->getStatusCode(), $response->getReasonPhrase());
-            }
+//            if (!$response->hasHeader('WWW-Authenticate') && $response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
+//                $exception = new HttpException($response->getStatusCode(), $response->getReasonPhrase());
+//                $exception->setContentType($response->getHeaderLine('Content-Type'));
+//                throw $exception;
+//            }
             return $response;
         } catch (Throwable $exception) {
             if (!$exception instanceof HttpExceptionInterface) {
@@ -152,7 +154,7 @@ abstract class BaseKernel
                 'file' => $exception->getFile(),
                 'line' => $exception->getLine(),
             ],
-        ], sprintf('%s_request_error.log', $this->getEnv()));
+        ], 'request_error.log');
     }
 
     final protected function log(array $data, string $logFile = null): void
@@ -215,7 +217,7 @@ abstract class BaseKernel
     private function configureErrorHandling(): void
     {
         ini_set("log_errors", '1');
-        ini_set("error_log", filepath_join($this->getLogDir(), sprintf('%s_error.log', $this->getEnv())));
+        ini_set("error_log", filepath_join($this->getLogDir(), 'error.log'));
 
         if ($this->getEnv() === 'dev') {
             ErrorHandler::register();

+ 2 - 1
src/ErrorHandler/ExceptionHandler.php

@@ -53,7 +53,8 @@ class ExceptionHandler
 
     public function render(ServerRequestInterface $request, Throwable $exception): ResponseInterface
     {
-        return $this->renderByMimetype($request->getHeaderLine('accept'), $exception);
+        $mimeType = $exception instanceof HttpExceptionInterface ? $exception->getContentType() : $request->getHeaderLine('accept');
+        return $this->renderByMimetype($mimeType, $exception);
     }
 
     protected function renderJsonResponse(HttpExceptionInterface $exception): ResponseInterface

+ 13 - 0
src/Http/Exception/HttpException.php

@@ -12,6 +12,8 @@ class HttpException extends \Exception implements HttpExceptionInterface
     protected static ?string $defaultMessage = 'An error occurred . Please try again later.';
     private int $statusCode;
 
+    private ?string $contentType = null;
+
     public function __construct(int $statusCode, ?string $message = null, int $code = 0, \Throwable $previous = null)
     {
         if ($message === null) {
@@ -34,4 +36,15 @@ class HttpException extends \Exception implements HttpExceptionInterface
     {
         return static::$defaultMessage;
     }
+
+    public function setContentType(string  $contentType): self
+    {
+        $this->contentType = $contentType;
+        return $this;
+    }
+
+    public function getContentType(): ?string
+    {
+        return $this->contentType;
+    }
 }

+ 3 - 0
src/Http/Exception/HttpExceptionInterface.php

@@ -22,4 +22,7 @@ interface HttpExceptionInterface extends \Throwable
      * @return string
      */
     public function getDefaultMessage(): string;
+
+
+    public function getContentType(): ?string;
 }

+ 3 - 7
src/Middlewares/DebugMiddleware.php → src/Middlewares/ProfilerMiddleware.php

@@ -14,7 +14,7 @@ use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Server\MiddlewareInterface;
 use Psr\Http\Server\RequestHandlerInterface;
 
-final class DebugMiddleware implements MiddlewareInterface
+final class ProfilerMiddleware implements MiddlewareInterface
 {
     protected ?RequestProfiler $requestProfiler = null;
     private bool $debug = false;
@@ -95,7 +95,7 @@ final class DebugMiddleware implements MiddlewareInterface
             }
         }
 
-        $this->log($requestProfilerData, 'debug.log');
+        $this->log($requestProfilerData, 'profiler.log');
 
         return $response;
     }
@@ -112,7 +112,7 @@ final class DebugMiddleware implements MiddlewareInterface
         ]);
     }
 
-    final protected function log(array $data, string $logFile = null): void
+    final protected function log(array $data, string $logFile): void
     {
         if ($this->logDir === null) {
             return;
@@ -122,10 +122,6 @@ final class DebugMiddleware implements MiddlewareInterface
             @mkdir($this->logDir, 0777, true);
         }
 
-        if ($logFile === null) {
-            $logFile = $this->env . '.log';
-        }
-
         file_put_contents(filepath_join($this->logDir, $logFile), json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND|LOCK_EX);
     }
 }

+ 3 - 3
src/Package/MichelCorePackage.php

@@ -18,7 +18,7 @@ use Michel\Framework\Core\Debug\DebugDataCollector;
 use Michel\Framework\Core\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
 use Michel\Framework\Core\ErrorHandler\ExceptionHandler;
 use Michel\Framework\Core\Http\RequestContext;
-use Michel\Framework\Core\Middlewares\DebugMiddleware;
+use Michel\Framework\Core\Middlewares\ProfilerMiddleware;
 use Michel\Framework\Core\Middlewares\ForceHttpsMiddleware;
 use Michel\Framework\Core\Middlewares\IpRestrictionMiddleware;
 use Michel\Framework\Core\Middlewares\MaintenanceMiddleware;
@@ -101,8 +101,8 @@ final class MichelCorePackage implements PackageInterface
             DebugDataCollector::class => static function (ContainerInterface $container): DebugDataCollector {
                 return new DebugDataCollector($container->get('michel.debug'));
             },
-            DebugMiddleware::class => static function (ContainerInterface $container) {
-                return new DebugMiddleware([
+            ProfilerMiddleware::class => static function (ContainerInterface $container) {
+                return new ProfilerMiddleware([
                     'debug' => $container->get('michel.debug'),
                     'profiler' => $container->get('app.profiler'),
                     'env' => $container->get('michel.environment'),