Repository errors
Add the error definitions to the website/repository.go file and let’s analyze them:
- An
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 -Create()orUpdate()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 theGetByName()method. - An
ErrUpdateFailederror is an error returned by theUpdate()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-existentid. - An
ErrDeleteFailederror should occur when no row was deleted as a result of executing theDelete()method, e.g., because there is no row with the givenid.
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.
website/repository.go
| |