Proper Error Handling

Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
konrad 2017-10-09 18:29:09 +02:00 committed by kolaente
parent c7256eb77b
commit d26df6c160
3 changed files with 13 additions and 4 deletions

View File

@ -16,12 +16,12 @@ func (Book) TableName() string{
return "books"
}
func ListBooks() (books []*Book) {
func ListBooks() (books []*Book, err error) {
err := x.Find(&books)
err = x.Find(&books)
if err != nil {
fmt.Println("Error getting Books", err)
}
return books
return books, err
}

5
models/message.go Normal file
View File

@ -0,0 +1,5 @@
package models
type Message struct{
Message string
}

View File

@ -9,7 +9,11 @@ import (
func List(c echo.Context) error {
list := models.ListBooks()
list, err := models.ListBooks()
if err != nil{
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting books"})
}
return c.JSON(http.StatusOK, list)
}