Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: More qrm error constants #109

Closed
CmpHDL opened this issue Dec 10, 2021 · 6 comments
Closed

Feature Request: More qrm error constants #109

CmpHDL opened this issue Dec 10, 2021 · 6 comments

Comments

@CmpHDL
Copy link

CmpHDL commented Dec 10, 2021

We have a qrm.ErrNoRows, perhaps we should have more of these common errors as constants, like violating constraints.
qrm.ErrDuplicate for duplicate key value violates unique constraint, as an example.

If anyone runs into or can think of any more that are common usecase, add them to this thread!
If theres an existing way to accomplish this, let me know!

@CmpHDL
Copy link
Author

CmpHDL commented Dec 12, 2021

@go-jet If you can show me how to correctly implement something like this within the lib I don't mind adding more.

@go-jet
Copy link
Owner

go-jet commented Dec 12, 2021

What do you mean by duplicate key value violates unique constraint?

@CmpHDL
Copy link
Author

CmpHDL commented Dec 12, 2021

What do you mean by duplicate key value violates unique constraint?

On databases you can put constraints to help maintain correct data and in some cases optimize searches.

ALTER TABLE users
ADD UNIQUE (username);

Would add a constraint so that any ticket name would have to be unique.
If you do
INSERT INTO users (username) VALUES ('go-jet');
The query will work. If you try it again
INSERT INTO users (username) VALUES ('go-jet');
You will get a constraints error as a user with username 'go-jet' already exists.

@CmpHDL
Copy link
Author

CmpHDL commented Dec 12, 2021

Here is an overview of constraints from the documentation.
https://www.postgresql.org/docs/9.4/ddl-constraints.html

@go-jet
Copy link
Owner

go-jet commented Dec 13, 2021

SQL driver is responsible for those kind of errors. Query() and Exec() methods will just forward driver errors, if there any.
Now, we could intercept driver errors, and convert those to qrm Error, but this would tightly couple jet with all existing golang database drivers. Because each database driver implements its own type of errors. Every time a new driver appears we will have to update error handling, as well as if some of the existing driver changes.
Depending of the driver, maybe, you can open a issue on a driver repo to add those errors, or you can add a check in your code:

func UniqueViolation(err error) bool {
	var pgErr *pgconn.PgError   //asuming pgx driver is used
	if errors.As(err, &pgErr) {
		if pgErr.Code == "23505" {
			return true
		}
	}
        return false
}

Or for pq driver, this library might be useful: https://github.com/omeid/pgerror

@CmpHDL
Copy link
Author

CmpHDL commented Dec 13, 2021

Because each database driver implements its own type of errors. Every time a new driver appears we will have to update error handling, as well as if some of the existing driver changes.
Depending of the driver, maybe, you can open a issue on a driver repo to add those errors, or you can add a check in your code:

This is great information. Love the way you think @go-jet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants