From 9986053e73c1ecf74353991c9cb0ef77fd46dcc8 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Sat, 1 Dec 2018 13:39:10 -0500 Subject: [PATCH] Controllers: allows lazy tabs to return custom errors. --- src/lib/Hydra/Controller/Root.pm | 8 +++++++- src/root/lazy_error.tt | 5 +++++ src/root/static/js/common.js | 6 +++++- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 src/root/lazy_error.tt diff --git a/src/lib/Hydra/Controller/Root.pm b/src/lib/Hydra/Controller/Root.pm index 146c37a4..a486091f 100644 --- a/src/lib/Hydra/Controller/Root.pm +++ b/src/lib/Hydra/Controller/Root.pm @@ -237,7 +237,13 @@ sub end : ActionClass('RenderView') { elsif (scalar @{$c->error}) { $c->stash->{resource} = { error => join "\n", @{$c->error} }; - $c->stash->{template} = 'error.tt'; + if ($c->stash->{lazy}) { + $c->response->headers->header('X-Hydra-Lazy', 'Yes'); + $c->stash->{template} = 'lazy_error.tt'; + } + else { + $c->stash->{template} = 'error.tt'; + } $c->stash->{errors} = $c->error; $c->response->status(500) if $c->response->status == 200; if ($c->response->status >= 300) { diff --git a/src/root/lazy_error.tt b/src/root/lazy_error.tt new file mode 100644 index 00000000..38bcfcb6 --- /dev/null +++ b/src/root/lazy_error.tt @@ -0,0 +1,5 @@ +[% PROCESS common.tt %] + +[% FOREACH error IN errors %] +
[% HTML.escape(error).replace('\n', '
') %]
+[% END %] diff --git a/src/root/static/js/common.js b/src/root/static/js/common.js index 1fe2f778..baa74ae6 100644 --- a/src/root/static/js/common.js +++ b/src/root/static/js/common.js @@ -108,9 +108,13 @@ function makeLazyTab(tabName, uri) { if (id == '#' + tabName && !tabsLoaded[id]) { tabsLoaded[id] = 1; $('#' + tabName).load(uri, function(response, status, xhr) { - if (status == "error") { + var lazy = xhr.getResponseHeader("X-Hydra-Lazy") === "Yes"; + if (status == "error" && !lazy) { $('#' + tabName).html("
Error loading tab: " + xhr.status + " " + xhr.statusText + "
"); } + else { + $('#' + tabName).html(response); + } }); } });