Revision d8e50a39 api/faults.py
b/api/faults.py | ||
---|---|---|
1 |
# vim: ts=4 sts=4 et ai sw=4 fileencoding=utf-8 |
|
2 | 1 |
# |
3 |
# Copyright © 2010 Greek Research and Technology Network
|
|
2 |
# Copyright (c) 2010 Greek Research and Technology Network
|
|
4 | 3 |
# |
5 | 4 |
|
6 |
from django.http import HttpResponse |
|
7 |
from django.utils import simplejson as json |
|
8 |
from piston.utils import HttpStatusCode |
|
5 |
def camelCase(s): |
|
6 |
return s[0].lower() + s[1:] |
|
9 | 7 |
|
10 |
class Fault(HttpStatusCode): |
|
11 |
"""Fault Exception""" |
|
12 |
pass |
|
13 | 8 |
|
14 |
class _fault_factory(object): |
|
15 |
""" |
|
16 |
Openstack API Faults factory |
|
17 |
""" |
|
9 |
class Fault(BaseException): |
|
10 |
def __init__(self, message='', details='', name=''): |
|
11 |
BaseException.__init__(self, message, details, name) |
|
12 |
self.message = message |
|
13 |
self.details = details |
|
14 |
self.name = name or camelCase(self.__class__.__name__) |
|
18 | 15 |
|
19 |
faults = { |
|
20 |
'serviceUnavailable': { |
|
21 |
'code': 503, |
|
22 |
'message': 'Service Unavailable', |
|
23 |
}, |
|
24 |
'unauthorized': { |
|
25 |
'code': 401, |
|
26 |
'message': 'Unauthorized', |
|
27 |
}, |
|
28 |
'badRequest': { |
|
29 |
'code': 400, |
|
30 |
'message': 'Bad request', |
|
31 |
}, |
|
32 |
'overLimit': { |
|
33 |
'code': 413, |
|
34 |
'message': 'Overlimit', |
|
35 |
}, |
|
36 |
'badMediaType': { |
|
37 |
'code': 415, |
|
38 |
'message': 'Bad media type', |
|
39 |
}, |
|
40 |
'badMethod': { |
|
41 |
'code': 405, |
|
42 |
'message': 'Bad method', |
|
43 |
}, |
|
44 |
'itemNotFound': { |
|
45 |
'code': 404, |
|
46 |
'message': 'Not Found', |
|
47 |
}, |
|
48 |
'buildInProgress': { |
|
49 |
'code': 409, |
|
50 |
'message': 'Build in progress', |
|
51 |
}, |
|
52 |
'serverCapacityUnavailable': { |
|
53 |
'code': 503, |
|
54 |
'message': 'Server capacity unavailable', |
|
55 |
}, |
|
56 |
'backupOrResizeInProgress': { |
|
57 |
'code': 409, |
|
58 |
'message': 'Backup or resize in progress', |
|
59 |
}, |
|
60 |
'resizeNotAllowed': { |
|
61 |
'code': 403, |
|
62 |
'message': 'Resize not allowed', |
|
63 |
}, |
|
64 |
'notImplemented': { |
|
65 |
'code': 501, |
|
66 |
'message': 'Not Implemented', |
|
67 |
}, |
|
68 |
} |
|
16 |
class BadRequest(Fault): |
|
17 |
code = 400 |
|
69 | 18 |
|
70 |
def __getattr__(self, attr): |
|
71 |
try: |
|
72 |
m = self.faults.get(attr) |
|
73 |
except TypeError: |
|
74 |
raise AttributeError(attr) |
|
19 |
class Unauthorized(Fault): |
|
20 |
code = 401 |
|
75 | 21 |
|
76 |
# details are not supported for now
|
|
77 |
m['details'] = ''
|
|
22 |
class ResizeNotAllowed(Fault):
|
|
23 |
code = 403
|
|
78 | 24 |
|
79 |
# piston > 0.2.2 does the serialization for us, but be compatible |
|
80 |
# 'till the next version gets released. XXX: this doesn't do XML! |
|
81 |
message = json.dumps({ attr: m }, ensure_ascii=False, indent=4) |
|
82 |
code = m['code'] |
|
83 |
response = HttpResponse(message, status=code) |
|
25 |
class ItemNotFound(Fault): |
|
26 |
code = 404 |
|
84 | 27 |
|
85 |
return Fault(response) |
|
86 |
|
|
87 |
|
|
88 |
fault = _fault_factory() |
|
89 |
|
|
90 |
# these are in the 2xx range, hence not faults/exceptions |
|
91 |
noContent = HttpResponse(status=204) |
|
92 |
accepted = HttpResponse(status=202) |
|
93 |
created = HttpResponse(status=201) |
|
94 |
notModified = HttpResponse(status=304) |
|
28 |
class ServiceUnavailable(Fault): |
|
29 |
code = 503 |
Also available in: Unified diff