Nuevos cambios para config del repo y creacion del servicio de creacion comercial
This commit is contained in:
@@ -2,15 +2,55 @@ from fastapi.responses import JSONResponse
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi import Request
|
||||
from Back_comercial_iko.core.HttpStatus import HttpStatus
|
||||
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||
|
||||
|
||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||
|
||||
print(f"Error de validación: {exc}")
|
||||
return JSONResponse(
|
||||
status_code=HttpStatus.BAD_REQUEST,
|
||||
content={
|
||||
"success": False,
|
||||
"message": "Formato de request inválido",
|
||||
"error_code": "INVALID_JSON"
|
||||
"error_code": "INVALID_JSON",
|
||||
"status_code": HttpStatus.BAD_REQUEST,
|
||||
"data": None
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def db_error_response(e: Exception):
|
||||
"""Convert any error to a friendly response"""
|
||||
|
||||
response = {
|
||||
"success": False,
|
||||
"message": "Error interno del servidor",
|
||||
"error_code": "INTERNAL_ERROR",
|
||||
"status_code": HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
"data": None
|
||||
}
|
||||
|
||||
if isinstance(e, IntegrityError):
|
||||
error_msg = str(e.orig) if e.orig else str(e)
|
||||
response["status_code"] = HttpStatus.CONFLICT
|
||||
print(f"Error en la base de datos: {error_msg}")
|
||||
|
||||
if "Duplicate entry" in error_msg:
|
||||
response["message"] = "Ya existe un registro con estos datos"
|
||||
response["error_code"] = "DUPLICATE_ENTRY"
|
||||
elif "foreign key constraint fails" in error_msg:
|
||||
response["message"] = "Referencia a un registro que no existe"
|
||||
response["error_code"] = "FOREIGN_KEY_ERROR"
|
||||
elif "cannot be null" in error_msg:
|
||||
response["message"] = "Faltan campos requeridos"
|
||||
response["error_code"] = "REQUIRED_FIELD_NULL"
|
||||
else:
|
||||
response["message"] = "Error de integridad de datos"
|
||||
response["error_code"] = "INTEGRITY_ERROR"
|
||||
|
||||
elif isinstance(e, SQLAlchemyError):
|
||||
response["message"] = "Error en la base de datos"
|
||||
response["error_code"] = "DATABASE_ERROR"
|
||||
response["status_code"] = HttpStatus.INTERNAL_SERVER_ERROR
|
||||
|
||||
return response
|
||||
14
Back_comercial_iko/core/MessageException.py
Normal file
14
Back_comercial_iko/core/MessageException.py
Normal file
@@ -0,0 +1,14 @@
|
||||
class MessageException(Exception):
|
||||
def __init__(self, societe_name=None, extra_msg=None):
|
||||
base_message = ""
|
||||
if societe_name:
|
||||
base_message += f": '{societe_name}'"
|
||||
if extra_msg:
|
||||
base_message += f"{extra_msg}"
|
||||
|
||||
self.message = base_message
|
||||
super().__init__(self.message)
|
||||
|
||||
@staticmethod
|
||||
def raise_it(societe_name=None, extra_msg=None):
|
||||
raise MessageException(societe_name, extra_msg)
|
||||
@@ -1,10 +0,0 @@
|
||||
class SocieteNotFoundException(Exception):
|
||||
def __init__(self, societe_name=None, extra_msg=None):
|
||||
base_message = "Societe not found"
|
||||
if societe_name:
|
||||
base_message += f": '{societe_name}'"
|
||||
if extra_msg:
|
||||
base_message += f" - {extra_msg}"
|
||||
|
||||
self.message = base_message
|
||||
super().__init__(self.message)
|
||||
Reference in New Issue
Block a user