Commit ed3e5f26 authored by kaiyou's avatar kaiyou

Intercept exceptions automatically

parent 8eae7f36
from amonit import util
import requests import requests
@util.wrap
def simple(context, url, expected_code=None): def simple(context, url, expected_code=None):
""" Run a GET http request and test the HTTP response code """ Run a GET http request and test the HTTP response code
""" """
req = requests.get(url) req = requests.get(url)
return { return {
"up": req.status_code != (expected_code or 200), "up": req.status_code == (expected_code or 200),
"code": req.status_code, "code": req.status_code,
"message": "the page returned code {}".format(req.status_code) "message": "the page returned code {}".format(req.status_code)
} }
...@@ -16,15 +16,19 @@ def render(template, data): ...@@ -16,15 +16,19 @@ def render(template, data):
def wrap(function): def wrap(function):
def replacement(context, *args, **kwargs): def replacement(*args, **kwargs):
try: try:
message = str(function(*args, **kwargs)) result = str(function(*args, **kwargs))
up = True if type(result) is not dict or "up" not in result:
result = {
"up": True,
"message": result
}
except Exception as error: except Exception as error:
message = str(error) result = {
up = False "up": False,
return { "error": type(error).__name__,
"up": up, "message": str(error)
"message": message }
} return result
return replacement return replacement
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment