Error on nonexistent sources instead of returning nothing

This commit is contained in:
Tim Van Baak 2025-01-31 14:16:45 -08:00
parent b0c62a832f
commit a0e4f1f9c4
4 changed files with 19 additions and 2 deletions

View File

@ -48,6 +48,10 @@ func feed(
var items []core.Item
var err error
if source != "" {
if exists, err := core.SourceExists(db, source); !exists || err != nil {
log.Fatalf("error: no such source %s", source)
}
if showInactive {
items, err = core.GetAllItemsForSource(db, source)
} else {

View File

@ -39,6 +39,13 @@ func GetSources(db DB) ([]string, error) {
return names, nil
}
func SourceExists(db DB, source string) (bool, error) {
row := db.QueryRow("select count(*) from sources where name = ?", source)
var c int
err := row.Scan(&c)
return c > 0, err
}
func DeleteSource(db DB, name string) error {
_, err := db.Exec(`
delete from sources

View File

@ -14,9 +14,16 @@ import (
func TestCreateSource(t *testing.T) {
db := EphemeralDb(t)
if exists, err := SourceExists(db, "one"); exists || err != nil {
t.Fatal(err)
}
if err := AddSource(db, "one"); err != nil {
t.Fatal(err)
}
if exists, err := SourceExists(db, "one"); !exists || err != nil {
t.Fatal(err)
}
if err := AddSource(db, "two"); err != nil {
t.Fatal(err)
}

View File

@ -10,12 +10,11 @@ import (
func (env *Env) getSource(writer http.ResponseWriter, req *http.Request) {
source := req.PathValue("source")
if source == "" {
if exists, err := core.SourceExists(env.db, source); !exists || err != nil {
http.NotFound(writer, req)
return
}
// TODO this needs to properly error if the source doesn't exist instead of just returning []
var items []core.Item
var err error
inactive := req.URL.Query().Get("inactive") == "1"