From 56a3215147969b4f9a4695fa445dc0d2c8bb73fa Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 29 Jan 2018 16:04:20 +0100 Subject: [PATCH] Return 400 instead of 500 when deleting or showing something where the id is not an int --- routes/api/v1/author_show.go | 2 +- routes/api/v1/book_show.go | 3 +++ routes/api/v1/items_show.go | 2 +- routes/api/v1/publishers_show.go | 2 +- routes/api/v1/user_show.go | 2 +- routes/api/v1/user_update_password.go | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/routes/api/v1/author_show.go b/routes/api/v1/author_show.go index e747ae9..4db1f5e 100644 --- a/routes/api/v1/author_show.go +++ b/routes/api/v1/author_show.go @@ -18,7 +18,7 @@ func AuthorShow(c echo.Context) error { // Make int authorID, err := strconv.ParseInt(author, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error getting author infos."}) + return c.JSON(http.StatusBadRequest, models.Message{"Author ID is invalid."}) } // Get Author Infos diff --git a/routes/api/v1/book_show.go b/routes/api/v1/book_show.go index b00f7b4..50f59fe 100644 --- a/routes/api/v1/book_show.go +++ b/routes/api/v1/book_show.go @@ -17,6 +17,9 @@ func BookShow(c echo.Context) error { // Make int bookID, err := strconv.ParseInt(book, 10, 64) + if err != nil { + return c.JSON(http.StatusBadRequest, models.Message{"Book ID is invalid."}) + } // Get book infos bookInfo, exists, err := models.GetBookByID(bookID) diff --git a/routes/api/v1/items_show.go b/routes/api/v1/items_show.go index 24d3e74..fd1eed2 100644 --- a/routes/api/v1/items_show.go +++ b/routes/api/v1/items_show.go @@ -18,7 +18,7 @@ func ItemShow(c echo.Context) error { // Make int itemID, err := strconv.ParseInt(item, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error getting item infos."}) + return c.JSON(http.StatusBadRequest, models.Message{"Item ID is invalid."}) } // Get item Infos diff --git a/routes/api/v1/publishers_show.go b/routes/api/v1/publishers_show.go index 9e7b3d2..a521608 100644 --- a/routes/api/v1/publishers_show.go +++ b/routes/api/v1/publishers_show.go @@ -18,7 +18,7 @@ func PublisherShow(c echo.Context) error { // Make int publisherID, err := strconv.ParseInt(publisher, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publisher infos."}) + return c.JSON(http.StatusBadRequest, models.Message{"Publisher ID is invalid."}) } // Get Publisher Infos diff --git a/routes/api/v1/user_show.go b/routes/api/v1/user_show.go index 593077e..23ab1e0 100644 --- a/routes/api/v1/user_show.go +++ b/routes/api/v1/user_show.go @@ -24,7 +24,7 @@ func UserShow(c echo.Context) error { // Make int userID, err := strconv.ParseInt(user, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error getting user infos."}) + return c.JSON(http.StatusBadRequest, models.Message{"User ID is invalid."}) } // Get User Infos diff --git a/routes/api/v1/user_update_password.go b/routes/api/v1/user_update_password.go index 0c2e554..0135a80 100644 --- a/routes/api/v1/user_update_password.go +++ b/routes/api/v1/user_update_password.go @@ -25,7 +25,7 @@ func UserChangePassword(c echo.Context) error { // Make int userID, err := strconv.ParseInt(user, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error getting user infos."}) + return c.JSON(http.StatusBadRequest, models.Message{"User ID is invalid."}) } // Check if the user is admin or itself