Skip to content

Commit

Permalink
Rename ResponseApiErr
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jul 22, 2024
1 parent 0f363ea commit dc29d67
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 83 deletions.
6 changes: 3 additions & 3 deletions internal/appeal/appeal_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (h *appealHandler) onAPIGetBanMessages() gin.HandlerFunc {
return func(ctx *gin.Context) {
banID, errParam := httphelper.GetInt64Param(ctx, "ban_id")
if errParam != nil {
httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrInvalidParameter)
httphelper.ResponseAPIErr(ctx, http.StatusNotFound, domain.ErrInvalidParameter)
slog.Warn("Got invalid ban_id parameter", log.ErrAttr(errParam), log.HandlerName(2))

return
Expand Down Expand Up @@ -102,7 +102,7 @@ func (h *appealHandler) editBanMessage() gin.HandlerFunc {

msg, errSave := h.appealUsecase.EditBanMessage(ctx, curUser, reportMessageID, req.BodyMD)
if errSave != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
slog.Error("Failed to save ban appeal message", log.ErrAttr(errSave), log.HandlerName(2))

return
Expand Down Expand Up @@ -143,7 +143,7 @@ func (h *appealHandler) onAPIGetAppeals() gin.HandlerFunc {

bans, errBans := h.appealUsecase.GetAppealsByActivity(ctx, req)
if errBans != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
slog.Error("Failed to fetch appeals", log.ErrAttr(errBans))

return
Expand Down
10 changes: 5 additions & 5 deletions internal/asset/asset_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (h mediaHandler) onAPISaveMedia() gin.HandlerFunc {

mediaFile, errOpen := req.File.Open()
if errOpen != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
slog.Error("Failed to open form file", log.ErrAttr(errOpen), handlerName)

return
Expand All @@ -60,7 +60,7 @@ func (h mediaHandler) onAPISaveMedia() gin.HandlerFunc {

media, errMedia := h.assets.Create(ctx, httphelper.CurrentUserProfile(ctx).SteamID, "media", req.Name, mediaFile)
if errMedia != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, errMedia)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, errMedia)
slog.Error("Failed to create new asset", log.ErrAttr(errMedia), handlerName)

return
Expand All @@ -76,15 +76,15 @@ func (h mediaHandler) onGetByUUID() gin.HandlerFunc {
return func(ctx *gin.Context) {
mediaID, idErr := httphelper.GetUUIDParam(ctx, "asset_id")
if idErr != nil {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
slog.Error("Got invalid asset_id", handlerName)

return
}

asset, reader, errGet := h.assets.Get(ctx, mediaID)
if errGet != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, errGet)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, errGet)
slog.Error("Failed to load asset", slog.String("asset_id", mediaID.String()), handlerName)

return
Expand All @@ -93,7 +93,7 @@ func (h mediaHandler) onGetByUUID() gin.HandlerFunc {
if asset.IsPrivate {
user := httphelper.CurrentUserProfile(ctx)
if !user.SteamID.Valid() && (user.SteamID == asset.AuthorID || user.HasPermission(domain.PModerator)) {
httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)
httphelper.ResponseAPIErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)
slog.Warn("Tried to access private asset", slog.String("asset_id", mediaID.String()), handlerName)

return
Expand Down
6 changes: 3 additions & 3 deletions internal/auth/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (h authHandler) onAPILogout() gin.HandlerFunc {
return func(ctx *gin.Context) {
fingerprint, errCookie := ctx.Cookie(domain.FingerprintCookieName)
if errCookie != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, nil)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, nil)
slog.Warn("Failed to get fingerprint", handlerName)

return
Expand All @@ -184,14 +184,14 @@ func (h authHandler) onAPILogout() gin.HandlerFunc {

personAuth := domain.PersonAuth{}
if errGet := h.authUsecase.GetPersonAuthByRefreshToken(ctx, fingerprint, &personAuth); errGet != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, nil)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, nil)
slog.Warn("Failed to load person via fingerprint", handlerName)

return
}

if errDelete := h.authUsecase.DeletePersonAuth(ctx, personAuth.PersonAuthID); errDelete != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, nil)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, nil)
slog.Error("Failed to delete person auth on logout", log.ErrAttr(errDelete), handlerName)

return
Expand Down
6 changes: 3 additions & 3 deletions internal/ban/ban_asn_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (h banASNHandler) onAPIGetBansASN() gin.HandlerFunc {

bansASN, errBans := h.banASN.Get(ctx, req)
if errBans != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
slog.Error("Failed to fetch banASN", log.ErrAttr(errBans))

return
Expand All @@ -78,7 +78,7 @@ func (h banASNHandler) onAPIDeleteBansASN() gin.HandlerFunc {
return func(ctx *gin.Context) {
asnID, asnIDErr := httphelper.GetInt64Param(ctx, "asn_id")
if asnIDErr != nil {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)

return
}
Expand All @@ -103,7 +103,7 @@ func (h banASNHandler) onAPIPostBansASNUpdate() gin.HandlerFunc {
return func(ctx *gin.Context) {
asnID, asnIDErr := httphelper.GetInt64Param(ctx, "asn_id")
if asnIDErr != nil {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)

return
}
Expand Down
14 changes: 7 additions & 7 deletions internal/ban/ban_net_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (h banNetHandler) onAPIExportBansValveIP() gin.HandlerFunc {
return func(ctx *gin.Context) {
bans, errBans := h.bans.Get(ctx, domain.CIDRBansQueryFilter{})
if errBans != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)

return
}
Expand Down Expand Up @@ -87,20 +87,20 @@ func (h banNetHandler) onAPIPostBansCIDRCreate() gin.HandlerFunc {

if errBanCIDR := domain.NewBanCIDR(sid, targetID, duration, req.Reason, req.ReasonText, req.Note, domain.Web,
req.CIDR, domain.Banned, &banCIDR); errBanCIDR != nil {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
slog.Warn("Failed to create new ban cidr", log.ErrAttr(errBanCIDR))

return
}

if errBan := h.bans.Ban(ctx, &banCIDR); errBan != nil {
if errors.Is(errBan, domain.ErrDuplicate) {
httphelper.ResponseApiErr(ctx, http.StatusConflict, domain.ErrDuplicate)
httphelper.ResponseAPIErr(ctx, http.StatusConflict, domain.ErrDuplicate)

return
}

httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
slog.Error("Failed to save cidr ban", log.ErrAttr(errBan))

return
Expand All @@ -124,7 +124,7 @@ func (h banNetHandler) onAPIGetBansCIDR() gin.HandlerFunc {

bans, errBans := h.bans.Get(ctx, req)
if errBans != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
slog.Error("Failed to fetch cidr bans", log.ErrAttr(errBans))

return
Expand All @@ -138,7 +138,7 @@ func (h banNetHandler) onAPIDeleteBansCIDR() gin.HandlerFunc {
return func(ctx *gin.Context) {
netID, netIDErr := httphelper.GetInt64Param(ctx, "net_id")
if netIDErr != nil {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)

return
}
Expand All @@ -164,7 +164,7 @@ func (h banNetHandler) onAPIPostBansCIDRUpdate() gin.HandlerFunc {
return func(ctx *gin.Context) {
netID, banIDErr := httphelper.GetInt64Param(ctx, "net_id")
if banIDErr != nil {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)

return
}
Expand Down
4 changes: 2 additions & 2 deletions internal/blocklist/blocklist_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (b *blocklistHandler) onAPICreateWhitelistSteam() gin.HandlerFunc {

steamID, ok := req.SteamID(ctx)
if !ok {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidSID)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidSID)
slog.Warn("Got invalid steamid", slog.String("steam_id", req.SteamIDValue))

return
Expand Down Expand Up @@ -232,7 +232,7 @@ func (b *blocklistHandler) onAPIPostBlockListUpdate() gin.HandlerFunc {

blockSource, errUpdate := b.blocklists.UpdateCIDRBlockSource(ctx, sourceID, req.Name, req.URL, req.Enabled)
if errUpdate != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)

return
}
Expand Down
8 changes: 4 additions & 4 deletions internal/chat/chat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (h chatHandler) onAPIQueryMessages() gin.HandlerFunc {
if errChat != nil && !errors.Is(errChat, domain.ErrNoResult) {
slog.Error("Failed to query messages history",
log.ErrAttr(errChat), slog.String("sid", req.SourceID))
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)

return
}
Expand All @@ -57,23 +57,23 @@ func (h chatHandler) onAPIQueryMessageContext() gin.HandlerFunc {
return func(ctx *gin.Context) {
messageID, errMessageID := httphelper.GetInt64Param(ctx, "person_message_id")
if errMessageID != nil {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
slog.Debug("Got invalid person_message_id", log.ErrAttr(errMessageID))

return
}

padding, errPadding := httphelper.GetIntParam(ctx, "padding")
if errPadding != nil {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
slog.Debug("Got invalid padding", log.ErrAttr(errPadding))

return
}

messages, errQuery := h.chat.GetPersonMessageContext(ctx, messageID, padding)
if errQuery != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)

return
}
Expand Down
34 changes: 17 additions & 17 deletions internal/contest/contest_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *contestHandler) contestFromCtx(ctx *gin.Context) (domain.Contest, bool)
}

if !contest.Public && httphelper.CurrentUserProfile(ctx).PermissionLevel < domain.PModerator {
httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrNotFound)
httphelper.ResponseAPIErr(ctx, http.StatusForbidden, domain.ErrNotFound)

return domain.Contest{}, false
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func (c *contestHandler) onAPIDeleteContest() gin.HandlerFunc {

if errContest := c.contests.ContestByID(ctx, contestID, &contest); errContest != nil {
if errors.Is(errContest, domain.ErrNoResult) {
httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrUnknownID)
httphelper.ResponseAPIErr(ctx, http.StatusNotFound, domain.ErrUnknownID)

return
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func (c *contestHandler) onAPISaveContestEntryMedia() gin.HandlerFunc {
}

if !slices.Contains(strings.Split(strings.ToLower(contest.MediaTypes), ","), strings.ToLower(mimeType.String())) {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrMimeTypeNotAllowed)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrMimeTypeNotAllowed)
slog.Warn("User tried to upload file with disallowed mime type", slog.String("mime", strings.ToLower(mimeType.String())))

return
Expand Down Expand Up @@ -331,7 +331,7 @@ func (c *contestHandler) onAPISaveContestEntrySubmit() gin.HandlerFunc {

existingEntries, errEntries := c.contests.ContestEntries(ctx, contest.ContestID)
if errEntries != nil && !errors.Is(errEntries, domain.ErrNoResult) {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrContestLoadEntries)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrContestLoadEntries)

return
}
Expand All @@ -344,7 +344,7 @@ func (c *contestHandler) onAPISaveContestEntrySubmit() gin.HandlerFunc {
}

if own >= contest.MaxSubmissions {
httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrContestMaxEntries)
httphelper.ResponseAPIErr(ctx, http.StatusForbidden, domain.ErrContestMaxEntries)

return
}
Expand All @@ -354,26 +354,26 @@ func (c *contestHandler) onAPISaveContestEntrySubmit() gin.HandlerFunc {

asset, _, errAsset := c.assets.Get(ctx, req.AssetID)
if errAsset != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrEntryCreate)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrEntryCreate)

return
}

if asset.AuthorID != steamID {
httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)
httphelper.ResponseAPIErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)

return
}

entry, errEntry := contest.NewEntry(steamID, req.AssetID, req.Description)
if errEntry != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrEntryCreate)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrEntryCreate)

