Skip to content

Commit

Permalink
fixing error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisDDBT committed Feb 19, 2025
1 parent 6a203d1 commit 78470e9
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions payroll/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json

from django.conf import settings
from django.http import JsonResponse
from django.core.exceptions import ValidationError
from django.http import JsonResponse

from core.utils.generic_helpers import get_previous_months_data
from payroll.views import EditPayrollBaseView
Expand Down Expand Up @@ -88,30 +88,31 @@ def post(self, request, *args, **kwargs):
)

return JsonResponse({})



class EmployeeNotesApi(EditPayrollBaseView):
def post(self, request, *args, **kwargs):
try:
data = json.loads(request.body)
if not data:
return JsonResponse(
{"error": "Missing request body"},
status=400
)
notes = data.get('notes')
employee_no = data.get('employee_no')
return JsonResponse({"error": "Missing request body"}, status=400)
notes = data.get("notes")
employee_no = data.get("employee_no")

if not notes or not employee_no:
return JsonResponse(
{"error": "Both 'notes' and 'employee_no' are required"},
status=400
{"error": "Both 'notes' and 'employee_no' are required"}, status=400
)
employee_data = payroll_service.get_employee_data(
self.cost_centre,
self.financial_year,
)
employee = next(
(item for item in employee_data if str(item["employee_no"]) == employee_no),
(
item
for item in employee_data
if str(item["employee_no"]) == employee_no
),
None,
)
if employee:
Expand All @@ -123,17 +124,10 @@ def post(self, request, *args, **kwargs):
)
return JsonResponse({}, status=204)
except json.JSONDecodeError:
return JsonResponse(
{"error": "Invalid JSON format"},
status=400
)
except ValidationError as e:
return JsonResponse(
{"error": str(e)},
status=400
)
return JsonResponse({"error": "Invalid JSON format"}, status=400)
except ValidationError:
return JsonResponse({"error": "Invalid data provided"}, status=400)
except Exception:
return JsonResponse(
{"error": "An error occurred while processing the request"},
status=500
)
{"error": "An error occurred while processing the request"}, status=500
)

0 comments on commit 78470e9

Please sign in to comment.