Skip to content

Commit

Permalink
Need to handle different error for not found in add
Browse files Browse the repository at this point in the history
  • Loading branch information
darrikonn committed Dec 7, 2019
1 parent 6640a95 commit b03404b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
10 changes: 8 additions & 2 deletions cli/cmd/add.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

import (
"database/sql"
"strings"

"github.com/spf13/cobra"

"cheat/cli/db"
"cheat/cli/exceptions"
"cheat/cli/models"
"cheat/cli/utils"
)

Expand Down Expand Up @@ -34,8 +36,12 @@ the cheat's "description" in your preferred editor.

Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cheat := db.GetCheatByName(args[0], false)
if cheat.Name == args[0] {
cheat, err := db.GetCheatByName(args[0], false)
if err != nil && err.(*exceptions.CheatExceptionType).Original() != sql.ErrNoRows {
panic(err)
}

if cheat != (models.Cheat{}) && cheat.Name == args[0] {
panic(exceptions.CheatException("<Cheat: "+args[0]+"> already exists!", nil))
}

Expand Down
6 changes: 5 additions & 1 deletion cli/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ Delete a cheat from your cheatsheet.

Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cheat := db.GetCheatByName(args[0], deleteFlags.ignoreCase)
cheat, err := db.GetCheatByName(args[0], deleteFlags.ignoreCase)
if err != nil {
panic(err)
}

if !deleteFlags.yes {
utils.Render(
" {BOLD}{name}{RESET}: {description}\n",
Expand Down
5 changes: 4 additions & 1 deletion cli/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ prompted for the cheat's "description" in your preferred editor.

Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cheat := db.GetCheatByName(args[0], editFlags.ignoreCase)
cheat, err := db.GetCheatByName(args[0], editFlags.ignoreCase)
if err != nil {
panic(err)
}

// Let's first try to rename the cheat
if cmd.Flags().Changed("name") {
Expand Down
5 changes: 4 additions & 1 deletion cli/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ Get a cheat info by name

Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cheat := db.GetCheatByName(args[0], getFlags.ignoreCase)
cheat, err := db.GetCheatByName(args[0], getFlags.ignoreCase)
if err != nil {
panic(err)
}

// Header
utils.Render(
Expand Down
6 changes: 5 additions & 1 deletion cli/db/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ func RenameCheat(name string, newName string) _Cheat {
)
}

return GetCheatByName(newName, false)
cheat, err := GetCheatByName(newName, false)
if err != nil {
panic(err)
}
return cheat
}

// EditCheat : edits cheat's attributes in the database
Expand Down
8 changes: 4 additions & 4 deletions cli/db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// GetCheatByName : returns a cheat by name from the database
func GetCheatByName(name string, ignoreCase bool) _Cheat {
func GetCheatByName(name string, ignoreCase bool) (_Cheat, error) {
var cheat _Cheat
row := database.QueryRow(`
SELECT name, created, description, weight FROM cheat
Expand All @@ -21,11 +21,11 @@ func GetCheatByName(name string, ignoreCase bool) _Cheat {

switch err {
case sql.ErrNoRows:
panic(exceptions.CheatException("<Cheat: "+name+"> could not be found!", err))
return cheat, exceptions.CheatException("<Cheat: "+name+"> could not be found!", err)
case nil:
return cheat
return cheat, nil
default:
panic(exceptions.CheatException("Unknown exception occurred", err))
return cheat, exceptions.CheatException("Unknown exception occurred", err)
}
}

Expand Down

0 comments on commit b03404b

Please sign in to comment.