title: How to catch and log all exceptions in an Apigility ZF2 application?
body: <p>I want to build an error handling &amp; logging mecanism into an Apigility Zend Framework 2 aplication and catch &amp; log all exceptions.</p>
  
  <p>After some research I found a Stack Overflow <a href="https://stackoverflow.com/a/15272010/2019043">answer</a> with a solution, that seemed exactly to meet this requirements. Here is the code from the answer (with some minor naming and formatting modifications):</p>
  
  <p><strong><code>Module.php</code></strong></p>
  
  <pre><code>...
  
  use Zend\Mvc\ModuleRouteListener;
  use Zend\Log\Logger;
  use Zend\Log\Writer\Stream;
  
  ...
  
  class Module implements ApigilityProviderInterface
  {
  
      public function onBootstrap(MvcEvent $mvcEvent)
      {
          $eventManager = $mvcEvent-&gt;getApplication()-&gt;getEventManager();
          $moduleRouteListener = new ModuleRouteListener();
          $moduleRouteListener-&gt;attach($eventManager);
          /**
           * Log any Uncaught Exceptions, including all Exceptions in the stack
           */
          $sharedEventManager = $mvcEvent-&gt;getApplication()-&gt;getEventManager()-&gt;getSharedManager();
          $serviceManager = $mvcEvent-&gt;getApplication()-&gt;getServiceManager();
          $sharedEventManager-&gt;attach('Zend\Mvc\Application', MvcEvent::EVENT_DISPATCH_ERROR,
              function($mvcEvent) use ($serviceManager) {
                  if ($mvcEvent-&gt;getParam('exception')){
                      $exception = $mvcEvent-&gt;getParam('exception');
                      do {
                          $serviceManager-&gt;get('Logger')-&gt;crit(
                              sprintf(
                                 "%s:%d %s (%d) [%s]\n", 
                                  $exception-&gt;getFile(), 
                                  $exception-&gt;getLine(), 
                                  $exception-&gt;getMessage(), 
                                  $exception-&gt;getCode(), 
                                  get_class($exception)
                              )
                          );
                      }
                      while($exception = $exception-&gt;getPrevious());
                  }
              }
          );
      }
  
      ...
  
      public function getServiceConfig() {
          return array(
              'factories' =&gt; array(
                  // V1
                  ...
                  'Logger' =&gt; function($sm){
                      $logger = new Logger;
                      $writer = new Stream('/var/log/httpd/sandbox-log');
                      $logger-&gt;addWriter($writer);
                      return $logger;
                  },
              ),
              ...
          );
      }
  
  }
  </code></pre>
  
  <p>So now I've tried this out (with a simple <code>throw new \Exception('foo')</code>) at several places in the code (in a <code>Resource</code>, in a <code>Service</code>, and in a <code>Mapper</code> class) and expected to get the exceptions cached and logged into the file I defiden for. But it isn't working.</p>
  
  <p>Am I doing something wrong? What? How to get it working? <strong>How to catch and log all exceptions in an Apigility driven Zend Framework 2 application?</strong></p>
  
  <hr>
  
  <p>Additional info: An example of a place in the code, where an exception gets thrown:</p>
  
  <pre><code>class AddressResource extends AbstractResourceListener ...
  {
      public function fetch($id) {
          throw new \Exception('fetch_EXCEPTION');
          $service = $this-&gt;getAddressService();
          $entity = $service-&gt;getAddress($id);
          return $entity;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Additional info: The trace in the respose (when if set <code>throw new \Exception('fetch_EXCEPTION');</code> in the <code>BarResource#fetch(...)</code>):</p>
  
  <pre><code>{
      "trace": [
          {
              "file": "/var/www/my-project/vendor/zfcampus/zf-rest/src/AbstractResourceListener.php",
              "line": 166,
              "function": "fetch",
              "class": "FooAPI\\V1\\Rest\\Bar\\BarResource",
              "type": "-&gt;",
              "args": [
                  "1"
              ]
          },
          {
              "function": "dispatch",
              "class": "ZF\\Rest\\AbstractResourceListener",
              "type": "-&gt;",
              "args": [
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php",
              "line": 444,
              "function": "call_user_func",
              "args": [
                  [
                      {},
                      "dispatch"
                  ],
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php",
              "line": 205,
              "function": "triggerListeners",
              "class": "Zend\\EventManager\\EventManager",
              "type": "-&gt;",
              "args": [
                  "fetch",
                  {},
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zfcampus/zf-rest/src/Resource.php",
              "line": 541,
              "function": "trigger",
              "class": "Zend\\EventManager\\EventManager",
              "type": "-&gt;",
              "args": [
                  {},
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zfcampus/zf-rest/src/RestController.php",
              "line": 483,
              "function": "fetch",
              "class": "ZF\\Rest\\Resource",
              "type": "-&gt;",
              "args": [
                  "1"
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractRestfulController.php",
              "line": 366,
              "function": "get",
              "class": "ZF\\Rest\\RestController",
              "type": "-&gt;",
              "args": [
                  "1"
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zfcampus/zf-rest/src/RestController.php",
              "line": 332,
              "function": "onDispatch",
              "class": "Zend\\Mvc\\Controller\\AbstractRestfulController",
              "type": "-&gt;",
              "args": [
                  {}
              ]
          },
          {
              "function": "onDispatch",
              "class": "ZF\\Rest\\RestController",
              "type": "-&gt;",
              "args": [
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php",
              "line": 444,
              "function": "call_user_func",
              "args": [
                  [
                      {},
                      "onDispatch"
                  ],
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php",
              "line": 205,
              "function": "triggerListeners",
              "class": "Zend\\EventManager\\EventManager",
              "type": "-&gt;",
              "args": [
                  "dispatch",
                  {},
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php",
              "line": 118,
              "function": "trigger",
              "class": "Zend\\EventManager\\EventManager",
              "type": "-&gt;",
              "args": [
                  "dispatch",
                  {},
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractRestfulController.php",
              "line": 300,
              "function": "dispatch",
              "class": "Zend\\Mvc\\Controller\\AbstractController",
              "type": "-&gt;",
              "args": [
                  {},
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php",
              "line": 93,
              "function": "dispatch",
              "class": "Zend\\Mvc\\Controller\\AbstractRestfulController",
              "type": "-&gt;",
              "args": [
                  {},
                  {}
              ]
          },
          {
              "function": "onDispatch",
              "class": "Zend\\Mvc\\DispatchListener",
              "type": "-&gt;",
              "args": [
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php",
              "line": 444,
              "function": "call_user_func",
              "args": [
                  [
                      {},
                      "onDispatch"
                  ],
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php",
              "line": 205,
              "function": "triggerListeners",
              "class": "Zend\\EventManager\\EventManager",
              "type": "-&gt;",
              "args": [
                  "dispatch",
                  {},
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php",
              "line": 314,
              "function": "trigger",
              "class": "Zend\\EventManager\\EventManager",
              "type": "-&gt;",
              "args": [
                  "dispatch",
                  {},
                  {}
              ]
          },
          {
              "file": "/var/www/my-project/public/index.php",
              "line": 56,
              "function": "run",
              "class": "Zend\\Mvc\\Application",
              "type": "-&gt;",
              "args": []
          }
      ],
      "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
      "title": "Internal Server Error",
      "status": 500,
      "detail": "fetch_EXCEPTION"
  }
  </code></pre>