Add the error definitions to the
website/repository.go file and let’s analyze them:
ErrDuplicateerror means that the user is trying to add a record that already exists, i.e., with the same name, because in our repository, we assume that each website should have a unique name. This error should be returned from the methods that write to the database -
Update()when a violation of the uniqueness constraint is detected.
- An error
ErrNotExistmeans no record with the specified parameters in the database. This should be returned from any functions that pull a single object from the repository. In our case, this is the
ErrUpdateFailederror is an error returned by the
Update()method. It should appear when no row has been affected by the requested update, such as when the change was to a row with a non-existent
ErrDeleteFailederror should occur when no row was deleted as a result of executing the
Delete()method, e.g., because there is no row with the given
Of course, when using the repository, a whole bunch of other errors can also occur, e.g., network errors, connection failures, timeouts, etc. However, these are unexpected errors, and not every application needs to handle them in a special way. But user errors like the above should always be handled so that the user can react and correct bad input.