~devcamcar/django-nova/improve_error_handling

« back to all changes in this revision

Viewing changes to src/django_nova/exceptions.py

  • Committer: Devin Carlen
  • Date: 2011-01-18 23:07:26 UTC
  • Revision ID: devin.carlen@gmail.com-20110118230726-19zxumdux5ibmyml
Improved reporting of nova api errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
import boto.exception
24
24
from django.shortcuts import redirect
25
 
from django.utils.http import urlquote
26
 
 
27
 
 
28
 
class NovaResponseError(Exception):
 
25
from django.core import exceptions as core_exceptions
 
26
 
 
27
 
 
28
class NovaServerError(Exception):
29
29
    """
30
30
    Consumes a BotoServerError and gives more meaningful errors.
31
31
    """
32
32
    def __init__(self, ec2error):
33
 
        self.code = ec2error.reason
34
 
        if ec2error.reason == 'Unauthorized':
35
 
            self.message = 'You do not have the necessary privileges to ' \
36
 
                           'perform the requested action.'
37
 
        else:
38
 
            self.message = 'An unexpected error occurred. ' \
39
 
                           'Please try your request again.'
40
 
 
41
 
    def __str__(self):
42
 
        return self.message
43
 
 
44
 
 
45
 
class NovaUnavailableError(Exception):
 
33
        self.status = ec2error.status
 
34
        self.message = ec2error.reason
 
35
 
 
36
    def __str__(self):
 
37
        return self.message
 
38
 
 
39
 
 
40
class NovaApiError(Exception):
 
41
    """
 
42
    Used when Nova returns a 400 Bad Request status.
 
43
    """
 
44
    def __init__(self, ec2error):
 
45
        self.message = ec2error.error_message
 
46
 
 
47
    def __str__(self):
 
48
        return self.message
 
49
 
 
50
 
 
51
class NovaUnavailableError(NovaServerError):
46
52
    """
47
53
    Used when Nova returns a 503 Service Unavailable status.
48
54
    """
49
55
    pass
50
56
 
51
57
 
 
58
class NovaUnauthorizedError(core_exceptions.PermissionDenied):
 
59
    """
 
60
    Used when Nova returns a 401 Not Authorized status.
 
61
    """
 
62
    pass
 
63
 
 
64
 
52
65
def wrap_nova_error(func):
53
66
    """
54
67
    Used to decorate a function that interacts with boto. It will catch
58
71
        try:
59
72
            return func(*args, **kwargs)
60
73
        except boto.exception.BotoServerError, e:
61
 
            if e.status == 503:
 
74
            if e.status == 400 and e.error_code == 'ApiError':
 
75
                raise NovaApiError(e)
 
76
            elif e.status == 401:
 
77
                raise NovaUnauthorizedError()
 
78
            elif e.status == 503:
62
79
                raise NovaUnavailableError(e)
63
 
            raise NovaResponseError(e)
 
80
            raise NovaServerError(e)
64
81
    return decorator
65
82
 
66
83