From 344115f97e88ac502ce9eebea209e09a6c145797 Mon Sep 17 00:00:00 2001 From: MustafaMunir123 Date: Mon, 25 Dec 2023 13:23:16 +0500 Subject: [PATCH] chore: make error handling better --- apps/transactions/api/v1/views.py | 11 +++---- .../migrations/0025_auto_20231223_2204.py | 29 ++++++++++++++++++ apps/transactions/models.py | 6 ++-- apps/utils.py | 6 ++-- db.sqlite3 | Bin 217088 -> 217088 bytes sql commads.txt | 2 ++ 6 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 apps/transactions/migrations/0025_auto_20231223_2204.py create mode 100644 sql commads.txt diff --git a/apps/transactions/api/v1/views.py b/apps/transactions/api/v1/views.py index b83acab..918580f 100644 --- a/apps/transactions/api/v1/views.py +++ b/apps/transactions/api/v1/views.py @@ -14,7 +14,7 @@ from apps.transactions.api.v1.services import ExportServices, LedgerServices, TransactionServices from apps.transactions.models import Account, Currency, Transaction from apps.transactions.permissions import OnlyAdmin, TransactionPermission -from apps.utils import success_response +from apps.utils import error_response, success_response class AccountAPIView(APIView): @@ -28,13 +28,11 @@ def get_serializer(): def post(self, request): try: user = request.user - print("Boss") serializer = self.get_serializer() serializer = serializer(data=request.data) - print("Toss") - serializer.is_valid(raise_exception=True) + if not serializer.is_valid(): + return error_response(error_msg=serializer.errors) serializer.save(user=user) - print("Loss") return success_response(data=serializer.validated_data, status=status.HTTP_200_OK) except Exception as ex: raise ex @@ -86,7 +84,8 @@ def post(self, request): from_account_instance = Account.objects.get(id=from_account) serializer = self.get_serializer() serializer = serializer(data=request.data) - serializer.is_valid(raise_exception=True) + if not serializer.is_valid(): + return error_response(error_msg=serializer.errors) serializer.save(to_account=to_account_instance, from_account=from_account_instance) serializer.validated_data["to_account"] = to_account_instance.title serializer.validated_data["from_account"] = from_account_instance.title diff --git a/apps/transactions/migrations/0025_auto_20231223_2204.py b/apps/transactions/migrations/0025_auto_20231223_2204.py new file mode 100644 index 0000000..da317fd --- /dev/null +++ b/apps/transactions/migrations/0025_auto_20231223_2204.py @@ -0,0 +1,29 @@ +# Generated by Django 3.2.6 on 2023-12-23 17:04 + +from django.db import migrations, models +import phonenumber_field.modelfields + + +class Migration(migrations.Migration): + + dependencies = [ + ('transactions', '0024_account_authorize'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='address', + field=models.CharField(blank=True, max_length=300, null=True), + ), + migrations.AlterField( + model_name='account', + name='company_name', + field=models.CharField(blank=True, max_length=200, null=True), + ), + migrations.AlterField( + model_name='account', + name='mobile_2', + field=phonenumber_field.modelfields.PhoneNumberField(blank=True, max_length=128, null=True, region=None, unique=True), + ), + ] diff --git a/apps/transactions/models.py b/apps/transactions/models.py index bcad26c..b64f865 100644 --- a/apps/transactions/models.py +++ b/apps/transactions/models.py @@ -31,9 +31,9 @@ class Account(models.Model): title = models.CharField(max_length=100, null=False, blank=False, unique=True) note = models.TextField(max_length=200, null=False) mobile_1 = PhoneNumberField(null=False, unique=True) - mobile_2 = PhoneNumberField(null=True, unique=True) - address = models.CharField(max_length=300, null=True) - company_name = models.CharField(max_length=200, null=True) + mobile_2 = PhoneNumberField(null=True, unique=True, blank=True) + address = models.CharField(max_length=300, null=True, blank=True) + company_name = models.CharField(max_length=200, null=True, blank=True) authorize = models.BooleanField(default=True, null=False, blank=False) def __str__(self): diff --git a/apps/utils.py b/apps/utils.py index 466e79f..d65a2d4 100644 --- a/apps/utils.py +++ b/apps/utils.py @@ -17,8 +17,9 @@ def success_response(status, data, success=True): def custom_exception_handler(exc, context): response = exception_handler(exc, context) + print(exc) if response is not None: - response.data = error_response(exc.args) + response.data = error_response(str(exc)) return response.data elif ( isinstance(exc, FieldError) @@ -26,7 +27,6 @@ def custom_exception_handler(exc, context): or isinstance(exc, TypeError) or isinstance(exc, AssertionError) or isinstance(exc, OperationalError) - or isinstance(exc, PermissionError) ): response = error_response(str(exc)) elif isinstance(exc, IntegrityError): @@ -35,6 +35,8 @@ def custom_exception_handler(exc, context): response = error_response(str(exc)) elif isinstance(exc, KeyError): response = error_response("Invalid Parameters") + else: + response = response return response diff --git a/db.sqlite3 b/db.sqlite3 index 14aeeb42cd4ad3ec23d9ae2988e365a3bfc29fb5..c46356699cb35d0f074e2c2bfd81e7ee6d52db5e 100644 GIT binary patch delta 2093 zcmah~U2GIp6yBNH?XYcUrhtmJRfkp#0Xx0-&YiynLbvS(N-0&O&}yK}?(E-ecem|s z*KG;e2Ia{FK_2D>6B0;>rcq3RO*BS{j~asdU^N@3?bOf#|Bv4(%>?oJT!ZQ2$tj+?oH z87rpqxdH%488#=1`C$Z*3=xut5dzr#(*{u+l0~S601iQ2lCf&&aQ4!Ae>0UmJJY_2 z0<&MX8#k%%uhvsv2(3P!uXU?|WDF35P&0J$z_-?WA|t4g-0)fhc?jQnS4unhT;;1>Qs|G0Lqp^v+RWdSByQGhg<|fs2-f~Vi z(X*BD-3RncXkah}GW|VjF`H96`_X|zMgsTB6GdGfP>q41Tq=UXlWl{201uH~;x+)= zfF?pcgyfKdC9Ehah9_F+$m`Ixzh@x^?FKKo`m*RWM2kls- z|CMyuIoCvAA|_O=HxwW4o|uY2B@%(jeO;LzIXwo3)G50&o3RlZG+&DMycpgKx+24| zMH8xfp|I|nfI!y_`9y22XF|!?=6EVS3MZn)T+W^v8Pia-j7EwXc|3~C@(L5Z?WPs%U7OZ`lyF>soo8GSVimH zpog8f)(eWK@`;RIh?-HWdPkz0DB^O)C;T9MDqIv!3JGDCuvrN5clpow8RGI3fA}dT zOfA-XMbqn-yk60cx_hq|#(zbjYOBGWG)+-|RO>tFJ3=iP@wys!&;!+f`{~*S_LxxR zJNORb1n||49KDTNL*AbBDZ41=Mvm5;=^TyMdE3BU4G3ru%9yN%QTl`YI)op%qgYdA zwfgxjdWXpVEc`0W2;_jH=LBa)V7B{7*2f+5WBL)6BR^aG7aX>M+1khsvKwmd)Lg46 z*X*td2Ic}zzzS^f&-*VBONac=4rPPBuu$Nrpj)GbQz{}LcL9U|lym?TtU)ia)BZKJ zv9e{vB|N%>u;7wxMMF|F1$i0rGml5F0*xs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