Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency on responses #1318

Open
AndreMPCosta opened this issue Nov 7, 2023 · 2 comments
Open

Inconsistency on responses #1318

AndreMPCosta opened this issue Nov 7, 2023 · 2 comments

Comments

@AndreMPCosta
Copy link

AndreMPCosta commented Nov 7, 2023

For example on the register endpoint:

responses={
            status.HTTP_400_BAD_REQUEST: {
                "model": ErrorModel,
                "content": {
                    "application/json": {
                        "examples": {
                            ErrorCode.REGISTER_USER_ALREADY_EXISTS: {
                                "summary": "A user with this email already exists.",
                                "value": {
                                    "detail": ErrorCode.REGISTER_USER_ALREADY_EXISTS
                                },
                            },
                            ErrorCode.REGISTER_INVALID_PASSWORD: {
                                "summary": "Password validation failed.",
                                "value": {
                                    "detail": {
                                        "code": ErrorCode.REGISTER_INVALID_PASSWORD,
                                        "reason": "Password should be"
                                        "at least 3 characters",
                                    }
                                },
                            },
                        }
                    }
                },
            },
        },
async def register(
        request: Request,
        user_create: user_create_schema,  # type: ignore
        user_manager: BaseUserManager[models.UP, models.ID] = Depends(get_user_manager),
    ):
        try:
            created_user = await user_manager.create(
                user_create, safe=True, request=request
            )
        except exceptions.UserAlreadyExists:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ErrorCode.REGISTER_USER_ALREADY_EXISTS,
            )
        except exceptions.InvalidPasswordException as e:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail={
                    "code": ErrorCode.REGISTER_INVALID_PASSWORD,
                    "reason": e.reason,
                },
            )

Why do we have a detail which is a string, and a detail which is an object?

Consuming this on the frontend is harder because this is not consistent.

@hgalytoby
Copy link
Contributor

I've reviewed the entire 'router API,' and it appears that the only difference is the way password length is returned.

Regarding the password issue, I believe that the front-end should check the password length before sending it to the back-end. This way, the front-end won't encounter password length errors.

@frankie567
Copy link
Member

Actually, the reason field could take whatever value, since you can implement custom password validation rules. So it might not be only a matter of password length.

I agree that it's not very good that it's the only place that breaks the convention for returning error. The right way would be to always nest the error code inside an object, so it gives us flexibility to implement specific things like password validation while still maintaining a common schema.

It would be a breaking change though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants