Commit ed3e5f26 authored by kaiyou's avatar kaiyou

Intercept exceptions automatically

parent 8eae7f36
from amonit import util
import requests
@util.wrap
def simple(context, url, expected_code=None):
""" Run a GET http request and test the HTTP response code
"""
req = requests.get(url)
return {
"up": req.status_code != (expected_code or 200),
"up": req.status_code == (expected_code or 200),
"code": req.status_code,
"message": "the page returned code {}".format(req.status_code)
}
......@@ -16,15 +16,19 @@ def render(template, data):
def wrap(function):
def replacement(context, *args, **kwargs):
def replacement(*args, **kwargs):
try:
message = str(function(*args, **kwargs))
up = True
result = str(function(*args, **kwargs))
if type(result) is not dict or "up" not in result:
result = {
"up": True,
"message": result
}
except Exception as error:
message = str(error)
up = False
return {
"up": up,
"message": message
result = {
"up": False,
"error": type(error).__name__,
"message": str(error)
}
return result
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