Skip to content

Commit 014e121

Browse files
committed
Restructure fatal error messages
Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent 62f8775 commit 014e121

File tree

4 files changed

+86
-56
lines changed

4 files changed

+86
-56
lines changed

src/satosa/base.py

+65-32
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@
88
from saml2.s_utils import UnknownSystemEntity
99

1010
from satosa import util
11-
from satosa.response import Redirect
1211
from satosa.response import BadRequest
1312
from satosa.response import NotFound
13+
from satosa.response import Redirect
1414
from .context import Context
15-
from .exception import SATOSAError
1615
from .exception import SATOSAAuthenticationError
17-
from .exception import SATOSAUnknownError
18-
from .exception import SATOSAMissingStateError
1916
from .exception import SATOSAAuthenticationFlowError
2017
from .exception import SATOSABadRequestError
21-
from .plugin_loader import load_backends, load_frontends
22-
from .plugin_loader import load_request_microservices, load_response_microservices
23-
from .routing import ModuleRouter, SATOSANoBoundEndpointError
24-
from .state import cookie_to_state, SATOSAStateError, State, state_to_cookie
18+
from .exception import SATOSAError
19+
from .exception import SATOSAMissingStateError
20+
from .exception import SATOSANoBoundEndpointError
21+
from .exception import SATOSAUnknownError
22+
from .exception import SATOSAStateError
23+
from .plugin_loader import load_backends
24+
from .plugin_loader import load_frontends
25+
from .plugin_loader import load_request_microservices
26+
from .plugin_loader import load_response_microservices
27+
from .routing import ModuleRouter
28+
from .state import State
29+
from .state import cookie_to_state
30+
from .state import state_to_cookie
2531

2632
import satosa.logging_util as lu
2733

@@ -262,77 +268,104 @@ def run(self, context):
262268
resp = self._run_bound_endpoint(context, spec)
263269
self._save_state(resp, context)
264270
except SATOSABadRequestError as e:
271+
error_id = uuid.uuid4().urn
265272
msg = {
266273
"message": "Bad Request",
267-
"error": e.error,
268-
"error_id": uuid.uuid4().urn
274+
"error": str(e),
275+
"error_id": error_id,
269276
}
270277
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
271278
logger.error(logline)
272279
generic_error_url = self.config.get("ERROR_URL")
273280
if generic_error_url:
281+
redirect_url = f"{generic_error_url}?errorid={error_id}"
274282
return Redirect(generic_error_url)
275-
else:
276-
return BadRequest(e.error)
283+
return BadRequest(error)
277284
except SATOSAMissingStateError as e:
285+
error_id = uuid.uuid4().urn
278286
msg = {
279287
"message": "Missing SATOSA State",
280-
"error": e.error,
281-
"error_id": uuid.uuid4().urn
288+
"error": str(e),
289+
"error_id": error_id,
282290
}
283291
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
284292
logger.error(logline)
285293
generic_error_url = self.config.get("ERROR_URL")
286294
if generic_error_url:
295+
redirect_url = f"{generic_error_url}?errorid={error_id}"
287296
return Redirect(generic_error_url)
288-
else:
289-
raise
297+
raise
290298
except SATOSAAuthenticationFlowError as e:
299+
error_id = uuid.uuid4().urn
291300
msg = {
292301
"message": "SATOSA Authentication Flow Error",
293-
"error": e.error,
294-
"error_id": uuid.uuid4().urn
302+
"error": str(e),
303+
"error_id": error_id,
295304
}
296305
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
297306
logger.error(logline)
298307
generic_error_url = self.config.get("ERROR_URL")
299308
if generic_error_url:
309+
redirect_url = f"{generic_error_url}?errorid={error_id}"
300310
return Redirect(generic_error_url)
301-
else:
302-
raise
311+
raise
303312
except SATOSANoBoundEndpointError as e:
304-
msg = str(e)
313+
error_id = uuid.uuid4().urn
314+
msg = {
315+
"message": "URL-path is not bound to any endpoint function",
316+
"error": str(e),
317+
"error_id": error_id,
318+
}
305319
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
306320
logger.error(logline)
321+
generic_error_url = self.config.get("ERROR_URL")
322+
if generic_error_url:
323+
redirect_url = f"{generic_error_url}?errorid={error_id}"
324+
return Redirect(generic_error_url)
307325
return NotFound("The Service or Identity Provider you requested could not be found.")
308-
except SATOSAError:
309-
msg = "Uncaught SATOSA error"
326+
except SATOSAError as e:
327+
error_id = uuid.uuid4().urn
328+
msg = {
329+
"message": "Uncaught SATOSA error",
330+
"error": str(e),
331+
"error_id": error_id,
332+
}
310333
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
311334
logger.error(logline)
312335
generic_error_url = self.config.get("ERROR_URL")
313336
if generic_error_url:
337+
redirect_url = f"{generic_error_url}?errorid={error_id}"
314338
return Redirect(generic_error_url)
315-
else:
316-
raise
339+
raise
317340
except UnknownSystemEntity as e:
318-
msg = f"Configuration error: unknown system entity: {e}"
341+
error_id = uuid.uuid4().urn
342+
msg = {
343+
"message": "Configuration error: unknown system entity",
344+
"error": str(e),
345+
"error_id": error_id,
346+
}
319347
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
320348
logger.error(logline)
321349
generic_error_url = self.config.get("ERROR_URL")
322350
if generic_error_url:
351+
redirect_url = f"{generic_error_url}?errorid={error_id}"
323352
return Redirect(generic_error_url)
324-
else:
325-
raise
353+
raise
326354
except Exception as e:
327-
msg = "Uncaught exception"
355+
error_id = uuid.uuid4().urn
356+
msg = {
357+
"message": "Uncaught exception",
358+
"error": str(e),
359+
"error_id": error_id,
360+
}
328361
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
329362
logger.error(logline)
330363
generic_error_url = self.config.get("ERROR_URL")
331364
if generic_error_url:
332365
return Redirect(generic_error_url)
333-
else:
334-
raise SATOSAUnknownError("Unknown error") from e
335-
return resp
366+
raise SATOSAUnknownError("Unknown error") from e
367+
else:
368+
return resp
336369

