Revision 0dc4b304

b/snf-cyclades-app/synnefo/quotas/__init__.py
37 37
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN,
38 38
                              ASTAKOS_AUTH_URL)
39 39
from astakosclient import AstakosClient
40
from astakosclient.errors import AstakosClientException, QuotaLimit
41
from functools import wraps
40
from astakosclient import errors
42 41

  
43 42
import logging
44 43
log = logging.getLogger(__name__)
......
71 70
        return cls._object
72 71

  
73 72

  
74
def handle_astakosclient_error(func):
75
    """Decorator for converting astakosclient errors to 500."""
76
    @wraps(func)
77
    def wrapper(*args, **kwargs):
78
        try:
79
            return func(*args, **kwargs)
80
        except AstakosClientException:
81
            log.exception("Unexpected error")
73
class AstakosClientExceptionHandler(object):
74
    def __init__(self, *args, **kwargs):
75
        pass
76

  
77
    def __enter__(self):
78
        pass
79

  
80
    def __exit__(self, exc_type, value, traceback):
81
        if value is not None:  # exception
82
            if not isinstance(value, errors.AstakosClientException):
83
                return False  # reraise
84
            if exc_type is errors.QuotaLimit:
85
                msg, details = render_overlimit_exception(value)
86
                raise faults.OverLimit(msg, details=details)
87

  
88
            log.exception("Unexpected error %s" % value.message)
82 89
            raise faults.InternalServerError("Unexpected error")
83
    return wrapper
84 90

  
85 91

  
86
@handle_astakosclient_error
87 92
def issue_commission(resource, action, name="", force=False, auto_accept=False,
88 93
                     action_fields=None):
89 94
    """Issue a new commission to the quotaholder.
......
103 108
    source = DEFAULT_SOURCE
104 109

  
105 110
    qh = Quotaholder.get()
106
    try:
107
        if True:  # placeholder
111
    if True:  # placeholder
112
        with AstakosClientExceptionHandler():
108 113
            serial = qh.issue_one_commission(user, source,
109 114
                                             provisions, name=name,
110 115
                                             force=force,
111 116
                                             auto_accept=auto_accept)
112
    except QuotaLimit as e:
113
        msg, details = render_overlimit_exception(e)
114
        raise faults.OverLimit(msg, details=details)
115 117

  
116 118
    if serial:
117 119
        serial_info = {"serial": serial}
......
152 154
    return resolve_commissions(reject=rejected, strict=strict)
153 155

  
154 156

  
155
@handle_astakosclient_error
156 157
def resolve_commissions(accept=None, reject=None, strict=True):
157 158
    if accept is None:
158 159
        accept = []
......
160 161
        reject = []
161 162

  
162 163
    qh = Quotaholder.get()
163
    response = qh.resolve_commissions(accept, reject)
164
    with AstakosClientExceptionHandler():
165
        response = qh.resolve_commissions(accept, reject)
164 166

  
165 167
    if strict:
166 168
        failed = response["failed"]

Also available in: Unified diff