amplication
amplication copied to clipboard
š Bug Report: internal server error while trying to create a user with a username that already exisits
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
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!
we can add a custom validation decorator and use it on the username property
https://github.com/typestack/class-validator#custom-validation-decorators
(a screenshot from a generated app - User.ts)
@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
Can i be assigned to this please :) ?
Can i be assigned to this please :) ?
sure! @shubhwip
Join our discord if you need any help :)
@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
@yuval-hazaz @abrl91 please reply to him.
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