337370

338371
class SAMLBaseModule(object):

src/satosa/context.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
from warnings import warn as _warn
22

3-
from satosa.exception import SATOSAError
4-
5-
6-
class SATOSABadContextError(SATOSAError):
7-
"""
8-
Raise this exception if validating the Context and failing.
9-
"""
10-
pass
3+
from satosa.exception import SATOSABadContextError
114

125

136
class Context(object):

src/satosa/exception.py

+18
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ def message(self):
6868
"""
6969
return self._message.format(error_id=self.error_id)
7070

71+
7172
class SATOSABasicError(SATOSAError):
7273
"""
7374
eduTEAMS error
7475
"""
7576
def __init__(self, error):
7677
self.error = error
7778

79+
7880
class SATOSAMissingStateError(SATOSABasicError):
7981
"""
8082
SATOSA Missing State error.
@@ -85,6 +87,7 @@ class SATOSAMissingStateError(SATOSABasicError):
8587
"""
8688
pass
8789

90+
8891
class SATOSAAuthenticationFlowError(SATOSABasicError):
8992
"""
9093
SATOSA Flow error.
@@ -95,10 +98,25 @@ class SATOSAAuthenticationFlowError(SATOSABasicError):
9598
"""
9699
pass
97100

101+
98102
class SATOSABadRequestError(SATOSABasicError):
99103
"""
100104
SATOSA Bad Request error.
101105
102106
This exception should be raised when we want to return an HTTP 400 Bad Request
103107
"""
104108
pass
109+
110+
111+
class SATOSABadContextError(SATOSAError):
112+
"""
113+
Raise this exception if validating the Context and failing.
114+
"""
115+
pass
116+
117+
118+
class SATOSANoBoundEndpointError(SATOSAError):
119+
"""
120+
Raised when a given url path is not bound to any endpoint function
121+
"""
122+
pass

src/satosa/routing.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import logging
55
import re
66

7-
from satosa.context import SATOSABadContextError
8-
from satosa.exception import SATOSAError
7+
from satosa.exception import SATOSABadContextError
8+
from satosa.exception import SATOSANoBoundEndpointError
99

1010
import satosa.logging_util as lu
1111

@@ -15,20 +15,6 @@
1515
STATE_KEY = "ROUTER"
1616

1717

18-
class SATOSANoBoundEndpointError(SATOSAError):
19-
"""
20-
Raised when a given url path is not bound to any endpoint function
21-
"""
22-
pass
23-
24-
25-
class SATOSAUnknownTargetBackend(SATOSAError):
26-
"""
27-
Raised when targeting an unknown backend
28-
"""
29-
pass
30-
31-
3218
class ModuleRouter(object):
3319
class UnknownEndpoint(ValueError):
3420
pass

0 commit comments

Comments
 (0)