diff options
Diffstat (limited to 'talerbank/app/middleware.py')
-rw-r--r-- | talerbank/app/middleware.py | 52 |
1 files changed, 12 insertions, 40 deletions
diff --git a/talerbank/app/middleware.py b/talerbank/app/middleware.py index a0d8f40..21c747f 100644 --- a/talerbank/app/middleware.py +++ b/talerbank/app/middleware.py @@ -1,12 +1,14 @@ import logging import zlib +from . import urls from django.http import JsonResponse +from django.urls import reverse from django.shortcuts import redirect from .models import BankAccount, BankTransaction, \ BankAccountDoesNotExist, BankTransactionDoesNotExist from .views import \ (DebitLimitException, SameAccountException, - LoginFailed, RejectNoRightsException) + LoginFailed, RejectNoRightsException, UnhandledException) from .schemas import \ (JSONFieldException, @@ -68,31 +70,12 @@ class ExceptionMiddleware: def __init__(self, get_response): self.get_response = get_response - # List of all the exceptions that are managed by - # this module. - self.excs = { - BankAccountDoesNotExist: 5110, - BankTransactionDoesNotExist: 5111, - SameAccountException: 5102, - URLParamValidationError: 5105, - JSONFieldException: 5106, - CurrencyMismatch: 5104, - BadFormatAmount: 11, - LoginFailed: 5312, - NumberTooBig: 5108, - NegativeNumber: 5107, - DebitLimitException: 5103, - RejectNoRightsException: 5200, - } - # Map between endpoints and Web pages to render # after the exception gets managed. self.render = { - "/profile": "profile", - "/accounts/register": "index", - "/public-accounts": "index", - "/pin/verify": "profile", - "/withdraw": "profile" + reverse("profile", urlconf=urls): "profile", + reverse("register", urlconf=urls): "index", + reverse("public-accounts", urlconf=urls): "index", } ## @@ -117,28 +100,17 @@ class ExceptionMiddleware: # @param exception the exception raised from the bank. def process_exception(self, request, exception): LOGGER.warning(str(exception)) - # See if we manage this exception. Return None if not. - exc_class = None - for e in self.excs: - if isinstance(exception, e): - exc_class = e - break - if not exc_class: - return None - - # Managed exception. Build response. - taler_ec = self.excs.get(exc_class) - - # Check if the endpoint should cause a human-readable - # page to be returned. + + if not hasattr(exception, "taler_error_code"): + exception = UnhandledException() + render_to = self.render.get(request.path) if not render_to: - return JsonResponse({"ec": taler_ec, + return JsonResponse({"ec": exception.taler_error_code, "error": exception.hint}, status=exception.http_status_code) - request.session["profile_hint"] = \ - True, False, exception.hint + request.session["profile_hint"] = True, False, exception.hint return redirect(render_to) # [1] https://git.taler.net/exchange.git/tree/src/include/taler_error_codes.h |