feat: abort with status instead of returning html on erros

This commit is contained in:
log101 2024-06-25 15:04:20 +03:00
parent 84119e32c5
commit 3f6cf095dc
4 changed files with 5 additions and 15 deletions

View File

@ -19,7 +19,7 @@ func GetEmojiForm(c *gin.Context) {
// get emoji counts for each emoji
emojiCounter, err := GetEmojis(postId)
if err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts"})
c.AbortWithStatus(http.StatusInternalServerError)
return
}
@ -36,7 +36,7 @@ func PostEmojiForm(c *gin.Context) {
// Check if parameters are missing
if reactedPostId == "" || reaction == "" {
c.HTML(http.StatusBadRequest, "emoji_form_error.tmpl", gin.H{"errorMessage": "missing parameters"})
c.AbortWithStatus(http.StatusBadRequest)
}
// Add the new emoji reaction to the database
@ -44,13 +44,13 @@ func PostEmojiForm(c *gin.Context) {
DoUpdates: clause.AssignmentColumns([]string{"emoji"}),
}).Create(&models.EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId})
if result.Error != nil {
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error writing to database"})
c.AbortWithStatus(http.StatusInternalServerError)
}
// get emoji counts for each emoji
emojiCounter, err := GetEmojis(reactedPostId)
if err != nil {
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error getting the emoji counts"})
c.AbortWithStatus(http.StatusInternalServerError)
}
// Return the html with the updated emoji counter

View File

@ -73,7 +73,6 @@ func TestPostEmojiFormMissingParams(t *testing.T) {
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "missing parameters")
}
func TestGetEmojis(t *testing.T) {

View File

@ -6,4 +6,4 @@
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">&nbsp;🤢&nbsp; {{ if gt (index .results "🤢") 0 }} {{ index .results "🤢"}} {{ end }}</button>
<button name="emojiInput" value="👀" type="submit" class="emoji-button">&nbsp;👀&nbsp; {{ if gt (index .results "👀") 0 }} {{ index .results "👀"}} {{ end }}</button>
</div>
<div id="emoji-form-error"><p>{{ .errorMessage }}</p></div>

View File

@ -1,9 +0,0 @@
<div class="emoji-buttons-container">
<button name="emojiInput" value="👍" type="submit" class="emoji-button">&nbsp;👍&nbsp;</button>
<button name="emojiInput" value="👎" type="submit" class="emoji-button">&nbsp;👎&nbsp;</button>
<button name="emojiInput" value="😀" type="submit" class="emoji-button">&nbsp;😀&nbsp;</button>
<button name="emojiInput" value="😑" type="submit" class="emoji-button">&nbsp;😑&nbsp;</button>
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">&nbsp;🤢&nbsp;</button>
<button name="emojiInput" value="👀" type="submit" class="emoji-button">&nbsp;👀&nbsp;</button>
</div>
<div id="emoji-form-error"><p>{{ .errorMessage }}</p></div>