From 3678bffd5bcff06cdc195d69f9022c7946588db8 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sun, 17 Dec 2023 19:49:24 +0100 Subject: [PATCH] fix error showing up when an invalid channel was provided --- cmd/nourybot/router.go | 12 ++++++------ internal/data/commands.go | 7 ++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cmd/nourybot/router.go b/cmd/nourybot/router.go index ea9cf3d..17cf559 100644 --- a/cmd/nourybot/router.go +++ b/cmd/nourybot/router.go @@ -19,16 +19,16 @@ func (app *application) startRouter() { func (app *application) channelCommandsRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { channel := ps.ByName("channel") + var cs []string + var text string command, err := app.Models.Commands.GetAllChannel(channel) if err != nil { - app.Log.Errorw("Error trying to retrieve all timers from database", err) + app.Log.Errorw("Error trying to retrieve all commands for a channel from database", err) return } - // The slice of timers is only used to log them at // the start so it looks a bit nicer. - var cs []string // Iterate over all timers and then add them onto the scheduler. for i, v := range command { @@ -59,11 +59,11 @@ func (app *application) channelCommandsRoute(w http.ResponseWriter, r *http.Requ // Add new value to the slice cs = append(cs, c) - } - text := strings.Join(cs, "") - fmt.Fprintf(w, fmt.Sprint(text), ps.ByName("name")) + text = strings.Join(cs, "") + fmt.Fprintf(w, fmt.Sprint(text)) + } func (app *application) statusPageRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { diff --git a/internal/data/commands.go b/internal/data/commands.go index 58dae1d..192bd2a 100644 --- a/internal/data/commands.go +++ b/internal/data/commands.go @@ -306,7 +306,12 @@ func (c CommandModel) GetAllChannel(channel string) ([]*Command, error) { &command.Help, ) if err != nil { - return nil, err + switch { + case errors.Is(err, sql.ErrNoRows): + return nil, ErrRecordNotFound + default: + return nil, err + } } // Add the single movie struct onto the slice. commands = append(commands, &command)