feat: abort with status instead of returning html on erros
This commit is contained in:
parent
84119e32c5
commit
3f6cf095dc
|
@ -19,7 +19,7 @@ func GetEmojiForm(c *gin.Context) {
|
||||||
// get emoji counts for each emoji
|
// get emoji counts for each emoji
|
||||||
emojiCounter, err := GetEmojis(postId)
|
emojiCounter, err := GetEmojis(postId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts"})
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ func PostEmojiForm(c *gin.Context) {
|
||||||
|
|
||||||
// Check if parameters are missing
|
// Check if parameters are missing
|
||||||
if reactedPostId == "" || reaction == "" {
|
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
|
// Add the new emoji reaction to the database
|
||||||
|
@ -44,13 +44,13 @@ func PostEmojiForm(c *gin.Context) {
|
||||||
DoUpdates: clause.AssignmentColumns([]string{"emoji"}),
|
DoUpdates: clause.AssignmentColumns([]string{"emoji"}),
|
||||||
}).Create(&models.EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId})
|
}).Create(&models.EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId})
|
||||||
if result.Error != nil {
|
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
|
// get emoji counts for each emoji
|
||||||
emojiCounter, err := GetEmojis(reactedPostId)
|
emojiCounter, err := GetEmojis(reactedPostId)
|
||||||
if err != nil {
|
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
|
// Return the html with the updated emoji counter
|
||||||
|
|
|
@ -73,7 +73,6 @@ func TestPostEmojiFormMissingParams(t *testing.T) {
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
assert.Contains(t, w.Body.String(), "missing parameters")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetEmojis(t *testing.T) {
|
func TestGetEmojis(t *testing.T) {
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
<button name="emojiInput" value="🤢" type="submit" class="emoji-button"> 🤢 {{ if gt (index .results "🤢") 0 }} {{ index .results "🤢"}} {{ end }}</button>
|
<button name="emojiInput" value="🤢" type="submit" class="emoji-button"> 🤢 {{ if gt (index .results "🤢") 0 }} {{ index .results "🤢"}} {{ end }}</button>
|
||||||
<button name="emojiInput" value="👀" type="submit" class="emoji-button"> 👀 {{ if gt (index .results "👀") 0 }} {{ index .results "👀"}} {{ end }}</button>
|
<button name="emojiInput" value="👀" type="submit" class="emoji-button"> 👀 {{ if gt (index .results "👀") 0 }} {{ index .results "👀"}} {{ end }}</button>
|
||||||
</div>
|
</div>
|
||||||
|
<div id="emoji-form-error"><p>{{ .errorMessage }}</p></div>
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
<div class="emoji-buttons-container">
|
|
||||||
<button name="emojiInput" value="👍" type="submit" class="emoji-button"> 👍 </button>
|
|
||||||
<button name="emojiInput" value="👎" type="submit" class="emoji-button"> 👎 </button>
|
|
||||||
<button name="emojiInput" value="😀" type="submit" class="emoji-button"> 😀 </button>
|
|
||||||
<button name="emojiInput" value="😑" type="submit" class="emoji-button"> 😑 </button>
|
|
||||||
<button name="emojiInput" value="🤢" type="submit" class="emoji-button"> 🤢 </button>
|
|
||||||
<button name="emojiInput" value="👀" type="submit" class="emoji-button"> 👀 </button>
|
|
||||||
</div>
|
|
||||||
<div id="emoji-form-error"><p>{{ .errorMessage }}</p></div>
|
|
Loading…
Reference in New Issue
Block a user