[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [donate/master] Error handling fixes



commit 76d4ac338d0fdf17033e789b5d8f0a389af9cd97
Author: Peter Haight <peterh@xxxxxxxxxxxxxxx>
Date:   Wed Sep 16 12:30:26 2020 -0700

    Error handling fixes
    
    With moving the form to a different website we need to have some
    Access-Control headers set or the browser will block fetch requests.
    Slim doesn't apply the middleware when it's handling the response
    object for errors, so the middleware wasn't adding these headers so the
    browser was blocking the error results.
    
    The PHP errors are handled with a separate error handler so I had to
    repeat everything for those as well so that PHP errors will also get
    handled correctly.
    
    Issue #48541
---
 src/ErrorHandler.php    | 25 +++++++++++++++++--------
 src/PhpErrorHandler.php | 41 +++++++++++++++++++++++++++++++++++++++++
 src/dependencies.php    |  5 +++++
 3 files changed, 63 insertions(+), 8 deletions(-)

diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php
index 81b0ccd6..82edfb01 100644
--- a/src/ErrorHandler.php
+++ b/src/ErrorHandler.php
@@ -13,6 +13,18 @@ class ErrorHandler extends \Slim\Handlers\Error {
     parent::__construct(FALSE);
   }
 
+  public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Exception $exception) {
+    $response = parent::__invoke($request, $response, $exception);
+    $torSiteBaseUrl = $this->container->get('settings')['torSiteBaseUrl'];
+    $response = $response->withHeader('Access-Control-Allow-Origin', $torSiteBaseUrl);
+    $response = $response->withHeader('Access-Control-Allow-Credentials', 'true');
+    $response = $response->withHeader('Access-Control-Allow-Headers', 'Content-Type');
+    return $response;
+  }
+
+  public function handlePhpError($request, $response, $error) {
+  }
+
   protected function writeToErrorLog($throwable) {
     $message = sprintf('Uncaught Exception %s: "%s" at %s line %s', get_class($throwable), $throwable->getMessage(), $throwable->getFile(), $throwable->getLine());
     $logger = $this->container->get('logger');
@@ -22,14 +34,11 @@ class ErrorHandler extends \Slim\Handlers\Error {
   protected function renderJsonErrorMessage(\Exception $exception)
   {
     $message = $exception->getMessage();
-    $response = array(
-      'errors' => array(
-        array(
-          'error_code' => $exception->getCode(),
-          'message' => $message
-        ),
-      ),
-    );
+    $response = [
+      'errors' => [
+        $message,
+      ]
+    ];
     return json_encode($response, JSON_PRETTY_PRINT);
   }
 }
diff --git a/src/PhpErrorHandler.php b/src/PhpErrorHandler.php
new file mode 100644
index 00000000..e573cde9
--- /dev/null
+++ b/src/PhpErrorHandler.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Tor;
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+
+class PhpErrorHandler extends \Slim\Handlers\PhpError {
+  public $container;
+
+  public function __construct($container) {
+    $this->container = $container;
+    parent::__construct(FALSE);
+  }
+
+  public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Throwable $error) {
+    $response = parent::__invoke($request, $response, $error);
+    $torSiteBaseUrl = $this->container->get('settings')['torSiteBaseUrl'];
+    $response = $response->withHeader('Access-Control-Allow-Origin', $torSiteBaseUrl);
+    $response = $response->withHeader('Access-Control-Allow-Credentials', 'true');
+    $response = $response->withHeader('Access-Control-Allow-Headers', 'Content-Type');
+    return $response;
+  }
+
+  protected function writeToErrorLog($throwable) {
+    $message = sprintf('Uncaught Exception %s: "%s" at %s line %s', get_class($throwable), $throwable->getMessage(), $throwable->getFile(), $throwable->getLine());
+    $logger = $this->container->get('logger');
+    $logger->error($message, array('exception' => $throwable, 'trace' => $throwable->getTrace()));
+  }
+
+  protected function renderJsonErrorMessage(\Throwable $error)
+  {
+    $message = $error->getMessage();
+    $response = [
+      'errors' => [
+        $message,
+      ]
+    ];
+    return json_encode($response, JSON_PRETTY_PRINT);
+  }
+}
diff --git a/src/dependencies.php b/src/dependencies.php
index 65058873..dce480db 100644
--- a/src/dependencies.php
+++ b/src/dependencies.php
@@ -67,6 +67,11 @@ $container['errorHandler'] = function($container) {
   return $error_handler;
 };
 
+$container['phpErrorHandler'] = function($container) {
+  $error_handler = new \Tor\PhpErrorHandler($container);
+  return $error_handler;
+};
+
 $container['ipRateLimiter'] = function($container) {
   return new \Tor\IpRateLimiter($container);
 };



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits