Handling duplicate users

Since we set our email field to be unique, our database already prevents duplicate users. However, if you were to try this for yourself, the output we receive back is not the best. Try it for yourself by adding the same user again in the Nameko shell.

Another problem with this is that, if there were any other errors when creating a new user, there is no nice way for our external services to react to these different types of errors without knowing the type of database we are using, which we want to avoid at all costs.

To solve this, let's start by creating two new exception classes in our users.py:

class CreateUserError(Exception): 
    pass 
 
class UserAlreadyExists(CreateUserError): 
    pass 

We also need to update our imports ...

Get Python Programming Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.