Clean: Error messages

This commit is contained in:
Omma 2024-08-21 17:01:39 +03:30
parent 7d23c4e3b0
commit 7ef4bda603

View file

@ -41,8 +41,7 @@ response = requests.post('http://<well_service_url>/<...>/', headers=headers, js
#error_list #error_list
''' '''
{ {
"200_created": ({"data": "<serializer.data>", "message": "<costume message>"}, status=status.HTTP_200_OK), "200": ({"data": "<serializer.data>", "message": "<costume message>"}, status=status.HTTP_200_OK),
"200_updated": ({"data": "<serializer.data>", "message": f"<Successfully updated {instance.license_code}>"}, status=status.HTTP_200_OK),
"400":({"message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST), "400":({"message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST),
"404": ({"message": "<Not Found>"}, status=status.HTTP_404_NOT_FOUND), "404": ({"message": "<Not Found>"}, status=status.HTTP_404_NOT_FOUND),
"409": ({"message":f'<{data["license_code"]} already exists>'}, status=status.HTTP_409_CONFLICT), "409": ({"message":f'<{data["license_code"]} already exists>'}, status=status.HTTP_409_CONFLICT),
@ -116,7 +115,7 @@ def delete_well_by_well_id(request, obj_id):
instance = Well.objects.get(id=obj_id, log_type=0) instance = Well.objects.get(id=obj_id, log_type=0)
instance.log_type = 1 instance.log_type = 1
instance.save() instance.save()
return Response({"message": f"Successfully removed log type for well {instance.license_code}"}, status=status.HTTP_200_OK) return Response({"message": f"Successfully removed {instance.license_code}"}, status=status.HTTP_200_OK)
except Well.DoesNotExist: except Well.DoesNotExist:
return Response({"message": "Not Found"}, status=status.HTTP_404_NOT_FOUND) return Response({"message": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
except Exception as e: except Exception as e:
@ -136,14 +135,14 @@ def update_well_by_well_id(request, obj_id):
serializer = WellSerializer(data=data) serializer = WellSerializer(data=data)
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK) return Response({"data": serializer.data}, status=status.HTTP_200_OK)
else: else:
return Response({"message":serializer.errors}, status=status.HTTP_400_BAD_REQUEST) return Response({"message":serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
except Well.DoesNotExist: except Well.DoesNotExist:
return Response({"error": "Not Found"}, status=status.HTTP_404_NOT_FOUND) return Response({"message": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
except Exception as e: except Exception as e:
print(e) print(e)
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response({"message": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@permission_classes([IsAuthenticated]) @permission_classes([IsAuthenticated])
@ -159,11 +158,11 @@ def edit_well_by_well_id(request, obj_id):
if serializer.is_valid(): if serializer.is_valid():
serializer["log_of"] = instance.id serializer["log_of"] = instance.id
serializer.save() serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK) return Response({"data": serializer.data}, status=status.HTTP_200_OK)
else: else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response({"message":serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
except Well.DoesNotExist: except Well.DoesNotExist:
return Response({"error": "Not Found"}, status=status.HTTP_404_NOT_FOUND) return Response({"message": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
except Exception as e: except Exception as e:
print(e) print(e)
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response({"message": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)