bun
bun copied to clipboard
Differenciate while querying for data from nonexistent resultsets and errors with Bun
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 :innocent: