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 — without parsing string values. Currently an HTTP 404
is returned, but for those cases where a system failure is expected, an HTTP 500
is more appropriate, rather than stating there is no resource available.
So I'm not sure if Bun
could return nil
for those cases where nothing was found from the database. Not sure what's the best outcome here, and if it's possible to do that currently.
Any suggestions with this? As you can tell, I'm fairly new in the Go
world ;)
UPDATES
While checking for all those errors might be something that provides more control, I really think this is "a problem" with the underlying PostgreSQL driver. I've been testing some other libraries and they fail, as expected, for the use cases I was testing (server down, saturated pool, etc.), while returning the correct datasets whenever the requested data was present or not.