From 9986053e73c1ecf74353991c9cb0ef77fd46dcc8 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Sat, 1 Dec 2018 13:39:10 -0500 Subject: [PATCH 1/2] 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); + } }); } }); From ed85daf2ac9e4f06ae5746bda0f5f1cf8a15fd43 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Sat, 1 Dec 2018 13:39:28 -0500 Subject: [PATCH 2/2] User: jobs tab returns its error as a lazy error. --- src/lib/Hydra/Controller/User.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/Hydra/Controller/User.pm b/src/lib/Hydra/Controller/User.pm index e9953a83..cad19d78 100644 --- a/src/lib/Hydra/Controller/User.pm +++ b/src/lib/Hydra/Controller/User.pm @@ -349,9 +349,10 @@ sub dashboard :Chained('dashboard_base') :PathPart('') :Args(0) { sub my_jobs_tab :Chained('dashboard_base') :PathPart('my-jobs-tab') :Args(0) { my ($self, $c) = @_; + $c->stash->{lazy} = 1; $c->stash->{template} = 'dashboard-my-jobs-tab.tt'; - die unless $c->stash->{user}->emailaddress; + error($c, "No email address is set for this user.") unless $c->stash->{user}->emailaddress; # Get all current builds of which this user is a maintainer. $c->stash->{builds} = [$c->model('DB::Builds')->search(