Skip to content

Commit 3008f91

Browse files
pikachu0542tallen42
authored andcommitted
Changed all status codes to the http.StatusCode equivalent. Also changed some 500s to 400s where appropriate to indicate user error (#85)
1 parent 257d216 commit 3008f91

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

main.go

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func main() {
104104

105105
polls, err := database.GetOpenPolls(c)
106106
if err != nil {
107-
c.JSON(500, gin.H{"error": err.Error()})
107+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
108108
return
109109
}
110110
sort.Slice(polls, func(i, j int) bool {
@@ -113,12 +113,12 @@ func main() {
113113

114114
closedPolls, err := database.GetClosedVotedPolls(c, claims.UserInfo.Username)
115115
if err != nil {
116-
c.JSON(500, gin.H{"error": err.Error()})
116+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
117117
return
118118
}
119119
ownedPolls, err := database.GetClosedOwnedPolls(c, claims.UserInfo.Username)
120120
if err != nil {
121-
c.JSON(500, gin.H{"error": err.Error()})
121+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
122122
return
123123
}
124124
closedPolls = append(closedPolls, ownedPolls...)
@@ -128,7 +128,7 @@ func main() {
128128
})
129129
closedPolls = uniquePolls(closedPolls)
130130

131-
c.HTML(200, "index.tmpl", gin.H{
131+
c.HTML(http.StatusOK, "index.tmpl", gin.H{
132132
"Polls": polls,
133133
"Username": claims.UserInfo.Username,
134134
"FullName": claims.UserInfo.FullName,
@@ -141,12 +141,12 @@ func main() {
141141

142142
closedPolls, err := database.GetClosedVotedPolls(c, claims.UserInfo.Username)
143143
if err != nil {
144-
c.JSON(500, gin.H{"error": err.Error()})
144+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
145145
return
146146
}
147147
ownedPolls, err := database.GetClosedOwnedPolls(c, claims.UserInfo.Username)
148148
if err != nil {
149-
c.JSON(500, gin.H{"error": err.Error()})
149+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
150150
return
151151
}
152152
closedPolls = append(closedPolls, ownedPolls...)
@@ -156,7 +156,7 @@ func main() {
156156
})
157157
closedPolls = uniquePolls(closedPolls)
158158

159-
c.HTML(200, "closed.tmpl", gin.H{
159+
c.HTML(http.StatusOK, "closed.tmpl", gin.H{
160160
"ClosedPolls": closedPolls,
161161
"Username": claims.UserInfo.Username,
162162
"FullName": claims.UserInfo.FullName,
@@ -167,14 +167,14 @@ func main() {
167167
cl, _ := c.Get("cshauth")
168168
claims := cl.(cshAuth.CSHClaims)
169169
if !DEV_DISABLE_ACTIVE_FILTERS && !slices.Contains(claims.UserInfo.Groups, "active") {
170-
c.HTML(403, "unauthorized.tmpl", gin.H{
170+
c.HTML(http.StatusForbidden, "unauthorized.tmpl", gin.H{
171171
"Username": claims.UserInfo.Username,
172172
"FullName": claims.UserInfo.FullName,
173173
})
174174
return
175175
}
176176

177-
c.HTML(200, "create.tmpl", gin.H{
177+
c.HTML(http.StatusOK, "create.tmpl", gin.H{
178178
"Username": claims.UserInfo.Username,
179179
"FullName": claims.UserInfo.FullName,
180180
"IsEvals": isEvals(claims.UserInfo),
@@ -185,7 +185,7 @@ func main() {
185185
cl, _ := c.Get("cshauth")
186186
claims := cl.(cshAuth.CSHClaims)
187187
if !DEV_DISABLE_ACTIVE_FILTERS && !slices.Contains(claims.UserInfo.Groups, "active") {
188-
c.HTML(403, "unauthorized.tmpl", gin.H{
188+
c.HTML(http.StatusForbidden, "unauthorized.tmpl", gin.H{
189189
"Username": claims.UserInfo.Username,
190190
"FullName": claims.UserInfo.FullName,
191191
})
@@ -239,7 +239,7 @@ func main() {
239239
}
240240
if poll.Gatekeep {
241241
if !isEvals(claims.UserInfo) {
242-
c.HTML(403, "unauthorized.tmpl", gin.H{
242+
c.HTML(http.StatusForbidden, "unauthorized.tmpl", gin.H{
243243
"Username": claims.UserInfo.Username,
244244
"FullName": claims.UserInfo.FullName,
245245
})
@@ -253,11 +253,11 @@ func main() {
253253

254254
pollId, err := database.CreatePoll(c, poll)
255255
if err != nil {
256-
c.JSON(500, gin.H{"error": err.Error()})
256+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
257257
return
258258
}
259259

260-
c.Redirect(302, "/poll/"+pollId)
260+
c.Redirect(http.StatusFound, "/poll/"+pollId)
261261
}))
262262

263263
r.GET("/poll/:id", csh.AuthWrapper(func(c *gin.Context) {
@@ -268,13 +268,13 @@ func main() {
268268

269269
poll, err := database.GetPoll(c, c.Param("id"))
270270
if err != nil {
271-
c.JSON(500, gin.H{"error": err.Error()})
271+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
272272
return
273273
}
274274

275275
// If the user can't vote, just show them results
276276
if canVote(claims.UserInfo, *poll, poll.AllowedUsers) > 0 || !poll.Open {
277-
c.Redirect(302, "/results/"+poll.Id)
277+
c.Redirect(http.StatusFound, "/results/"+poll.Id)
278278
return
279279
}
280280

@@ -305,18 +305,18 @@ func main() {
305305

306306
poll, err := database.GetPoll(c, c.Param("id"))
307307
if err != nil {
308-
c.JSON(500, gin.H{"error": err.Error()})
308+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
309309
return
310310
}
311311

312312
if canVote(claims.UserInfo, *poll, poll.AllowedUsers) > 0 || !poll.Open {
313-
c.Redirect(302, "/results/"+poll.Id)
313+
c.Redirect(http.StatusFound, "/results/"+poll.Id)
314314
return
315315
}
316316

317317
pId, err := primitive.ObjectIDFromHex(poll.Id)
318318
if err != nil {
319-
c.JSON(500, gin.H{"error": err.Error()})
319+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
320320
return
321321
}
322322

@@ -336,7 +336,7 @@ func main() {
336336
} else if poll.AllowWriteIns && c.PostForm("option") == "writein" {
337337
vote.Option = c.PostForm("writeinOption")
338338
} else {
339-
c.JSON(400, gin.H{"error": "Invalid Option"})
339+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Option"})
340340
return
341341
}
342342
database.CastSimpleVote(c, &vote, &voter)
@@ -359,7 +359,7 @@ func main() {
359359
continue
360360
}
361361
if err != nil {
362-
c.JSON(400, gin.H{"error": "non-number ranking"})
362+
c.JSON(http.StatusBadRequest, gin.H{"error": "non-number ranking"})
363363
return
364364
}
365365

@@ -370,17 +370,17 @@ func main() {
370370
if c.PostForm("writeinOption") != "" && c.PostForm("writein") != "" {
371371
for candidate := range vote.Options {
372372
if strings.EqualFold(candidate, strings.TrimSpace(c.PostForm("writeinOption"))) {
373-
c.JSON(500, gin.H{"error": "Write-in is already an option"})
373+
c.JSON(http.StatusBadRequest, gin.H{"error": "Write-in is already an option"})
374374
return
375375
}
376376
}
377377
rank, err := strconv.Atoi(c.PostForm("writein"))
378378
if err != nil {
379-
c.JSON(500, gin.H{"error": "Write-in rank is not numerical"})
379+
c.JSON(http.StatusBadRequest, gin.H{"error": "Write-in rank is not numerical"})
380380
return
381381
}
382382
if rank < 1 {
383-
c.JSON(500, gin.H{"error": "Write-in rank is not positive"})
383+
c.JSON(http.StatusBadRequest, gin.H{"error": "Write-in rank is not positive"})
384384
return
385385
}
386386
vote.Options[c.PostForm("writeinOption")] = rank
@@ -392,26 +392,26 @@ func main() {
392392
for _, rank := range vote.Options {
393393
if rank > 0 && rank <= maxNum {
394394
if voted[rank-1] {
395-
c.JSON(400, gin.H{"error": "You ranked two or more candidates at the same level"})
395+
c.JSON(http.StatusBadRequest, gin.H{"error": "You ranked two or more candidates at the same level"})
396396
return
397397
}
398398
voted[rank-1] = true
399399
} else {
400-
c.JSON(400, gin.H{"error": fmt.Sprintf("votes must be from 1 - %d", maxNum)})
400+
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("votes must be from 1 - %d", maxNum)})
401401
return
402402
}
403403
}
404404

405405
rankedCandidates := len(vote.Options)
406406
for _, voteOpt := range vote.Options {
407407
if voteOpt > rankedCandidates {
408-
c.JSON(400, gin.H{"error": "Rank choice is more than the amount of candidates ranked"})
408+
c.JSON(http.StatusBadRequest, gin.H{"error": "Rank choice is more than the amount of candidates ranked"})
409409
return
410410
}
411411
}
412412
database.CastRankedVote(c, &vote, &voter)
413413
} else {
414-
c.JSON(500, gin.H{"error": "Unknown Poll Type"})
414+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unknown Poll Type"})
415415
return
416416
}
417417

@@ -427,7 +427,7 @@ func main() {
427427
}
428428
}
429429

430-
c.Redirect(302, "/results/"+poll.Id)
430+
c.Redirect(http.StatusFound, "/results/"+poll.Id)
431431
}))
432432

433433
r.GET("/results/:id", csh.AuthWrapper(func(c *gin.Context) {
@@ -438,20 +438,20 @@ func main() {
438438

439439
poll, err := database.GetPoll(c, c.Param("id"))
440440
if err != nil {
441-
c.JSON(500, gin.H{"error": err.Error()})
441+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
442442
return
443443
}
444444

445445
results, err := poll.GetResult(c)
446446
if err != nil {
447-
c.JSON(500, gin.H{"error": err.Error()})
447+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
448448
return
449449
}
450450

451451
canModify := containsString(claims.UserInfo.Groups, "active_rtp") || containsString(claims.UserInfo.Groups, "eboard") || poll.CreatedBy == claims.UserInfo.Username
452452

453453
votesNeededForQuorum := int(poll.QuorumType * float64(len(poll.AllowedUsers)))
454-
c.HTML(200, "result.tmpl", gin.H{
454+
c.HTML(http.StatusOK, "result.tmpl", gin.H{
455455
"Id": poll.Id,
456456
"Title": poll.Title,
457457
"Description": poll.Description,
@@ -476,18 +476,18 @@ func main() {
476476

477477
poll, err := database.GetPoll(c, c.Param("id"))
478478
if err != nil {
479-
c.JSON(500, gin.H{"error": err.Error()})
479+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
480480
return
481481
}
482482

483483
if poll.CreatedBy != claims.UserInfo.Username {
484-
c.JSON(403, gin.H{"error": "Only the creator can hide a poll result"})
484+
c.JSON(http.StatusForbidden, gin.H{"error": "Only the creator can hide a poll result"})
485485
return
486486
}
487487

488488
err = poll.Hide(c)
489489
if err != nil {
490-
c.JSON(500, gin.H{"error": err.Error()})
490+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
491491
return
492492
}
493493
pId, _ := primitive.ObjectIDFromHex(poll.Id)
@@ -500,11 +500,11 @@ func main() {
500500
}
501501
err = database.WriteAction(c, &action)
502502
if err != nil {
503-
c.JSON(500, gin.H{"error": err.Error()})
503+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
504504
return
505505
}
506506

507-
c.Redirect(302, "/results/"+poll.Id)
507+
c.Redirect(http.StatusFound, "/results/"+poll.Id)
508508
}))
509509

510510
r.POST("/poll/:id/close", csh.AuthWrapper(func(c *gin.Context) {
@@ -515,7 +515,7 @@ func main() {
515515

516516
poll, err := database.GetPoll(c, c.Param("id"))
517517
if err != nil {
518-
c.JSON(500, gin.H{"error": err.Error()})
518+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
519519
return
520520
}
521521

@@ -527,14 +527,14 @@ func main() {
527527
if poll.CreatedBy != claims.UserInfo.Username {
528528
if containsString(claims.UserInfo.Groups, "active_rtp") || containsString(claims.UserInfo.Groups, "eboard") {
529529
} else {
530-
c.JSON(403, gin.H{"error": "You cannot end this poll."})
530+
c.JSON(http.StatusForbidden, gin.H{"error": "You cannot end this poll."})
531531
return
532532
}
533533
}
534534

535535
err = poll.Close(c)
536536
if err != nil {
537-
c.JSON(500, gin.H{"error": err.Error()})
537+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
538538
return
539539
}
540540
pId, _ := primitive.ObjectIDFromHex(poll.Id)
@@ -547,11 +547,11 @@ func main() {
547547
}
548548
err = database.WriteAction(c, &action)
549549
if err != nil {
550-
c.JSON(500, gin.H{"error": err.Error()})
550+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
551551
return
552552
}
553553

554-
c.Redirect(302, "/results/"+poll.Id)
554+
c.Redirect(http.StatusFound, "/results/"+poll.Id)
555555
}))
556556

557557
r.GET("/stream/:topic", csh.AuthWrapper(broker.ServeHTTP))

0 commit comments

Comments
 (0)