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

Differenciate while querying for data from nonexistent resultsets and errors with Bun #876

Open
x80486 opened this issue Aug 1, 2023 · 0 comments

Comments

@x80486
Copy link

x80486 commented Aug 1, 2023

I'm evaluating Bun for a new project. I started to model some queries just to found out that usually they return the same "empty" struct, regardless of the underlying failure.

For instance, consider something like the following SELECT:

ctx := context.Background()
category := new(model.Category)
db.NewSelect().Model(category).Where("id = ?", id).Scan(ctx)

When checking for the returned error, the only way to figure it out (that I know of) what type of failure happened is by analyzing/parsing the returned string value in the error type.

# Failure because nothing matched
[bun]  13:30:38.945   SELECT                7.532ms  SELECT "category"."id", "category"."name", "category"."description", "category"."picture" FROM "categories" AS "category" WHERE (id = 'f9f0e038-ba40-4553-9fb6-a41ec2a892fb')        *errors.errorString: sql: no rows in result set 
2023/08/01 13:30:38 Category = [&{{} 00000000-0000-0000-0000-000000000000   }]
2023/08/01 13:30:38 [sql: no rows in result set]
13:30:38 | 404 |     1.4ms |       127.0.0.1 | GET     | /categories/f9f0e038-ba40-4553-9fb6-a41ec2a892fb

# Failure because the database server is unreachable
2023/08/01 13:39:59 Searching for category with ID [5c5b2317-a88f-45f1-9c69-bfbe74cef628]
"category"."description", "category"."picture" FROM "categories" AS "category" WHERE (id = '5c5b2317-a88f-45f1-9c69-bfbe74cef628')        *net.OpError: dial tcp [::1]:5432: connect: connection refused 
2023/08/01 13:39:59 Category = [&{{} 00000000-0000-0000-0000-000000000000   }]
2023/08/01 13:39:59 [dial tcp [::1]:5432: connect: connection refused]
13:39:59 | 404 |     1ms |       127.0.0.1 | GET     | /categories/5c5b2317-a88f-45f1-9c69-bfbe74cef628

Basically, I'm trying to find a way to return the correct HTTP response based on the actions executed, so I'm not sure if Bun could return nil for those cases where nothing was found from the database. Not sure either what's the best outcome here, and if it's possible to do that currently.

Obligatory Stackoverflow question 😇

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

1 participant