1 # -*- coding: utf-8 -*-
3 from functools
import wraps
12 Method_Not_Allowed
= 405
16 Service_Unavailable
= 503
17 Internal_Server_Error
= 500
20 class HttpMappedError(Exception):
21 """Base class for a new hierarchy that translate HTTP error codes
24 This class accepts an extra argument ``http_code`` (integer
25 representing HTTP error codes).
28 def __init__(self
, message
, http_code
=Internal_Server_Error
):
29 Exception.__init
__(self
, message
)
30 self
.http_code
= http_code
33 class ErrorHandler(object):
34 """Defines a default strategy for handling HttpMappedError.
36 This class implements a wrapper (can also be used as decorator), that
37 watches out for different exceptions and log them accordingly.
40 logger(logging.Logger): logger object to be used to report errors
42 def __init__(self
, logger
=None):
43 self
.logger
= logger
or logging
.getLogger('openmano.http')
45 def __call__(self
, function
):
47 def _wraped(*args
, **kwargs
):
49 return function(*args
, **kwargs
)
50 except bottle
.HTTPError
:
52 except HttpMappedError
as ex
:
55 function
.__name
__, ex
.http_code
, exc_info
=True)
56 bottle
.abort(ex
.http_code
, str(ex
))
57 except yaml
.YAMLError
as ex
:
59 "YAML error while trying to serialize/unserialize fields",
61 bottle
.abort(Bad_Request
, type(ex
).__name
__ + ": " + str(ex
))
62 except Exception as ex
:
63 self
.logger
.error("Unexpected exception: ", exc_info
=True)
64 bottle
.abort(Internal_Server_Error
,
65 type(ex
).__name
__ + ": " + str(ex
))