Skip to content

Commit 8e22d09

Browse files
committed
Display errors better
1 parent fb08dab commit 8e22d09

File tree

6 files changed

+80
-25
lines changed

6 files changed

+80
-25
lines changed

Collector/ClientDataCollector.php renamed to Collector/ClientDataProvider.php

+34-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
namespace Http\HttplugBundle\Collector;
44

55
/**
6-
* An object to handle the collected data for a client.
6+
* An object to handle the collected data for a client. This is used to display data.
77
*
88
* The Request object at $requests[0][2] is the state of the object between the third
99
* and the fourth plugin. The response after that plugin is found in $responses[0][2].
1010
*
1111
* @author Tobias Nyholm <[email protected]>
1212
*/
13-
class ClientDataCollector
13+
class ClientDataProvider
1414
{
15+
/**
16+
* Array that tell if a request errored or not. true = success, false = failure.
17+
* @var array
18+
*/
19+
private $failure;
20+
1521
/**
1622
* A multidimensional array with requests.
1723
* $requests[0][0] is the first request before all plugins.
@@ -31,11 +37,13 @@ class ClientDataCollector
3137
private $responses;
3238

3339
/**
40+
* @param array $failure if the response was successful or not
3441
* @param array $requests
3542
* @param array $responses
3643
*/
37-
public function __construct(array $requests, array $responses)
44+
public function __construct(array $failure, array $requests, array $responses)
3845
{
46+
$this->failure = $failure;
3947
$this->requests = $requests;
4048
$this->responses = $responses;
4149
}
@@ -45,7 +53,7 @@ public function __construct(array $requests, array $responses)
4553
*
4654
* @param array $data
4755
*
48-
* @return ClientDataCollector[]
56+
* @return ClientDataProvider[]
4957
*/
5058
public static function createFromCollectedData(array $data)
5159
{
@@ -58,16 +66,23 @@ public static function createFromCollectedData(array $data)
5866
}
5967

6068
/**
61-
* @param array $messages is an array with keys 'request' and 'response' which hold requests for each call to
69+
* @param array $messages is an array with keys 'failure', 'request' and 'response' which hold requests for each call to
6270
* sendRequest and for each depth.
6371
*
64-
* @return ClientDataCollector
72+
* @return ClientDataProvider
6573
*/
6674
private static function createOne($messages)
6775
{
76+
$orderedFaulure = [];
6877
$orderedRequests = [];
6978
$orderedResponses = [];
7079

80+
foreach ($messages['failure'] as $depth => $failures) {
81+
foreach ($failures as $idx => $failure) {
82+
$orderedFaulure[$idx][$depth] = $failure;
83+
}
84+
}
85+
7186
foreach ($messages['request'] as $depth => $requests) {
7287
foreach ($requests as $idx => $request) {
7388
$orderedRequests[$idx][$depth] = $request;
@@ -80,7 +95,7 @@ private static function createOne($messages)
8095
}
8196
}
8297

83-
return new self($orderedRequests, $orderedResponses);
98+
return new self($orderedFaulure, $orderedRequests, $orderedResponses);
8499
}
85100

86101
/**
@@ -94,7 +109,7 @@ public function getRequests()
94109
/**
95110
* @param array $requests
96111
*
97-
* @return ClientDataCollector
112+
* @return ClientDataProvider
98113
*/
99114
public function setRequests($requests)
100115
{
@@ -114,7 +129,7 @@ public function getResponses()
114129
/**
115130
* @param array $responses
116131
*
117-
* @return ClientDataCollector
132+
* @return ClientDataProvider
118133
*/
119134
public function setResponses($responses)
120135
{
@@ -152,4 +167,14 @@ public function getResponseStack($idx)
152167
{
153168
return $this->responses[$idx];
154169
}
170+
171+
/**
172+
* @param int $idx
173+
*
174+
* @return array failures
175+
*/
176+
public function getFailureStack($idx)
177+
{
178+
return $this->failure[$idx];
179+
}
155180
}

Collector/DebugPlugin.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
5252

5353
return $next($request)->then(function (ResponseInterface $response) use ($collector, $clientName, &$depth) {
5454
$collector->addResponse($response, $clientName, $depth--);
55-
56-
55+
5756
return $response;
5857
}, function (Exception $exception) use ($collector, $clientName, &$depth) {
5958
$collector->addFailure($exception, $clientName, $depth--);

Collector/DebugPluginCollector.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function addRequest(RequestInterface $request, $clientName, $depth)
4848
public function addResponse(ResponseInterface $response, $clientName, $depth)
4949
{
5050
$this->data[$clientName]['response'][$depth][] = $this->formatter->formatResponse($response);
51+
$this->data[$clientName]['failure'][$depth][] = false;
5152
}
5253

5354
/**
@@ -76,8 +77,12 @@ public function getSucessfulRequests()
7677
{
7778
$count = 0;
7879
foreach ($this->data as $client) {
79-
if (isset($client['request'])) {
80-
$count += count($client['request'][0]);
80+
if (isset($client['failure'])) {
81+
foreach ($client['failure'][0] as $failure) {
82+
if (!$failure) {
83+
$count++;
84+
}
85+
}
8186
}
8287
}
8388

@@ -94,7 +99,11 @@ public function getFailedRequests()
9499
$count = 0;
95100
foreach ($this->data as $client) {
96101
if (isset($client['failure'])) {
97-
$count += count($client['failure'][0]);
102+
foreach ($client['failure'][0] as $failure) {
103+
if ($failure) {
104+
$count++;
105+
}
106+
}
98107
}
99108
}
100109

@@ -112,11 +121,11 @@ public function getTotalRequests()
112121
}
113122

114123
/**
115-
* @return ClientDataCollector[]
124+
* @return ClientDataProvider[]
116125
*/
117126
public function getClients()
118127
{
119-
return ClientDataCollector::createFromCollectedData($this->data);
128+
return ClientDataProvider::createFromCollectedData($this->data);
120129
}
121130

122131
/**

Collector/Twig/HttpMessageMarkupExtension.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ public function getFilters()
2525
public function markup($message)
2626
{
2727
$safeMessage = htmlentities($message);
28-
list($headers, $body) = preg_split('|\\r?\\n\\r?\\n|', $safeMessage, 2);
28+
$parts = preg_split('|\\r?\\n\\r?\\n|', $safeMessage, 2);
29+
30+
if (!isset($parts[1])) {
31+
// This is not a HTTP message
32+
return $safeMessage;
33+
}
2934

3035
// make header names bold
31-
$headers = preg_replace("|\n(.*?): |si", "\n<b>$1</b>: ", $headers);
36+
$headers = preg_replace("|\n(.*?): |si", "\n<b>$1</b>: ", $parts[0]);
3237

33-
return sprintf("%s\n\n<div class='httplug-http-body'>%s</div>", $headers, $body);
38+
return sprintf("%s\n\n<div class='httplug-http-body'>%s</div>", $headers, $parts[1]);
3439
}
3540

3641
public function getName()

Resources/public/style/httplug.css

+3
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
.httplug-request-stack {
1919

2020
display: none;
21+
}
22+
.httplug-error {
23+
color: red;
2124
}

Resources/views/webprofiler.html.twig

+20-6
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@
6464
</p>
6565

6666
{% for stackIndex in client.stackIndexKeys %}
67-
<h3>Request {{ stackIndex }}</h3>
68-
{{ macro.printMessages(client.requstStack(stackIndex), client.responseStack(stackIndex), collector.journal.plugins(name)) }}
67+
{% set failureStack = client.failureStack(stackIndex) %}
68+
<h3>
69+
Request {{ stackIndex }}
70+
{% if failureStack[0] %}
71+
- <span class="httplug-error">Errored</span>
72+
{% endif %}
73+
</h3>
74+
{{ macro.printMessages(client.requstStack(stackIndex), client.responseStack(stackIndex), failureStack, collector.journal.plugins(name)) }}
6975
{% endfor %}
7076
</div>
7177
</div>
@@ -79,7 +85,7 @@
7985

8086
{% endblock %}
8187

82-
{% macro printMessages(requestStack, responseStack, pluginNames) %}
88+
{% macro printMessages(requestStack, responseStack, failureStack, pluginNames) %}
8389
<table class="httplug-request-table">
8490
<tr>
8591
<th width="50%">Request</th>
@@ -101,12 +107,20 @@
101107
{# We do not have a plugin at the first entry in the stack #}
102108
<tr class="httplug-request-stack">
103109
<td class="httplug-plugin-name">&darr; Start </td>
104-
<td class="httplug-plugin-name">- End</td>
110+
<td class="httplug-plugin-name">- End
111+
{% if failureStack[idx] %}
112+
<span class="httplug-error">&#9747;</span>
113+
{% endif %}
114+
</td>
105115
</tr>
106116
{% else %}
107117
<tr class="httplug-request-stack">
108118
<td class="httplug-plugin-name">&darr; {{ pluginNames[idx-1] }} </td>
109-
<td class="httplug-plugin-name">&uarr;</td>
119+
<td class="httplug-plugin-name">&uarr;
120+
{% if failureStack[idx] %}
121+
<span class="httplug-error">&#9747;</span>
122+
{% endif %}
123+
</td>
110124
</tr>
111125
{% endif %}
112126
<tr class="httplug-request-stack">
@@ -115,7 +129,7 @@
115129
</tr>
116130
{% if loop.last %}
117131
<tr class="httplug-request-stack">
118-
<td class="httplug-plugin-name">&#10230; <span class="push-right">Internet</span></td>
132+
<td class="httplug-plugin-name">&#10230; <span class="push-right">HTTP client</span></td>
119133
<td class="httplug-plugin-name">&uarr;</td>
120134
</tr>
121135
{% endif %}

0 commit comments

Comments
 (0)