Skip to content

Commit

Permalink
create generic error page to handle exceptions in views. (#1549)
Browse files Browse the repository at this point in the history
* create error page to handle exceptions in views.

* pip and docstring
  • Loading branch information
BradleySappington committed Apr 30, 2024
1 parent ab5532a commit 4340686
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jwql/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def filesystem_path(filename, check_existence=True, search=None):
elif os.path.isfile(full_path_proprietary):
full_path = full_path_proprietary
else:
raise FileNotFoundError('{} is not in the predicted location: {}'.format(filename, full_path))
raise FileNotFoundError('{} is not in the expected location: {}'.format(filename, full_path))

return full_path

Expand Down
10 changes: 8 additions & 2 deletions jwql/website/apps/jwql/data_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,10 @@ def get_additional_exposure_info(root_file_infos, image_info):
# get_image_info() has already globbed over the directory with the files and
# returned the list of existing suffixes, so we shouldn't need to check for
# file existence here.
file_path = filesystem_path(filename, check_existence=True)
try:
file_path = filesystem_path(filename, check_existence=True)
except FileNotFoundError as e:
raise e

header = fits.getheader(file_path)
header_sci = fits.getheader(file_path, 1)
Expand Down Expand Up @@ -1203,7 +1206,10 @@ def get_header_info(filename, filetype):
header_info = {}

# Open the file
fits_filepath = filesystem_path(filename, search=f'*_{filetype}.fits')
try:
fits_filepath = filesystem_path(filename, search=f'*_{filetype}.fits')
except FileNotFoundError as e:
raise e
hdulist = fits.open(fits_filepath)

# Extract header information from file
Expand Down
5 changes: 5 additions & 0 deletions jwql/website/apps/jwql/static/css/jwql.css
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,11 @@
font-size: 0.65rem;
}

.wrapped-error-text {
max-width: 650px; /* Set the maximum width */
word-wrap: break-word; /* Ensure words break if they exceed the width */
}

/*Format the version identifier text in bottom corner*/
#version-div {
float: right;
Expand Down
36 changes: 36 additions & 0 deletions jwql/website/apps/jwql/templates/error_view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends "base.html" %}

{% block preamble %}

<title>There seems to be an issue - JWQL</title>

{% endblock %}

{% block content %}

<main role="main" class="container">

<!--Add JavaScript to make text fade in after video loads-->
<script>
$(document).ready(function() {
$("#space_404_text").delay(500).fadeIn(200);
});
</script>

<!-- The video -->
<video autoplay muted loop id="space_404">
<source src={{ static("img/flyby.mov") }} type="video/mp4" alt="">
</video>

<!-- Overlay text to describe the video -->
<div id="space_404_text">
<h1>Don't Panic!</h1>
<p class="wrapped-error-text">{{ error_message }}</p>
<p class="wrapped-error-text">{{ exception_message }}</p>
<img src={{ static("img/spacecat.jpg") }} alt='SpaceCAT' style="width:650px;height:400px;">

</div>

</main>

{% endblock %}
35 changes: 34 additions & 1 deletion jwql/website/apps/jwql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,14 +1171,23 @@ def view_exposure(request, inst, group_root):

# Get our current views RootFileInfo model and send our "viewed/new" information
root_file_info = RootFileInfo.objects.filter(root_name__startswith=group_root)
if len(root_file_info) == 0:
return generate_error_view(request, inst, f"No groups starting with {group_root} currently in JWQL database.")
viewed = all([rf.viewed for rf in root_file_info])

# Convert expstart from MJD to a date
expstart_str = Time(root_file_info[0].expstart, format='mjd').to_datetime().strftime('%d %b %Y %H:%M')

# Create one dict of info to show at the top of the page, and another dict of info
# to show in the collapsible text box.
basic_info, additional_info = get_additional_exposure_info(root_file_info, image_info)
try:
basic_info, additional_info = get_additional_exposure_info(root_file_info, image_info)
except FileNotFoundError as e:
return generate_error_view(request, inst,
"Looks like at least one of your files has not yet been ingested into the JWQL database. \
If this is a newer observation, please wait a few hours and try again. \
If this observation is over a day old please contact JWQL support.",
exception_message=f"Received Error: '{e}'")

# Build the context
context = {'base_url': get_base_url(),
Expand Down Expand Up @@ -1297,3 +1306,27 @@ def view_image(request, inst, file_root):
'additional_info': additional_info}

return render(request, template, context)


def generate_error_view(request, inst, error_message, exception_message=""):
"""Generate the error view page
Parameters
----------
request : HttpRequest object
Incoming request from the webpage
inst : str
Name of JWST instrument
error_message : str
Custom Error Message to be seen in error_view.html
exception_message: str
if an exception caused this to be generated, pass the exception message along for display
Returns
-------
HttpResponse object
Outgoing response sent to the webpage
"""
template = 'error_view.html'
context = {'base_url': get_base_url(), 'inst': inst, 'error_message': error_message, 'exception_message': exception_message}
return render(request, template, context)

0 comments on commit 4340686

Please sign in to comment.