return
}

if errSave := c.contests.ContestEntrySave(ctx, entry); errSave != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrEntrySave)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrEntrySave)

return
}
Expand All @@ -390,7 +390,7 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc {

contestEntryID, idErr := httphelper.GetUUIDParam(ctx, "contest_entry_id")
if idErr != nil {
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)

return
}
Expand All @@ -399,12 +399,12 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc {

if errContest := c.contests.ContestEntry(ctx, contestEntryID, &entry); errContest != nil {
if errors.Is(errContest, domain.ErrNoResult) {
httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrUnknownID)
httphelper.ResponseAPIErr(ctx, http.StatusNotFound, domain.ErrUnknownID)

return
}

httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)

slog.Error("Error getting contest entry for deletion", log.ErrAttr(errContest))

Expand All @@ -413,7 +413,7 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc {

// Only >=moderators or the entry author are allowed to delete entries.
if !(user.PermissionLevel >= domain.PModerator || user.SteamID == entry.SteamID) {
httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)
httphelper.ResponseAPIErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)

return
}
Expand All @@ -422,12 +422,12 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc {

if errContest := c.contests.ContestByID(ctx, entry.ContestID, &contest); errContest != nil {
if errors.Is(errContest, domain.ErrNoResult) {
httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrUnknownID)
httphelper.ResponseAPIErr(ctx, http.StatusNotFound, domain.ErrUnknownID)

return
}

httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)

slog.Error("Error getting contest", log.ErrAttr(errContest))

Expand All @@ -436,15 +436,15 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc {

// Only allow mods to delete entries from expired contests.
if user.SteamID == entry.SteamID && time.Since(contest.DateEnd) > 0 {
httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)
httphelper.ResponseAPIErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)

slog.Error("User tried to delete entry from expired contest")

return
}

if errDelete := c.contests.ContestEntryDelete(ctx, entry.ContestEntryID); errDelete != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)

slog.Error("Error deleting contest entry", log.ErrAttr(errDelete))

Expand Down
2 changes: 1 addition & 1 deletion internal/demo/demo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (h demoHandler) onAPIPostDemosQuery() gin.HandlerFunc {
return func(ctx *gin.Context) {
demos, errDemos := h.demos.GetDemos(ctx)
if errDemos != nil {
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
slog.Error("Failed to query demos", log.ErrAttr(errDemos))

return
Expand Down
Loading

0 comments on commit dc29d67

Please sign in to comment.