|
1 | 1 | import waterbutler.core.exceptions |
2 | 2 |
|
| 3 | +from mfr import settings |
| 4 | + |
3 | 5 |
|
4 | 6 | class PluginError(waterbutler.core.exceptions.PluginError): |
5 | | - """The MFR related errors raised from a plugin |
6 | | - should inherit from PluginError |
| 7 | + """The MFR related errors raised from a plugin should inherit from PluginError |
7 | 8 | """ |
8 | 9 |
|
| 10 | + __TYPE = 'plugin' |
| 11 | + |
| 12 | + def __init__(self, message, *args, code=500, **kwargs): |
| 13 | + super().__init__(message, code) |
| 14 | + self.attr_stack = [ |
| 15 | + ['error', {'message': self.message, 'code': self.code}], |
| 16 | + [self.__TYPE, {}], |
| 17 | + ] |
| 18 | + |
9 | 19 | def as_html(self): |
10 | 20 | return ''' |
11 | 21 | <link rel="stylesheet" href="/static/css/bootstrap.min.css"> |
12 | 22 | <div class="alert alert-warning" role="alert">{}</div> |
13 | | - <div style="display: none;">This text and the text below is only presented because IE consumes error messages below 512 bytes</div> |
14 | | - <div style="display: none;">Want to help save science? Want to get paid to develop free, open source software? Check out our openings!</div> |
15 | | - '''.format(self.message) |
| 23 | + <div style="display: none;">This text and the text below is only presented because |
| 24 | + IE consumes error messages below 512 bytes</div> |
| 25 | + <div style="display: none;">Want to help save science? Want to get paid to develop |
| 26 | + free, open source software? Check out our openings!</div> |
| 27 | + '''.format(self.message) |
| 28 | + |
| 29 | + def _format_original_exception(self, exc): |
| 30 | + """Sometimes we catch an error from an external library, but would like to throw our own |
| 31 | + error instead. This method will take in an external error class and format it for |
| 32 | + consistent representation in the error metrics. |
| 33 | + """ |
| 34 | + formatted_exc = {'class': '', 'message': ''} |
| 35 | + if exc is not None: |
| 36 | + formatted_exc['class'] = exc.__class__.__name__ |
| 37 | + formatted_exc['message'] = str(exc) |
| 38 | + return formatted_exc |
16 | 39 |
|
17 | 40 |
|
18 | 41 | class ExtensionError(PluginError): |
19 | | - """The MFR related errors raised |
20 | | - from a :class:`mfr.core.extension` should |
21 | | - inherit from ExtensionError |
| 42 | + """The MFR related errors raised from a :class:`mfr.core.extension` should inherit from |
| 43 | + ExtensionError |
22 | 44 | """ |
23 | 45 |
|
| 46 | + __TYPE = 'extension' |
| 47 | + |
| 48 | + def __init__(self, message, *args, extension: str='', **kwargs): |
| 49 | + super().__init__(message, *args, **kwargs) |
| 50 | + self.extension = extension |
| 51 | + self.attr_stack.append([self.__TYPE, {'extension': self.extension}]) |
| 52 | + |
24 | 53 |
|
25 | 54 | class RendererError(ExtensionError): |
26 | | - """The MFR related errors raised |
27 | | - from a :class:`mfr.core.extension` and relating |
28 | | - to rendering should inherit from RendererError |
| 55 | + """The MFR related errors raised from a :class:`mfr.core.extension` and relating to rendering |
| 56 | + should inherit from RendererError |
29 | 57 | """ |
30 | 58 |
|
| 59 | + __TYPE = 'renderer' |
| 60 | + |
| 61 | + def __init__(self, message, *args, renderer_class: str='', **kwargs): |
| 62 | + super().__init__(message, *args, **kwargs) |
| 63 | + self.renderer_class = renderer_class |
| 64 | + self.attr_stack.append([self.__TYPE, {'class': self.renderer_class}]) |
| 65 | + |
31 | 66 |
|
32 | 67 | class ExporterError(ExtensionError): |
33 | | - """The MFR related errors raised |
34 | | - from a :class:`mfr.core.extension` and relating |
35 | | - to exporting should inherit from ExporterError |
| 68 | + """The MFR related errors raised from a :class:`mfr.core.extension` and relating to exporting |
| 69 | + should inherit from ExporterError |
36 | 70 | """ |
37 | 71 |
|
| 72 | + __TYPE = 'exporter' |
| 73 | + |
| 74 | + def __init__(self, message, *args, exporter_class: str='', **kwargs): |
| 75 | + super().__init__(message, *args, **kwargs) |
| 76 | + self.exporter_class = exporter_class |
| 77 | + self.attr_stack.append([self.__TYPE, {'exporter_class': self.exporter_class}]) |
| 78 | + |
| 79 | + |
| 80 | +class SubprocessError(ExporterError): |
| 81 | + """The MFR related errors raised from a :class:`mfr.core.extension` and relating to subprocess |
| 82 | + should inherit from SubprocessError |
| 83 | + """ |
| 84 | + |
| 85 | + __TYPE = 'subprocess' |
| 86 | + |
| 87 | + def __init__(self, message, *args, code: int=500, process: str='', cmd: str='', |
| 88 | + returncode: int=None, path: str='', **kwargs): |
| 89 | + super().__init__(message, *args, code=code, **kwargs) |
| 90 | + self.process = process |
| 91 | + self.cmd = cmd |
| 92 | + self.return_code = returncode |
| 93 | + self.path = path |
| 94 | + self.attr_stack.append([self.__TYPE, { |
| 95 | + 'process': self.process, |
| 96 | + 'cmd': self.cmd, |
| 97 | + 'returncode': self.return_code, |
| 98 | + 'path': self.path, |
| 99 | + }]) |
| 100 | + |
38 | 101 |
|
39 | 102 | class ProviderError(PluginError): |
40 | | - """The MFR related errors raised |
41 | | - from a :class:`mfr.core.provider` should |
42 | | - inherit from ProviderError |
| 103 | + """The MFR related errors raised from a :class:`mfr.core.provider` should inherit from |
| 104 | + ProviderError |
43 | 105 | """ |
44 | 106 |
|
| 107 | + __TYPE = 'provider' |
| 108 | + |
| 109 | + def __init__(self, message, *args, provider: str='', **kwargs): |
| 110 | + super().__init__(message, *args, **kwargs) |
| 111 | + self.provider = provider |
| 112 | + self.attr_stack.append([self.__TYPE, {'provider': self.provider}]) |
| 113 | + |
45 | 114 |
|
46 | 115 | class DownloadError(ProviderError): |
47 | | - """The MFR related errors raised |
48 | | - from a :class:`mfr.core.provider` and relating |
49 | | - to downloads should inherit from DownloadError |
| 116 | + """The MFR related errors raised from a :class:`mfr.core.provider` and relating to downloads |
| 117 | + should inherit from DownloadError |
50 | 118 | """ |
51 | 119 |
|
| 120 | + __TYPE = 'download' |
| 121 | + |
| 122 | + def __init__(self, message, *args, download_url: str='', response: str='', **kwargs): |
| 123 | + super().__init__(message, *args, **kwargs) |
| 124 | + self.download_url = download_url |
| 125 | + self.response = response |
| 126 | + self.attr_stack.append([self.__TYPE, { |
| 127 | + 'download_url': self.download_url, |
| 128 | + 'response': self.response |
| 129 | + }]) |
| 130 | + |
52 | 131 |
|
53 | 132 | class MetadataError(ProviderError): |
54 | | - """The MFR related errors raised |
55 | | - from a :class:`mfr.core.provider` and relating |
56 | | - to metadata should inherit from MetadataError |
| 133 | + """The MFR related errors raised from a :class:`mfr.core.provider` and relating to metadata |
| 134 | + should inherit from MetadataError |
57 | 135 | """ |
| 136 | + |
| 137 | + __TYPE = 'metadata' |
| 138 | + |
| 139 | + def __init__(self, message, *args, metadata_url: str='', response: str='', **kwargs): |
| 140 | + super().__init__(message, *args, **kwargs) |
| 141 | + self.metadata_url = metadata_url |
| 142 | + self.response = response |
| 143 | + self.attr_stack.append([self.__TYPE, { |
| 144 | + 'metadata_url': self.metadata_url, |
| 145 | + 'response': self.response |
| 146 | + }]) |
| 147 | + |
| 148 | + |
| 149 | +class DriverManagerError(PluginError): |
| 150 | + |
| 151 | + __TYPE = 'drivermanager' |
| 152 | + |
| 153 | + def __init__(self, message, *args, namespace: str='', name: str='', invoke_on_load: bool=None, |
| 154 | + invoke_args: dict=None, **kwargs): |
| 155 | + super().__init__(message, *args, **kwargs) |
| 156 | + |
| 157 | + self.namespace = namespace |
| 158 | + self.name = name |
| 159 | + self.invoke_on_load = invoke_on_load |
| 160 | + self.invoke_args = invoke_args or {} |
| 161 | + |
| 162 | + self.attr_stack.append([self.__TYPE, { |
| 163 | + 'namespace': self.namespace, |
| 164 | + 'name': self.name, |
| 165 | + 'invoke_on_load': self.invoke_on_load, |
| 166 | + 'invoke_args': self.invoke_args, |
| 167 | + }]) |
| 168 | + |
| 169 | + |
| 170 | +class MakeProviderError(DriverManagerError): |
| 171 | + """Thrown when MFR can't find an applicable provider class. This indicates programmer error, |
| 172 | + so ``code`` defaults to ``500``.""" |
| 173 | + |
| 174 | + def __init__(self, message, *args, code: int=500, **kwargs): |
| 175 | + super().__init__(message, *args, code=code, **kwargs) |
| 176 | + |
| 177 | + |
| 178 | +class UnsupportedExtensionError(DriverManagerError): |
| 179 | + """When make_renderer and make_exporter fail, it's usually because MFR doesn't support that |
| 180 | + extension yet. This error inherits from DriverManagerError (since it's the DriverManager that |
| 181 | + trips this) and includes a handler_type argsument |
| 182 | + """ |
| 183 | + |
| 184 | + __TYPE = 'unsupported_extension' |
| 185 | + |
| 186 | + def __init__(self, *args, code: int=400, handler_type: str='', **kwargs): |
| 187 | + super().__init__(*args, code=code, **kwargs) |
| 188 | + |
| 189 | + self.handler_type = handler_type |
| 190 | + |
| 191 | + self.attr_stack.append([self.__TYPE, {'handler_type': self.handler_type}]) |
| 192 | + |
| 193 | + |
| 194 | +class MakeRendererError(UnsupportedExtensionError): |
| 195 | + """The MFR related errors raised from a :def:`mfr.core.utils.make_renderer` should inherit from |
| 196 | + MakeRendererError |
| 197 | + """ |
| 198 | + |
| 199 | + def __init__(self, *args, **kwargs): |
| 200 | + super().__init__(settings.UNSUPPORTED_RENDER_MSG, *args, handler_type='renderer', |
| 201 | + **kwargs) |
| 202 | + |
| 203 | + |
| 204 | +class MakeExporterError(UnsupportedExtensionError): |
| 205 | + """The MFR related errors raised from a :def:`mfr.core.utils.make_exporter` should inherit from |
| 206 | + MakeExporterError |
| 207 | + """ |
| 208 | + |
| 209 | + def __init__(self, *args, **kwargs): |
| 210 | + super().__init__(settings.UNSUPPORTED_EXPORTER_MSG, *args, handler_type='exporter', |
| 211 | + **kwargs) |
0 commit comments