# Buggy code
def get(self, session_id, graph_key):
    from ..scheduler.utils import GraphState

    state = self.web_api.get_graph_state(session_id, graph_key)
    if state == GraphState.RUNNING:
        self.write(json.dumps(dict(state="running")))
    elif state == GraphState.SUCCEEDED:
        self.write(json.dumps(dict(state="success")))
    elif state == GraphState.FAILED:
        self.write(json.dumps(dict(state="failed")))
    elif state == GraphState.CANCELLED:
        self.write(json.dumps(dict(state="cancelled")))
    elif state == GraphState.CANCELLING:
        self.write(json.dumps(dict(state="cancelling")))
    elif state == GraphState.PREPARING:
        self.write(json.dumps(dict(state="preparing")))


# Traceback (ValueError)
500 GET /worker?endpoint
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/tornado/web.py", line 1592, in _execute
result = yield result
File "/opt/conda/lib/python3.6/site-packages/tornado/gen.py", line 1133, in run
value = future.result()
File "/opt/conda/lib/python3.6/site-packages/tornado/gen.py", line 1147, in run
yielded = self.gen.send(value)
File "/opt/conda/lib/python3.6/site-packages/bokeh/server/views/doc_handler.py", line 27, in get
template_variables=session.document.template_variables)
File "/opt/conda/lib/python3.6/site-packages/bokeh/embed/server.py", line 230, in server_html_page_for_session
template=template, template_variables=template_variables)
File "/opt/conda/lib/python3.6/site-packages/bokeh/embed/elements.py", line 133, in html_page_for_render_items
html = template.render(context)
File "/opt/conda/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/opt/conda/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/opt/conda/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/opt/conda/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/admin/work/public-mars-0.2.0b1-cupid.zip/mars/web/templates/worker_detail.html", line 1, in top-level template code
{% extends "base.html" %}
File "/home/admin/work/public-mars-0.2.0b1-cupid.zip/mars/web/templates/base.html", line 65, in top-level template code
{% block body %}{% endblock %}
File "/home/admin/work/public-mars-0.2.0b1-cupid.zip/mars/web/templates/worker_detail.html", line 252, in block "body"
<td>{{ stats['min_est_finish_time'] | format_ts }}</td>
File "/home/admin/work/public-mars-0.2.0b1-cupid.zip/mars/web/server.py", line 49, in format_ts
return datetime.fromtimestamp(value).strftime('%Y-%m-%d %H:%M:%S')
ValueError: Invalid value NaN (not a number)
500 GET /worker?endpoint=10.101.216.148:53630 (10.101.202.142) 137.45ms

# Fixed code
def get(self, session_id, graph_key):
    from ..scheduler.utils import GraphState

    try:
        state = self.web_api.get_graph_state(session_id, graph_key)
    except GraphNotExists:
        raise web.HTTPError(404, "Graph not exists")

    if state == GraphState.RUNNING:
        self.write(json.dumps(dict(state="running")))
    elif state == GraphState.SUCCEEDED:
        self.write(json.dumps(dict(state="success")))
    elif state == GraphState.FAILED:
        self.write(json.dumps(dict(state="failed")))
    elif state == GraphState.CANCELLED:
        self.write(json.dumps(dict(state="cancelled")))
    elif state == GraphState.CANCELLING:
        self.write(json.dumps(dict(state="cancelling")))
    elif state == GraphState.PREPARING:
        self.write(json.dumps(dict(state="preparing")))