Skip to content

Commit

Permalink
chore: make error handling better
Browse files Browse the repository at this point in the history
  • Loading branch information
MustafaMunir123 committed Dec 25, 2023
1 parent 899905b commit 344115f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
11 changes: 5 additions & 6 deletions apps/transactions/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions apps/transactions/migrations/0025_auto_20231223_2204.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
6 changes: 3 additions & 3 deletions apps/transactions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions apps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ 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)
or isinstance(exc, AttributeError)
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):
Expand All @@ -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

Expand Down
Binary file modified db.sqlite3
Binary file not shown.
2 changes: 2 additions & 0 deletions sql commads.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sqlite
UPDATE sqlite_sequence SET seq = 1 WHERE name = 'transactions_transaction';

0 comments on commit 344115f

Please sign in to comment.