amplication icon indicating copy to clipboard operation
amplication copied to clipboard

šŸ› Bug Report: internal server error while trying to create a user with a username that already exisits

Open abrl91 opened this issue 2 years ago ā€¢ 2 comments

What happened?

on create user, when I try to create a user with a username that already exists, I get an internal server error

What you expected to happen

I expect to get something like 409 and a message that a user with the requested username already exists https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses

image

How to reproduce

generate an app and make a post request to create a user

Amplication version

0.12.8

Environment

No response

Are you willing to submit PR?

Yes I am willing to submit a PR!

abrl91 avatar May 28 '22 07:05 abrl91

we can add a custom validation decorator and use it on the username property https://github.com/typestack/class-validator#custom-validation-decorators image (a screenshot from a generated app - User.ts)

abrl91 avatar May 28 '22 14:05 abrl91

@abrl91 I think a decorator would not be the best solution for this problem. It feels that a local validation in the service code before creating the user would be more appropriate. It is part of the logic of the function

yuval-hazaz avatar May 29 '22 14:05 yuval-hazaz

Can i be assigned to this please :) ?

shubhwip avatar Aug 17 '22 08:08 shubhwip

Can i be assigned to this please :) ?

sure! @shubhwip

Join our discord if you need any help :)

souravjain540 avatar Aug 17 '22 08:08 souravjain540

@yuval-hazazĀ @abrl91Ā I have come up with below approaches ā€” Adding a new error type in errors.ts forĀ ConflictException Adding validation inĀ CREATE_ENTITY_FUNCTIONĀ inĀ controller.base.template.ts * Validation would be to put a check on finding resource based on unique specified attributes of model before creating new resource. * If we find any resource matching one of the unique define field of model, we will throw conflict exception. ā€” Or if we don't use amplication data generator forĀ userĀ then we can put validation inĀ user serviceĀ directly but i think we are using amplication data generator forĀ user model and it would be nice to put error handling for resource creation in case of errors. ā€” Or Since prisma client can throw unique constraint violation error and we can catch and wrap it inside CREATE_ENTITY_FUNCTION up as 400 BAD Request. https://www.prisma.io/docs/reference/api-reference/error-reference#p2002

shubhwip avatar Aug 18 '22 09:08 shubhwip

@yuval-hazaz @abrl91 please reply to him.

souravjain540 avatar Aug 20 '22 15:08 souravjain540

https://www.prisma.io/docs/reference/api-reference/error-reference#p2002

I think the last solution is preferable, but with 409 conflict HTTP error code

abrl91 avatar Aug 20 '22 20:08 abrl91