Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/galaxy/exceptions/error_codes.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
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 from typing import Dict | |
7 | |
8 from pkg_resources import resource_string | |
9 | |
10 from galaxy.util import unicodify | |
11 | |
12 | |
13 # Error codes are provided as a convience to Galaxy API clients, but at this | |
14 # time they do represent part of the more stable interface. They can change | |
15 # without warning between releases. | |
16 UNKNOWN_ERROR_MESSAGE = "Unknown error occurred while processing request." | |
17 | |
18 | |
19 class ErrorCode: | |
20 """Small class allowing object representation for error descriptions loaded from JSON.""" | |
21 | |
22 def __init__(self, code, default_error_message): | |
23 """Construct a :class:`ErrorCode` from supplied integer and error message.""" | |
24 self.code = code | |
25 self.default_error_message = default_error_message or UNKNOWN_ERROR_MESSAGE | |
26 | |
27 def __str__(self): | |
28 """Return the error code message.""" | |
29 return str(self.default_error_message) | |
30 | |
31 def __repr__(self): | |
32 """Return object representation of this error code.""" | |
33 return "ErrorCode[code=%d,message=%s]" % (self.code, str(self.default_error_message)) | |
34 | |
35 def __int__(self): | |
36 """Return the error code integer.""" | |
37 return int(self.code) | |
38 | |
39 | |
40 def _from_dict(entry): | |
41 """Build a :class:`ErrorCode` object from a JSON entry.""" | |
42 name = entry.get("name") | |
43 code = entry.get("code") | |
44 message = entry.get("message") | |
45 return (name, ErrorCode(code, message)) | |
46 | |
47 | |
48 error_codes_json = unicodify(resource_string(__name__, 'error_codes.json')) | |
49 error_codes_by_name: Dict[str, ErrorCode] = {} | |
50 | |
51 for entry in loads(error_codes_json): | |
52 name, error_code_obj = _from_dict(entry) | |
53 globals()[name] = error_code_obj | |
54 error_codes_by_name[name] = error_code_obj |