Stream: bugs

Topic: basic-webserver sqlite constraint violation errors swallowed


view this post on Zulip Isaac Van Doren (Dec 24 2024 at 20:57):

It appears that constraint violations (uniqueness, not null, etc) are not reported as errors in Roc for the basic-webserver sqlite impl. Is this a known issue? I know there was some work on a new implementation so maybe this will be fixed by that work anyway.

With this db

create table test (username text unique);
insert into test (username) values ('isaac');

and this server

app [Model, init!, respond!] {
    pf: platform "platform/main.roc",
}

import pf.Http exposing [Request, Response]
import pf.Sqlite3

Model : {}

init! : {} => Result Model []
init! = \{} ->
    Ok {}

respond! : Request, Model => Result Response _
respond! = \_, _ ->
    _ =
        Sqlite3.execute! {
            path: "bug.db",
            query: "insert into test (username) values ('isaac')",
            bindings: [],
        }
        |> try
    Ok { status: 200, headers: [], body: [] }

Sqlite.execute! returns an Ok even though the insert failed for violating uniqueness constraint.

view this post on Zulip Brendan Hansknecht (Dec 24 2024 at 21:50):

We feed straight to the SQlite driver (though one of the more major updates got reverted, so not sure the exact state)

view this post on Zulip Ayaz Hafiz (Dec 24 2024 at 21:54):

bug is probably ignoring the error case here
https://github.com/roc-lang/basic-webserver/blob/ead72acb6e22c7cc74c2634d4bf83618ea1a924c/crates/roc_host/src/roc.rs#L378

view this post on Zulip Brendan Hansknecht (Dec 24 2024 at 21:55):

Might be, but I don't think insert returns anything via the cursor. I think with SQlite you have to separately request how many rows were inserted or updated to know if an insert failed.

view this post on Zulip Ayaz Hafiz (Dec 24 2024 at 22:00):

i think that's fine, the cursor will just return done immediately then. but the statement doesn't execute until you call next it looks like

view this post on Zulip Brendan Hansknecht (Dec 24 2024 at 22:00):

Ah yeah. Makes sense

view this post on Zulip Isaac Van Doren (Dec 25 2024 at 20:06):

Cool, thanks!


Last updated: Jul 05 2025 at 12:14 UTC