From 653f6ae08c74c137b6044e1ae70810138d39660d Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 29 Oct 2020 00:23:07 +0100 Subject: use ErrorCode enum from GANA instead of hard-coded constants --- setup.py | 2 +- talerbank/app/middleware.py | 3 ++- talerbank/app/schemas.py | 9 +++++---- talerbank/app/views.py | 24 ++++++++++++++---------- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index 8b82b8c..8485cce 100755 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ setup(name='talerbank', "jinja2", "qrcode", "lxml", - "taler-util>=0.6.4"], + "taler-util>=0.8.0"], scripts=["./bin/taler-bank-manage"], package_data={ 'talerbank.app': [ diff --git a/talerbank/app/middleware.py b/talerbank/app/middleware.py index 08d8979..a2fc586 100644 --- a/talerbank/app/middleware.py +++ b/talerbank/app/middleware.py @@ -17,6 +17,7 @@ from .views import ( from .schemas import JSONFieldException, URLParamValidationError, InvalidSession from taler.util.amount import CurrencyMismatchError, AmountFormatError +from taler.util.taler_error_codes import ErrorCode LOGGER = logging.getLogger() @@ -110,7 +111,7 @@ class ExceptionMiddleware: return redirect(render_to) else: return JsonResponse( - dict(code=5300, + dict(code=ErrorCode.BANK_UNMANAGED_EXCEPTION, hint="unexpected exception", exception=str(exception)), status=500) diff --git a/talerbank/app/schemas.py b/talerbank/app/schemas.py index 6c08acf..2ff7892 100644 --- a/talerbank/app/schemas.py +++ b/talerbank/app/schemas.py @@ -1,6 +1,6 @@ ## # This file is part of TALER -# (C) 2014, 2015, 2016 Taler Systems SA +# (C) 2014, 2015, 2016, 2020 Taler Systems SA # # TALER is free software; you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -25,6 +25,7 @@ from django.core.exceptions import ValidationError from django import forms from django.core.validators import RegexValidator from urllib.parse import urlparse +from taler.util.taler_error_codes import ErrorCode ## # Constant value for the biggest number the bank handles. @@ -59,7 +60,7 @@ class InternalServerError(Exception): def __init__(self, hint): self.hint = hint self.http_status_code = 500 - self.taler_error_code = 1011 # TALER_EC_INTERNAL_LOGIC_ERROR + self.taler_error_code = ErrorCode.INTERNAL_LOGIC_ERROR ## @@ -82,7 +83,7 @@ class JSONFieldException(ValueError): super(JSONFieldException, self).__init__(line) self.hint = line self.http_status_code = http_status_code - self.taler_error_code = 5106 + self.taler_error_code = TalerErrorCode.BANK_JSON_INVALID ## @@ -99,7 +100,7 @@ class URLParamValidationError(ValueError): def __init__(self, error, http_status_code): self.hint = json.stringify(error.as_json()) self.http_status_code = http_status_code - self.taler_error_code = 5105 + self.taler_error_code = ErrorCode.BANK_PARAMETER_MISSING_OR_INVALID super().__init__() diff --git a/talerbank/app/views.py b/talerbank/app/views.py index 853b6ae..94e9998 100644 --- a/talerbank/app/views.py +++ b/talerbank/app/views.py @@ -1,6 +1,6 @@ ## # This file is part of TALER -# (C) 2014, 2015, 2016 Taler Systems SA +# (C) 2014, 2015, 2016, 2020 Taler Systems SA # # TALER is free software; you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License as @@ -49,6 +49,7 @@ from django.core.exceptions import ObjectDoesNotExist from datetime import datetime from .models import BankAccount, BankTransaction, TalerWithdrawOperation from taler.util.amount import Amount, SignedAmount +from taler.util.taler_error_codes import ErrorCode import qrcode import qrcode.image.svg import lxml @@ -115,7 +116,7 @@ class LoginFailed(Exception): super(LoginFailed, self).__init__(msg) self.hint = "Wrong password given" self.http_status_code = 401 - self.taler_error_code = 5109 + self.taler_error_code = ErrorCode.BANK_LOGIN_FAILED class InvalidInputData(Exception): @@ -123,7 +124,7 @@ class InvalidInputData(Exception): super(InvalidInputData, self).__init__(msg) self.hint = msg # should mention the picked username self.http_status_code = 400 - self.taler_error_code = 5400 + self.taler_error_code = ErrorCode.BANK_SOFT_EXCEPTION class UsernameUnavailable(Exception): @@ -131,7 +132,7 @@ class UsernameUnavailable(Exception): super(UsernameUnavailable, self).__init__(msg) self.hint = msg # should mention the picked username self.http_status_code = 406 - self.taler_error_code = 5400 + self.taler_error_code = ErrorCode.BANK_SOFT_EXCEPTION ## @@ -152,7 +153,7 @@ class DebitLimitException(Exception): super(DebitLimitException, self).__init__(msg) self.hint = "Payment aborted for insufficient credit" self.http_status_code = 406 - self.taler_error_code = 5103 + self.taler_error_code = ErrorCode.BANK_UNALLOWED_DEBIT ## @@ -164,7 +165,7 @@ class SameAccountException(Exception): super(SameAccountException, self).__init__(msg) self.hint = "Cannot send payment to oneself." self.http_status_code = 403 - self.taler_error_code = 5102 + self.taler_error_code = ErrorCode.BANK_SAME_ACCOUNT class UnhandledException(Exception): @@ -172,7 +173,7 @@ class UnhandledException(Exception): super(UnhandledException, self).__init__(msg) self.hint = msg self.http_status_code = 500 - self.taler_error_code = 5300 + self.taler_error_code = ErrorCode.BANK_UNMANAGED_EXCEPTION ## @@ -895,7 +896,8 @@ def twg_transfer(request, user_account, acct_id): LOGGER.error(f"credit account '{credit_account_name}' does not exist") # FIXME: use EC from taler-util library return JsonResponse( - dict(code=5110, error="credit account does not exist"), status=404 + dict(code=ErrorCode.BANK_UNKNOWN_ACCOUNT, + error="credit account does not exist"), status=404 ) credit_account = BankAccount.objects.get(user=credit_user) @@ -1083,7 +1085,8 @@ def api_withdraw_operation(request, withdraw_id): exchange_user = User.objects.get(username=exchange_account_name) except User.DoesNotExist: return JsonResponse( - dict(code=5110, hint="bank account in payto URI unknown"), status=400 + dict(code=ErrorCode.BANK_UNKNOWN_ACCOUNT, + hint="bank account in payto URI unknown"), status=400 ) exchange_account = exchange_user.bankaccount selected_reserve_pub = data.get("reserve_pub") @@ -1096,7 +1099,8 @@ def api_withdraw_operation(request, withdraw_id): ): return JsonResponse( dict( - code=5600, hint="selection of withdraw parameters already done" + code=ErrorCode.BANK_WITHDRAWAL_OPERATION_RESERVE_SELECTION_CONFLICT, + hint="selection of withdraw parameters already done" ), status=409, ) -- cgit v1.2.3