# Inspired from GENI error codes
import time
import pprint
class ResultValue(dict):
# type
SUCCESS = 0
WARNING = 1
ERROR = 2
# origin
CORE = 0
GATEWAY = 1
# code
SUCCESS = 0
SERVERBUSY = 32001
BADARGS = 1
ERROR = 2
FORBIDDEN = 3
BADVERSION = 4
SERVERERROR = 5
TOOBIG = 6
REFUSED = 7
TIMEDOUT = 8
DBERROR = 9
RPCERROR = 10
# description
ERRSTR = {
SUCCESS : 'Success',
SERVERBUSY : 'Server is (temporarily) too busy; try again later',
BADARGS : 'Bad Arguments: malformed',
ERROR : 'Error (other)',
FORBIDDEN : 'Operation Forbidden: eg supplied credentials do not provide sufficient privileges (on the given slice)',
BADVERSION : 'Bad Version (eg of RSpec)',
SERVERERROR : 'Server Error',
TOOBIG : 'Too Big (eg request RSpec)',
REFUSED : 'Operation Refused',
TIMEDOUT : 'Operation Timed Out',
DBERROR : 'Database Error',
RPCERROR : ''
}
ALLOWED_FIELDS = set(['origin', 'type', 'code', 'value', 'description', 'traceback', 'ts'])
def __init__(self, **kwargs):
# Checks
given = set(kwargs.keys())
cstr_success = set(['code', 'origin', 'value']) <= given
cstr_error = set(['code', 'type', 'origin', 'description']) <= given
assert given <= self.ALLOWED_FIELDS, "Wrong fields in ResultValue constructor: %r" % (given - self.ALLOWED_FIELDS)
assert cstr_success or cstr_error, 'Incomplete set of fields in ResultValue constructor: %r' % given
dict.__init__(self, **kwargs)
# Set missing fields to None
for field in self.ALLOWED_FIELDS - given:
self[field] = None
if not 'ts' in self:
self['ts'] = time.time()
# Internal MySlice errors : return ERROR
# Internal MySlice warnings : return RESULT WITH WARNINGS
# Debug : add DEBUG INFORMATION
# Gateway errors : return RESULT WITH WARNING
# all Gateways errors : return ERROR
@classmethod
def get_result_value(self, results, result_value_array):
# let's analyze the results of the query plan
# XXX we should inspect all errors to determine whether to return a
# result or not
if not result_value_array:
# No error
return ResultValue(code=self.SUCCESS, origin=[self.CORE, 0], value=results)
else:
# Handle errors
return ResultValue(code=self.WARNING, origin=[self.CORE, 0], description=result_value_array, value=results)
@classmethod
def get_error(self, error):
return ResultValue(code=error, origin=[self.CORE, 0], value=self.ERRSTR[error])
@classmethod
def get_success(self, result):
return ResultValue(code=self.SUCCESS, origin=[self.CORE, 0], value=result)
def ok_value(self):
return self['value']
def error(self):
err = "%r" % self['description']
@staticmethod
def to_html (raw_dict):
return pprint.pformat (raw_dict).replace("\\n","
")
# 67
# 68 9
# 69
# 70 Database Error
# 71
# 72
# 73 10
# 74
# 75 RPC Error
# 76
# 77
# 78 11
# 79
# 80 Unavailable (eg server in lockdown)
# 81
# 82
# 83 12
# 84
# 85 Search Failed (eg for slice)
# 86
# 87
# 88 13
# 89
# 90 Operation Unsupported
# 91
# 92
# 93 14
# 94
# 95 Busy (resource, slice, or server); try again
# later
# 96
# 97
# 98 15
# 99
# 100 Expired (eg slice)
# 101
# 102
# 103 16
# 104
# 105 In Progress
# 106
# 107
# 108 17
# 109
# 110 Already Exists (eg slice)
# 111
# 112
# 114
# 115 18
# 116
# 117 Required argument(s) missing
# 118
# 119
# 120 19
# 121
# 122 Input Argument outside of legal range
# 123
# 124
# 125 20
# 126
# 127 Not authorized: Supplied credential is
# invalid
# 128
# 129
# 130 21
# 131
# 132 Not authorized: Supplied credential expired
# 133
# 134
# 135 22
# 136
# 137 Not authorized: Supplied credential does not match client
# certificate or does not match the given slice URN
# 138
# 139
# 140 23
# 141
# 142 Not authorized: Supplied credential not signed by a trusted
# authority
# 143
# 144
# 145 24
# 146
# 147 VLAN tag(s) requested not available (likely stitching
# failure)
# 148
# 149
# 150
#