comparison planemo/lib/python3.7/site-packages/galaxy/exceptions/error_codes.py @ 0:d30785e31577 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:18:57 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d30785e31577
1 """Defines the :class:`ErrorCode` class and instantiates concrete objects from JSON.
2
3 See the file error_codes.json for actual error code descriptions.
4 """
5 from json import loads
6
7 from pkg_resources import resource_string
8
9 from galaxy.util import unicodify
10
11
12 # Error codes are provided as a convience to Galaxy API clients, but at this
13 # time they do represent part of the more stable interface. They can change
14 # without warning between releases.
15 UNKNOWN_ERROR_MESSAGE = "Unknown error occurred while processing request."
16
17
18 class ErrorCode(object):
19 """Small class allowing object representation for error descriptions loaded from JSON."""
20
21 def __init__(self, code, default_error_message):
22 """Construct a :class:`ErrorCode` from supplied integer and error message."""
23 self.code = code
24 self.default_error_message = default_error_message or UNKNOWN_ERROR_MESSAGE
25
26 def __str__(self):
27 """Return the error code message."""
28 return str(self.default_error_message)
29
30 def __repr__(self):
31 """Return object representation of this error code."""
32 return "ErrorCode[code=%d,message=%s]" % (self.code, str(self.default_error_message))
33
34 def __int__(self):
35 """Return the error code integer."""
36 return int(self.code)
37
38
39 def _from_dict(entry):
40 """Build a :class:`ErrorCode` object from a JSON entry."""
41 name = entry.get("name")
42 code = entry.get("code")
43 message = entry.get("message")
44 return (name, ErrorCode(code, message))
45
46
47 error_codes_json = unicodify(resource_string(__name__, 'error_codes.json'))
48 for entry in loads(error_codes_json):
49 name, error_code_obj = _from_dict(entry)
50 globals()[name] = error_code_obj