From a2bc070533c555210967ce3df1989d4e5abbd0ad Mon Sep 17 00:00:00 2001 From: Sebastien Besson Date: Mon, 23 Sep 2019 12:05:09 +0100 Subject: [PATCH] Be defensive and handle newly defined HTTP error code This commit implements a change to the linkcheck builder to handle undefined HTTP error code such as https://tools.ietf.org/html/rfc7538. At the moment, Sphinx will fail with an internal error. With this change, a non-registered HTTP error code will be treated in the same way as error code 0 i.e. unknown code. --- sphinx/builders/linkcheck.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/sphinx/builders/linkcheck.py b/sphinx/builders/linkcheck.py index 14aa1c530..1be2041bd 100644 --- a/sphinx/builders/linkcheck.py +++ b/sphinx/builders/linkcheck.py @@ -237,13 +237,16 @@ class CheckExternalLinksBuilder(Builder): else: logger.info(red('broken ') + uri + red(' - ' + info)) elif status == 'redirected': - text, color = { - 301: ('permanently', darkred), - 302: ('with Found', purple), - 303: ('with See Other', purple), - 307: ('temporarily', turquoise), - 0: ('with unknown code', purple), - }[code] + try: + text, color = { + 301: ('permanently', darkred), + 302: ('with Found', purple), + 303: ('with See Other', purple), + 307: ('temporarily', turquoise), + 0: ('with unknown code', purple), + }[code] + except KeyError: + text, color = ('with unknown code', purple) self.write_entry('redirected ' + text, docname, lineno, uri + ' to ' + info) logger.info(color('redirect ') + uri + color(' - ' + text + ' to ' + info))