Controllers: allows lazy tabs to return custom errors.

This commit is contained in:
Samuel Dionne-Riel 2018-12-01 13:39:10 -05:00
parent 14d5577bf8
commit 9986053e73
3 changed files with 17 additions and 2 deletions

View file

@ -237,7 +237,13 @@ sub end : ActionClass('RenderView') {
elsif (scalar @{$c->error}) { elsif (scalar @{$c->error}) {
$c->stash->{resource} = { error => join "\n", @{$c->error} }; $c->stash->{resource} = { error => join "\n", @{$c->error} };
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->{template} = 'error.tt';
}
$c->stash->{errors} = $c->error; $c->stash->{errors} = $c->error;
$c->response->status(500) if $c->response->status == 200; $c->response->status(500) if $c->response->status == 200;
if ($c->response->status >= 300) { if ($c->response->status >= 300) {

5
src/root/lazy_error.tt Normal file
View file

@ -0,0 +1,5 @@
[% PROCESS common.tt %]
[% FOREACH error IN errors %]
<div class="alert alert-error">[% HTML.escape(error).replace('\n', '<br/>') %]</div>
[% END %]

View file

@ -108,9 +108,13 @@ function makeLazyTab(tabName, uri) {
if (id == '#' + tabName && !tabsLoaded[id]) { if (id == '#' + tabName && !tabsLoaded[id]) {
tabsLoaded[id] = 1; tabsLoaded[id] = 1;
$('#' + tabName).load(uri, function(response, status, xhr) { $('#' + tabName).load(uri, function(response, status, xhr) {
if (status == "error") { var lazy = xhr.getResponseHeader("X-Hydra-Lazy") === "Yes";
if (status == "error" && !lazy) {
$('#' + tabName).html("<div class='alert alert-error'>Error loading tab: " + xhr.status + " " + xhr.statusText + "</div>"); $('#' + tabName).html("<div class='alert alert-error'>Error loading tab: " + xhr.status + " " + xhr.statusText + "</div>");
} }
else {
$('#' + tabName).html(response);
}
}); });
} }
}); });