Skip to content

Commit

Permalink
Unified handling of plain_title() and title().
Browse files Browse the repository at this point in the history
Change-Id: If2d1e7f59bb42eaa3180e0d4f26aaab46cd2241e
  • Loading branch information
spt29 committed Aug 6, 2019
1 parent 55b1d27 commit 4c64e4e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
17 changes: 17 additions & 0 deletions livestatus/api/python/livestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
import ssl
from typing import Tuple, Union, Dict, Pattern, Optional # pylint: disable=unused-import

try:
from cmk.gui.i18n import _
except ImportError:
_ = lambda x: x

# .--Globals-------------------------------------------------------------.
# | ____ _ _ _ |
# | / ___| | ___ | |__ __ _| |___ |
Expand Down Expand Up @@ -76,6 +81,12 @@ def __init__(self, value):
def __str__(self):
return str(self.parameter)

def plain_title(self):
return _("Livestatus problem")

def title(self):
return _("Livestatus problem")


class MKLivestatusSocketError(MKLivestatusException):
pass
Expand All @@ -97,6 +108,12 @@ class MKLivestatusNotFoundError(MKLivestatusException):
def __str__(self):
return "No matching entries found for query %s" % str(self.parameter)

def plain_title(self):
return _("Livestatus-data not found")

def title(self):
return _("Data not found")


class MKLivestatusTableNotFoundError(MKLivestatusException):
pass
Expand Down
1 change: 1 addition & 0 deletions locale/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ update-%:
-L python --from-code=utf-8 --omit-header \
-o scanned.po \
../../../web/app/index.wsgi \
../../../livestatus/api/python/livestatus.py \
$$(find -L ../../../cmk -type f -name "*.py") 2>&1 < /dev/null | \
sed '/format string with unnamed arguments cannot be properly localized/,/and a mapping instead of a tuple for the arguments./d' ; \
cat ../../header scanned.po > new.po ; \
Expand Down
18 changes: 4 additions & 14 deletions web/app/index.wsgi
Original file line number Diff line number Diff line change
Expand Up @@ -117,33 +117,23 @@ class Application(object):
livestatus.MKLivestatusException,
) as e:
# TODO: Refactor all the special cases handled here to simplify the exception handling
ty = type(e)
if ty == livestatus.MKLivestatusNotFoundError:
title = _("Data not found")
plain_title = _("Livestatus-data not found")
elif isinstance(e, livestatus.MKLivestatusException):
title = _("Livestatus problem")
plain_title = _("Livestatus problem")
else:
title = e.title()
plain_title = e.plain_title()

if self._plain_error():
html.set_output_format("text")
html.write("%s: %s\n" % (plain_title, e))
html.write("%s: %s\n" % (e.plain_title(), e))
elif not self._fail_silently():
html.header(title)
html.header(e.title())
html.show_error(e)
html.footer()

# Some exception need to set a specific HTTP status code
ty = type(e)
if ty == MKUnauthenticatedException:
self._response.status_code = httplib.UNAUTHORIZED
elif ty == livestatus.MKLivestatusException:
self._response.status_code = httplib.BAD_GATEWAY

if ty in [MKConfigError, MKGeneralException]:
logger.error("%s: %s" % (plain_title, e))
logger.error("%s: %s" % (e.plain_title(), e))

except Exception as e:
logger.exception("error processing WSGI request")
Expand Down

0 comments on commit 4c64e4e

Please sign in to comment.