Skip to content

Commit c573b30

Browse files
committed
Merge remote-tracking branch 'jdufresne/show-network-errors'
* jdufresne/show-network-errors: Display all fetch() errors in ajax()
2 parents bbef167 + 68de1b7 commit c573b30

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

debug_toolbar/static/debug_toolbar/js/utils.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,24 @@ const $$ = {
3636

3737
function ajax(url, init) {
3838
init = Object.assign({ credentials: "same-origin" }, init);
39-
return fetch(url, init).then(function (response) {
40-
if (response.ok) {
41-
return response.json();
42-
} else {
39+
return fetch(url, init)
40+
.then(function (response) {
41+
if (response.ok) {
42+
return response.json();
43+
}
44+
return Promise.reject(
45+
new Error(response.status + ": " + response.statusText)
46+
);
47+
})
48+
.catch(function (error) {
4349
const win = document.querySelector("#djDebugWindow");
4450
win.innerHTML =
4551
'<div class="djDebugPanelTitle"><a class="djDebugClose" href="">»</a><h3>' +
46-
response.status +
47-
": " +
48-
response.statusText +
52+
error.message +
4953
"</h3></div>";
5054
$$.show(win);
51-
return Promise.reject();
52-
}
53-
});
55+
throw error;
56+
});
5457
}
5558

5659
function ajaxForm(element) {

tests/test_integration.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from django.test.utils import override_settings
1313

1414
from debug_toolbar.middleware import DebugToolbarMiddleware, show_toolbar
15+
from debug_toolbar.panels import Panel
1516
from debug_toolbar.toolbar import DebugToolbar
1617

1718
from .base import BaseTestCase, IntegrationTestCase
@@ -31,6 +32,15 @@
3132
rf = RequestFactory()
3233

3334

35+
class BuggyPanel(Panel):
36+
def title(self):
37+
return "BuggyPanel"
38+
39+
@property
40+
def content(self):
41+
raise Exception
42+
43+
3444
@override_settings(DEBUG=True)
3545
class DebugToolbarTestCase(BaseTestCase):
3646
def test_show_toolbar(self):
@@ -466,3 +476,11 @@ def test_sql_action_and_go_back(self):
466476

467477
# SQL panel is still visible
468478
self.assertTrue(sql_panel.is_displayed())
479+
480+
@override_settings(DEBUG_TOOLBAR_PANELS=["tests.test_integration.BuggyPanel"])
481+
def test_displays_server_error(self):
482+
self.selenium.get(self.live_server_url + "/regular/basic/")
483+
debug_window = self.selenium.find_element_by_id("djDebugWindow")
484+
self.selenium.find_element_by_class_name("BuggyPanel").click()
485+
WebDriverWait(self.selenium, timeout=3).until(EC.visibility_of(debug_window))
486+
self.assertEqual(debug_window.text, \n500: Internal Server Error")

0 commit comments

Comments
 (0)