From 7dd773d2bca7d1efd36a81b4a9712bd246319170 Mon Sep 17 00:00:00 2001
From: Charles Sarrazin <charles@sarraz.in>
Date: Tue, 13 Jan 2015 11:32:03 +0100
Subject: [PATCH] Fixed issue with error, for stopwatch subscriber

---
 dist/GuzzleHttp/Subscriber/StopwatchSubscriber.php | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/dist/GuzzleHttp/Subscriber/StopwatchSubscriber.php b/dist/GuzzleHttp/Subscriber/StopwatchSubscriber.php
index c7d1f4a2..fdbddbe6 100644
--- a/dist/GuzzleHttp/Subscriber/StopwatchSubscriber.php
+++ b/dist/GuzzleHttp/Subscriber/StopwatchSubscriber.php
@@ -13,6 +13,7 @@
 
 use GuzzleHttp\Event\CompleteEvent;
 use GuzzleHttp\Event\BeforeEvent;
+use GuzzleHttp\Event\ErrorEvent;
 use GuzzleHttp\Event\RequestEvents;
 use GuzzleHttp\Event\SubscriberInterface;
 use Symfony\Component\Stopwatch\Stopwatch;
@@ -36,7 +37,7 @@ public function getEvents()
         return [
             'before'   => ['onBefore', RequestEvents::EARLY],
             'complete' => ['onFinish', RequestEvents::LATE],
-            'error'    => ['onFinish', RequestEvents::EARLY],
+            'error'    => ['onError', RequestEvents::EARLY],
         ];
     }
 
@@ -55,4 +56,15 @@ public function onFinish(CompleteEvent $event)
 
         $this->stopwatch->stop($url);
     }
+
+    public function onError(ErrorEvent $event)
+    {
+        $url = $event->getRequest()->getUrl();
+
+        if (!$this->stopwatch->isStarted($url)) {
+            return;
+        }
+
+        $this->stopwatch->stop($url);
+    }
 }