Skip to content

Commit

Permalink
adding error handleing
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisDDBT committed Feb 19, 2025
1 parent 2502f01 commit 6a203d1
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions payroll/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

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

return JsonResponse({})



class EmployeeNotesApi(EditPayrollBaseView):
def post(self, request, *args, **kwargs):
data = json.loads(request.body)
notes = data["notes"]
employee_no = data["employee_no"]
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),
None,
)
if employee:
payroll_service.update_employee_notes(
notes,
employee_no,
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')

if not notes or not employee_no:
return JsonResponse(
{"error": "Both 'notes' and 'employee_no' are required"},
status=400
)
employee_data = payroll_service.get_employee_data(
self.cost_centre,
self.financial_year,
)
return JsonResponse({})
employee = next(
(item for item in employee_data if str(item["employee_no"]) == employee_no),
None,
)
if employee:
payroll_service.update_employee_notes(
notes,
employee_no,
self.cost_centre,
self.financial_year,
)
return JsonResponse({}, status=204)
except json.JSONDecodeError:
return JsonResponse(
{"error": "Invalid JSON format"},
status=400
)
except ValidationError as e:
return JsonResponse(
{"error": str(e)},

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
status=400
)
except Exception:
return JsonResponse(
{"error": "An error occurred while processing the request"},
status=500
)

0 comments on commit 6a203d1

Please sign in to comment.