# Buggy code
def get_pre_post(self, client, node, replaceEnt):
    pre = ""
    if node["ids"]:
        if node["ids"][0] not in client.targets:
            pre = '<a name="%s"/>' % node["ids"][0]
            client.targets.append(node["ids"][0])
    else:
        pre = '<a name="%s"/>' % node["refuri"]
        client.targets.append(node["refuri"])
    return pre, ""


# Traceback (KeyError)
Traceback (most recent call last):
File "/usr/local/bin/rst2pdf", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/createpdf.py", line 1511, in main
compressed=options.compressed)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/createpdf.py", line 570, in createPdf
elements = self.gen_elements(self.doctree)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/basenodehandler.py", line 251, in elemdispatch
elements = self.getelements(client, node, style)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/basenodehandler.py", line 226, in getelements
elements = self.gather_elements(client, node, style)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/basenodehandler.py", line 206, in gather_elements
return client.gather_elements(node, style=style)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/createpdf.py", line 400, in gather_elements
r.extend(self.gen_elements(n, style=style))
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/basenodehandler.py", line 251, in elemdispatch
elements = self.getelements(client, node, style)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/basenodehandler.py", line 226, in getelements
elements = self.gather_elements(client, node, style)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/genelements.py", line 188, in gather_elements
return [Paragraph(client.gen_pdftext(node), style)]
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/basenodehandler.py", line 281, in textdispatch
text = self.get_text(client, node, replaceEnt)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/basenodehandler.py", line 267, in get_text
return client.gather_pdftext(node)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/createpdf.py", line 390, in gather_pdftext
for n in node.children])
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/basenodehandler.py", line 280, in textdispatch
pre, post = self.get_pre_post(client, node, replaceEnt)
File "/usr/local/lib/python2.7/dist-packages/rst2pdf/genpdftext.py", line 237, in get_pre_post
pre = u'<a name="%s"/>' % node['refuri']
File "/usr/local/lib/python2.7/dist-packages/docutils/nodes.py", line 585, in __getitem__
return self.attributes[key]
KeyError: 'refuri'

# Fixed code
def get_pre_post(self, client, node, replaceEnt):
    pre = ""
    if node["ids"]:
        if node["ids"][0] not in client.targets:
            pre = '<a name="%s"/>' % node["ids"][0]
            client.targets.append(node["ids"][0])
    else:
        for attr in ["refid", "refuri"]:
            value = node.get(attr)
            if value:
                pre = '<a name="%s"/>' % value
                client.targets.append(value)
                break
        else:
            raise ValueError(
                "Target node '%s' doesn't have neither 'refid' or 'refuri'" % node
            )
    return pre, ""