| 1 |
""" |
| 2 |
Global exceptions for the GAVO data center software. |
| 3 |
|
| 4 |
All exceptions escaping modules should inherit from Error in some way. |
| 5 |
Exceptions originating in only one module should usually be defined there, |
| 6 |
exceptions should only be defined here if they are raised by more than |
| 7 |
one module. |
| 8 |
|
| 9 |
Of course, for certain errors, built-in exceptions (e.g., NotImplemented |
| 10 |
or so) may be raised and propagated as well, but these should always |
| 11 |
signify internal bugs, never things a user should be confronted with |
| 12 |
under normal circumstances. |
| 13 |
|
| 14 |
And then there's stuff like fancyconfig that's supposed to live |
| 15 |
independently of the rest. It's ok if those raise other Exceptions, |
| 16 |
but clearly there shouldn't be many of those, or error reporting will |
| 17 |
become an even worse nightmare than it already is. |
| 18 |
""" |
| 19 |
|
| 20 |
|
| 21 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 22 |
#c |
| 23 |
#c This program is free software, covered by the GNU GPL. See the |
| 24 |
#c COPYING file in the source distribution. |
| 25 |
|
| 26 |
|
| 27 |
from gavo.utils.fancyconfig import NoConfigItem #noflake: exported name |
| 28 |
|
| 29 |
from gavo.utils.dachstypes import Optional |
| 30 |
|
| 31 |
|
| 32 |
class Error(Exception): |
| 33 |
"""The base class for all exceptions that can be expected to escape |
| 34 |
a module. |
| 35 |
|
| 36 |
Apart from the normal message, you can give a ``hint`` constructor argument. |
| 37 |
""" |
| 38 |
def __init__(self, msg: str="", hint: Optional[str]=None): |
| 39 |
Exception.__init__(self, msg) |
| 40 |
self.msg = msg |
| 41 |
self.hint = hint |
| 42 |
|
| 43 |
def __str__(self): |
| 44 |
return self.msg |
| 45 |
|
| 46 |
|
| 47 |
class StructureError(Error): |
| 48 |
"""is raised if an error occurs during the construction of |
| 49 |
structures. |
| 50 |
|
| 51 |
You can construct these with pos; this is an opaque object that, when |
| 52 |
stringified, should expand to something that gives the user a rough idea |
| 53 |
of where something went wrong. |
| 54 |
|
| 55 |
Since you will usually not know where you are in the source document |
| 56 |
when you want to raise a StructureError, xmlstruct will try |
| 57 |
to fill pos in when it's still None when it sees a StructureError. |
| 58 |
Thus, you're probably well advised to leave it blank. |
| 59 |
""" |
| 60 |
def __init__(self, |
| 61 |
msg: str, |
| 62 |
pos: Optional[str]=None, |
| 63 |
hint: Optional[str]=None): |
| 64 |
Error.__init__(self, msg, hint=hint) |
| 65 |
self.pos = pos |
| 66 |
self._posInSetMessage = False |
| 67 |
|
| 68 |
def addPos(self, baseMsg: str): |
| 69 |
if self.pos is None or self._posInSetMessage: |
| 70 |
return baseMsg |
| 71 |
else: |
| 72 |
return "At %s: %s"%(str(self.pos), baseMsg) |
| 73 |
|
| 74 |
def __str__(self): |
| 75 |
return self.addPos(self.msg) |
| 76 |
|
| 77 |
def _get_posInMsg(self) -> bool: |
| 78 |
return self._posInSetMessage or self.pos is not None |
| 79 |
|
| 80 |
def _set_posInMsg(self, val:bool) -> None: |
| 81 |
self._posInSetMessage = val |
| 82 |
|
| 83 |
posInMsg = property(_get_posInMsg, _set_posInMsg) |
| 84 |
|
| 85 |
|
| 86 |
class LiteralParseError(StructureError): |
| 87 |
"""is raised if an attribute literal is somehow bad. |
| 88 |
|
| 89 |
LiteralParseErrors are constructed with the name of the attribute |
| 90 |
that was being parsed, the offending literal, and optionally a |
| 91 |
parse position and a hint. |
| 92 |
""" |
| 93 |
def __init__(self, |
| 94 |
attName: str, |
| 95 |
literal: Optional[str], |
| 96 |
pos: Optional[str]=None, |
| 97 |
hint: Optional[str]=None): |
| 98 |
if literal is None: |
| 99 |
StructureError.__init__(self, |
| 100 |
"<literal not provided>", pos=pos, hint=hint) |
| 101 |
else: |
| 102 |
StructureError.__init__(self, literal, pos=pos, hint=hint) |
| 103 |
self.attName, self.literal = attName, literal |
| 104 |
|
| 105 |
def __str__(self): |
| 106 |
return self.addPos( |
| 107 |
"'%s' is not a valid value for %s"%(self.literal, self.attName)) |
| 108 |
|
| 109 |
|
| 110 |
class RestrictedElement(StructureError): |
| 111 |
"""is raised when elements forbidden in restricted RDs are encountered |
| 112 |
when restricted parsing is in effect. |
| 113 |
""" |
| 114 |
def __init__(self, |
| 115 |
elName: str, |
| 116 |
pos: Optional[str]=None, |
| 117 |
hint: Optional[str]=None): |
| 118 |
if hint is None: |
| 119 |
hint='If you are actually sure this RD is what you think it it,' |
| 120 |
' you could always gavo imp it from the command line' |
| 121 |
StructureError.__init__(self, "Illegal: "+elName, pos=pos, hint=hint) |
| 122 |
self.elName = elName |
| 123 |
|
| 124 |
def __str__(self): |
| 125 |
return self.addPos("'%s' is an illegal attribute or element" |
| 126 |
" when parsing from untrusted sources."%self.elName) |
| 127 |
|
| 128 |
|
| 129 |
class BadCode(StructureError): |
| 130 |
"""is raised when some code could not be compiled. |
| 131 |
|
| 132 |
BadCodes are constructed with the offending code, a code type, |
| 133 |
the original exception, and optionally a hint and a position. |
| 134 |
""" |
| 135 |
def __init__(self, |
| 136 |
code: str, |
| 137 |
codeType: str, |
| 138 |
origExc: Exception, |
| 139 |
hint: Optional[str]=None, |
| 140 |
pos: Optional[str]=None): |
| 141 |
if not hint: |
| 142 |
hint = "The offending code was:\n%s"%code |
| 143 |
StructureError.__init__(self, "Bad code", pos=pos, hint=hint) |
| 144 |
self.code, self.codeType = code, codeType |
| 145 |
self.origExc = origExc |
| 146 |
|
| 147 |
def __repr__(self): |
| 148 |
return self.addPos( |
| 149 |
"Bad source code in %s (%s)"%( |
| 150 |
self.codeType, str(self.origExc))) |
| 151 |
|
| 152 |
def __str__(self): |
| 153 |
return repr(self) |
| 154 |
|
| 155 |
|
| 156 |
class ValidationError(Error): |
| 157 |
"""is raised when the validation of a field fails. |
| 158 |
|
| 159 |
ValidationErrors are constructed with a message, a column name, |
| 160 |
and optionally a row (i.e., a dict) and a hint. |
| 161 |
""" |
| 162 |
def __init__(self, |
| 163 |
msg:str, |
| 164 |
colName:str, |
| 165 |
row: Optional[dict]=None, |
| 166 |
hint: Optional[str]=None): |
| 167 |
Error.__init__(self, msg, hint=hint) |
| 168 |
self.msg = msg |
| 169 |
self.colName, self.row = colName, row |
| 170 |
|
| 171 |
def __str__(self): |
| 172 |
recStr = "" |
| 173 |
# if self.row: |
| 174 |
# recStr = ", found in: row %s"%repr(self.row) |
| 175 |
if self.colName: |
| 176 |
return "Field %s: %s%s"%(self.colName, self.msg, recStr) |
| 177 |
else: |
| 178 |
return "Unidentified Field: %s%s"%(self.msg, recStr) |
| 179 |
|
| 180 |
|
| 181 |
class MultiplicityError(ValidationError): |
| 182 |
"""is raised when a singleton is passed in multiple times or vice versa. |
| 183 |
""" |
| 184 |
|
| 185 |
|
| 186 |
class SourceParseError(Error): |
| 187 |
"""is raised when some syntax error occurs during a source parse. |
| 188 |
|
| 189 |
They are constructed with the offending input construct (a source line |
| 190 |
or similar, None in a pinch) and the result of the row iterator's getLocator |
| 191 |
call. |
| 192 |
""" |
| 193 |
def __init__(self, |
| 194 |
msg: str, |
| 195 |
offending: Optional[str]=None, |
| 196 |
location: str="unspecified location", |
| 197 |
source: str="<unspecified source>", |
| 198 |
hint: Optional[str]=None): |
| 199 |
Error.__init__(self, msg, hint=hint) |
| 200 |
self.offending, self.location = offending, location |
| 201 |
self.source = source |
| 202 |
|
| 203 |
def __str__(self): |
| 204 |
if self.offending: |
| 205 |
return "At %s: %s, offending %s"%(self.location, self.msg, |
| 206 |
self.offending) |
| 207 |
else: |
| 208 |
return "At %s: %s"%(self.location, self.msg) |
| 209 |
|
| 210 |
|
| 211 |
class DataError(Error): |
| 212 |
"""is raised when something is wrong with a data set. |
| 213 |
|
| 214 |
When facing the web, these yield HTTP status 406. |
| 215 |
""" |
| 216 |
|
| 217 |
|
| 218 |
class ReportableError(Error): |
| 219 |
"""is raised when something decides it can come up with an error message |
| 220 |
that should be presented to the user as-is. |
| 221 |
|
| 222 |
UIs should, consequently, just dump the payload and not try adornments. |
| 223 |
The content should be treated as a unicode string. |
| 224 |
""" |
| 225 |
|
| 226 |
|
| 227 |
class NotFoundError(Error): |
| 228 |
"""is raised when something is asked for something that does not exist. |
| 229 |
|
| 230 |
lookedFor can be an arbitrary object, so be careful when your repr it -- |
| 231 |
that may be long. |
| 232 |
""" |
| 233 |
def __init__(self, |
| 234 |
lookedFor: str, |
| 235 |
what: str, |
| 236 |
within: str, |
| 237 |
hint: Optional[str]=None): |
| 238 |
Error.__init__(self, "ignored", hint=hint) |
| 239 |
self.lookedFor, self.what = lookedFor, what |
| 240 |
self.within = within |
| 241 |
|
| 242 |
def __str__(self): |
| 243 |
return "%s %r could not be located in %s"%( |
| 244 |
self.what, self.lookedFor, self.within) |
| 245 |
|
| 246 |
|
| 247 |
class EmptyData(Error): |
| 248 |
"""is raised within certain protocols to signify a request was successful |
| 249 |
but yielded no data. |
| 250 |
""" |
| 251 |
|
| 252 |
|
| 253 |
class RDNotFound(NotFoundError): |
| 254 |
"""is raised when an RD cannot be located. |
| 255 |
""" |
| 256 |
cacheable = True |
| 257 |
|
| 258 |
def __init__(self, rdId:str, hint: Optional[str]=None): |
| 259 |
NotFoundError.__init__(self, rdId, hint=hint, what="Resource descriptor", |
| 260 |
within="file system") |
| 261 |
|
| 262 |
|
| 263 |
class DBTableError(Error): |
| 264 |
"""is raised when a manipulation of an on-disk table fails. |
| 265 |
|
| 266 |
It always has a qName attribute containing the qualified name of |
| 267 |
the table causing the trouble. |
| 268 |
""" |
| 269 |
def __init__(self, msg, qName, hint=None): |
| 270 |
Error.__init__(self, msg, hint=hint) |
| 271 |
self.qName = qName |
| 272 |
self.args = [msg, qName] |
| 273 |
|
| 274 |
|
| 275 |
class ExecutiveAction(Exception): |
| 276 |
"""is a base class for exceptions that are supposed to break out of |
| 277 |
deep things and trigger actions higher up. |
| 278 |
""" |
| 279 |
|
| 280 |
|
| 281 |
class SkipThis(ExecutiveAction): |
| 282 |
"""is caught in rsc.makeData. You can raise this at any place during |
| 283 |
source processing to skip the rest of this source but the go on. |
| 284 |
|
| 285 |
You should pass something descriptive as message so upstream can |
| 286 |
potentially report something is skipped and why. |
| 287 |
|
| 288 |
Note: in a rowmaker, you probably usually want to raise IgnoreThisRow |
| 289 |
instead; it's rare that you want to ignore the rest of a source just |
| 290 |
because you don't like a row. |
| 291 |
""" |
| 292 |
|
| 293 |
|
| 294 |
from gavo.utils.parsetricks import ( #noflake: exported name |
| 295 |
ParseBaseException as ParseException) |