Small error handling refactor

This commit is contained in:
Florian Sylvain
2022-10-21 12:15:29 +02:00
parent 85258c985c
commit 2f69cd6c69
3 changed files with 27 additions and 28 deletions
+6 -6
View File
@@ -6,14 +6,14 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type answer struct { func SendBadRequest(c *gin.Context, message string) {
Message string `json:"message"` c.JSON(http.StatusBadRequest, gin.H{"message": message})
} }
func SendErrorMessageToClient(c *gin.Context, message string) { func SendOk(c *gin.Context, message string) {
c.JSON(http.StatusBadRequest, answer{Message: message}) c.JSON(http.StatusOK, gin.H{"message": message})
} }
func SendOkMessageToClient(c *gin.Context, message string) { func SendForbidden(c *gin.Context, message string) {
c.JSON(http.StatusOK, answer{Message: message}) c.JSON(http.StatusForbidden, gin.H{"message": message})
} }
+12 -13
View File
@@ -25,7 +25,7 @@ func GetAllArticles(c *gin.Context) {
var articles []Article var articles []Article
documents, err := getDocuments(ARTICLES_LOCATION, bson.D{}) documents, err := getDocuments(ARTICLES_LOCATION, bson.D{})
if err != nil { if err != nil {
SendErrorMessageToClient(c, err.Error()) SendBadRequest(c, err.Error())
return return
} }
@@ -43,11 +43,11 @@ func GetArticle(c *gin.Context) {
articles, err := getDocuments(ARTICLES_LOCATION, articles, err := getDocuments(ARTICLES_LOCATION,
bson.D{{Key: "id_name", Value: articleID}}) bson.D{{Key: "id_name", Value: articleID}})
if err != nil { if err != nil {
SendErrorMessageToClient(c, err.Error()) SendBadRequest(c, err.Error())
return return
} }
if len(articles) == 0 { if len(articles) == 0 {
SendErrorMessageToClient(c, "The ID provided doesn't match any article.") SendBadRequest(c, "The ID provided doesn't match any article.")
return return
} }
var parsedArticle Article var parsedArticle Article
@@ -71,50 +71,49 @@ func AddArticle(c *gin.Context) {
var article Article var article Article
article.Id_name = c.Params.ByName("id") article.Id_name = c.Params.ByName("id")
if c.BindJSON(&article) != nil { if c.BindJSON(&article) != nil {
SendErrorMessageToClient(c, "Could not correctly parse the article.") SendBadRequest(c, "Could not correctly parse the article.")
return return
} }
document, err := bson.Marshal(article) document, err := bson.Marshal(article)
if err != nil { if err != nil {
SendErrorMessageToClient(c, "Could not correctly marshal the article.") SendBadRequest(c, "Could not correctly marshal the article.")
return return
} }
if IsArticleIdAlreadyUsed(article.Id_name) { if IsArticleIdAlreadyUsed(article.Id_name) {
SendErrorMessageToClient(c, "Article ID already used.") SendBadRequest(c, "Article ID already used.")
return return
} }
err = pushDocument(ARTICLES_LOCATION, document) err = pushDocument(ARTICLES_LOCATION, document)
if err != nil { if err != nil {
SendErrorMessageToClient(c, "Could not insert document into DB.") SendBadRequest(c, "Could not insert document into DB.")
return return
} }
SendOkMessageToClient(c, "Article successfully added!") SendOk(c, "Article successfully added!")
} }
func DeleteArticle(c *gin.Context) { func DeleteArticle(c *gin.Context) {
var delArticle DelArticle var delArticle DelArticle
delArticle.Id_name = c.Params.ByName("id") delArticle.Id_name = c.Params.ByName("id")
if c.BindJSON(&delArticle) != nil { if c.BindJSON(&delArticle) != nil {
SendErrorMessageToClient(c, "Could not correctly parse the article ID.") SendBadRequest(c, "Could not correctly parse the article ID.")
return return
} }
document, err := bson.Marshal(delArticle) document, err := bson.Marshal(delArticle)
if err != nil { if err != nil {
SendErrorMessageToClient(c, "Could not correctly marshal the article ID.") SendBadRequest(c, "Could not correctly marshal the article ID.")
return return
} }
deleteCount, err := deleteDocument(ARTICLES_LOCATION, document) deleteCount, err := deleteDocument(ARTICLES_LOCATION, document)
if err != nil { if err != nil {
SendErrorMessageToClient(c, "Could not insert document into DB.") SendBadRequest(c, "Could not insert document into DB.")
return return
} }
SendOkMessageToClient(c, SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount))
fmt.Sprintf("%d articles were successfully deleted!", deleteCount))
} }
+9 -9
View File
@@ -54,40 +54,40 @@ func parseUserFromContext(c *gin.Context) (User, error) {
func LoginUser(c *gin.Context) { func LoginUser(c *gin.Context) {
user, err := parseUserFromContext(c) user, err := parseUserFromContext(c)
if err != nil { if err != nil {
SendErrorMessageToClient(c, err.Error()) SendBadRequest(c, err.Error())
return return
} }
if isUserLoggedIn(user) { if isUserLoggedIn(user) {
SendErrorMessageToClient(c, "User is already logged in!") SendBadRequest(c, "User is already logged in!")
return return
} }
if !isUserReal(user) { if !isUserReal(user) {
SendErrorMessageToClient(c, "Unknown email or wrong password.") SendBadRequest(c, "Unknown email or wrong password.")
return return
} }
addSession(user) addSession(user)
SendOkMessageToClient(c, "User successfully logged in.") SendOk(c, "User successfully logged in.")
} }
func LogoutUser(c *gin.Context) { func LogoutUser(c *gin.Context) {
user, err := parseUserFromContext(c) user, err := parseUserFromContext(c)
if err != nil { if err != nil {
SendErrorMessageToClient(c, err.Error()) SendBadRequest(c, err.Error())
return return
} }
if !isUserLoggedIn(user) { if !isUserLoggedIn(user) {
SendErrorMessageToClient(c, "User is not logged in!") SendBadRequest(c, "User is not logged in!")
return return
} }
removeSession(user) removeSession(user)
SendOkMessageToClient(c, "User successfully logged out.") SendOk(c, "User successfully logged out.")
} }
func AuthCheck(c *gin.Context) { func AuthCheck(c *gin.Context) {
var user User var user User
username, password, isOk := c.Request.BasicAuth() username, password, isOk := c.Request.BasicAuth()
if !isOk { if !isOk {
SendErrorMessageToClient(c, "Incorrect or missing user credentials.") SendBadRequest(c, "Incorrect or missing user credentials.")
c.Abort() c.Abort()
return return
} }
@@ -97,7 +97,7 @@ func AuthCheck(c *gin.Context) {
user.Password = getUserHashedPassword(user) user.Password = getUserHashedPassword(user)
if !isUserLoggedIn(user) { if !isUserLoggedIn(user) {
SendErrorMessageToClient(c, "Authentification failed, credentials could be wrong, user may not be logged in, session may have expired.") SendForbidden(c, "Authentification failed, credentials could be wrong, user may not be logged in, session may have expired.")
c.Abort() c.Abort()
return return
} }