Skip to content

Commit d024853

Browse files
author
broccoli
committed
improved error handling, using existing schema
+ using existing AvatarInfo schema instead of redundant schema + improved error handling, so that the branding information is always being updated, even if its not possible to remove the old files. + added updated wire_gen.go file
1 parent 97d773f commit d024853

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

cmd/wire_gen.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controller_admin/siteinfo_controller.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,17 @@ func (sc *SiteInfoController) UpdateBranding(ctx *gin.Context) {
275275
if handler.BindAndCheck(ctx, req) {
276276
return
277277
}
278-
currentBranding, _ := sc.siteInfoService.GetSiteBranding(ctx)
279-
err := sc.siteInfoService.CleanUpRemovedBrandingFiles(ctx, req, currentBranding)
280-
if err != nil {
281-
log.Error(err)
278+
currentBranding, getBrandingErr := sc.siteInfoService.GetSiteBranding(ctx)
279+
if getBrandingErr == nil {
280+
cleanUpErr := sc.siteInfoService.CleanUpRemovedBrandingFiles(ctx, req, currentBranding)
281+
if cleanUpErr != nil {
282+
log.Errorf("failed to clean up removed branding file(s): %v", cleanUpErr)
283+
}
284+
} else {
285+
log.Errorf("failed to get current site branding: %v", getBrandingErr)
282286
}
283-
err = sc.siteInfoService.SaveSiteBranding(ctx, req)
284-
handler.HandleResponse(ctx, err, nil)
287+
saveErr := sc.siteInfoService.SaveSiteBranding(ctx, req)
288+
handler.HandleResponse(ctx, saveErr, nil)
285289
}
286290

287291
// UpdateSiteWrite update site write info

internal/service/content/user_service.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,7 @@ func (us *UserService) cleanUpRemovedAvatar(
380380
return
381381
}
382382

383-
var oldAvatar, newAvatar struct {
384-
Type string `json:"type"`
385-
Custom string `json:"custom"`
386-
}
383+
var oldAvatar, newAvatar schema.AvatarInfo
387384

388385
_ = json.Unmarshal([]byte(oldAvatarJSON), &oldAvatar)
389386
_ = json.Unmarshal([]byte(newAvatarJSON), &newAvatar)

internal/service/siteinfo/siteinfo_service.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package siteinfo
2222
import (
2323
"context"
2424
"encoding/json"
25+
errpkg "errors"
2526
"fmt"
2627
"strings"
2728

@@ -448,6 +449,7 @@ func (s *SiteInfoService) CleanUpRemovedBrandingFiles(
448449
newBranding *schema.SiteBrandingReq,
449450
currentBranding *schema.SiteBrandingResp,
450451
) error {
452+
var allErrors []error
451453
currentFiles := map[string]string{
452454
"logo": currentBranding.Logo,
453455
"mobile_logo": currentBranding.MobileLogo,
@@ -467,18 +469,21 @@ func (s *SiteInfoService) CleanUpRemovedBrandingFiles(
467469
if currentFile != "" && currentFile != newFile {
468470
fileRecord, err := s.fileRecordService.GetFileRecordByURL(ctx, currentFile)
469471
if err != nil {
470-
log.Error(err)
472+
allErrors = append(allErrors, err)
471473
continue
472474
}
473475
if fileRecord == nil {
474-
log.Error("could not fetch file record by url")
476+
err := errpkg.New("file record is nil for key " + key)
477+
allErrors = append(allErrors, err)
475478
continue
476479
}
477480
if err := s.fileRecordService.DeleteAndMoveFileRecord(ctx, fileRecord); err != nil {
478-
log.Error(err)
481+
allErrors = append(allErrors, err)
479482
}
480483
}
481484
}
482-
485+
if len(allErrors) > 0 {
486+
return errpkg.Join(allErrors...)
487+
}
483488
return nil
484489
}

0 commit comments

Comments
 (0)