# Buggy code
def handle_page(
    self, pagename, addctx, templatename="page.html", outfilename=None, event_arg=None
):
    """Create a rendered page.

    This method is overwritten for genindex pages in order to fix href link
    attributes.
    """
    if pagename.startswith("genindex"):
        if not self.use_index:
            return
        self.fix_genindex(addctx["genindexentries"])
    addctx["doctype"] = self.doctype
    StandaloneHTMLBuilder.handle_page(
        self, pagename, addctx, templatename, outfilename, event_arg
    )


# Traceback (KeyError)
# Sphinx version: 1.5.5
# Python version: 2.7.12 (CPython)
# Docutils version: 0.13.1 release
# Jinja2 version: 2.9.6
# Last messages:
#   writing output... [ 25%] api/serv_soft/gui
#   writing output... [ 27%] api/serv_soft/ini
#   writing output... [ 29%] api/serv_soft/instala
#   writing output... [ 31%] api/serv_soft/m360
#   writing output... [ 34%] api/serv_soft/problemas
#   writing output... [ 36%] api/serv_soft/python
#   writing output... [ 38%] api/serv_soft/rec
#   writing output... [ 40%] api/serv_soft/so
#   writing output... [ 43%] api/solamente
#   writing output... [ 45%] genindex
# Loaded extensions:
#   sphinx.ext.todo (1.5.5) from /home/tony/.local/lib/python2.7/site-packages/sphinx/ext/todo.pyc
#   sphinx.ext.autodoc (1.5.5) from /home/tony/.local/lib/python2.7/site-packages/sphinx/ext/autodoc.pyc
#   alabaster (0.7.10) from /home/tony/.local/lib/python2.7/site-packages/alabaster/__init__.pyc
Traceback (most recent call last):
File "/home/tony/.local/lib/python2.7/site-packages/sphinx/cmdline.py", line 296, in main
app.build(opts.force_all, filenames)
File "/home/tony/.local/lib/python2.7/site-packages/sphinx/application.py", line 333, in build
self.builder.build_update()
File "/home/tony/.local/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 251, in build_update
'out of date' % len(to_build))
File "/home/tony/.local/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 322, in build
self.write(docnames, list(updated_docnames), method)
File "/home/tony/.local/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 360, in write
self._write_serial(sorted(docnames), warnings)
File "/home/tony/.local/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 368, in _write_serial
self.write_doc(docname, doctree)
File "/home/tony/.local/lib/python2.7/site-packages/sphinx/builders/epub.py", line 416, in write_doc
StandaloneHTMLBuilder.write_doc(self, docname, doctree)
File "/home/tony/.local/lib/python2.7/site-packages/sphinx/builders/html.py", line 461, in write_doc
self.handle_page(docname, ctx, event_arg=doctree)
File "/home/tony/.local/lib/python2.7/site-packages/sphinx/builders/epub.py", line 506, in handle_page
self.fix_genindex(addctx['genindexentries'])
KeyError: 'genindexentries'

# Fixed code
def handle_page(
    self, pagename, addctx, templatename="page.html", outfilename=None, event_arg=None
):
    """Create a rendered page.

    This method is overwritten for genindex pages in order to fix href link
    attributes.
    """
    if pagename.startswith("genindex") and "genindexentries" in addctx:
        if not self.use_index:
            return
        self.fix_genindex(addctx["genindexentries"])
    addctx["doctype"] = self.doctype
    StandaloneHTMLBuilder.handle_page(
        self, pagename, addctx, templatename, outfilename, event_arg
    )