A continuation of HomeBox the inventory and organization system built for the Home User
fix: a few random crash fixes (#1628)
Harrison Conlin13e8d24fd53b
diff --git a/backend/internal/data/repo/repo_group.go b/backend/internal/data/repo/repo_group.goindex ce30ed7b..0ae9f480 100644--- a/backend/internal/data/repo/repo_group.go+++ b/backend/internal/data/repo/repo_group.go@@ -28,7 +28,7 @@ type GroupRepository struct {attachments *AttachmentRepo}-func NewGroupRepository(db *ent.Client) *GroupRepository {+func NewGroupRepository(db *ent.Client, attachments *AttachmentRepo) *GroupRepository {gmap := func(g *ent.Group) Group {return Group{ID: g.ID,@@ -52,6 +52,7 @@ func NewGroupRepository(db *ent.Client) *GroupRepository {db: db,groupMapper: gmap,invitationMapper: imap,+ attachments: attachments,}}@@ -332,19 +333,25 @@ func (r *GroupRepository) GroupDelete(ctx context.Context, id uuid.UUID) error {itm, err := tx.Entity.Query().Where(entity.HasGroupWith(group.ID(id))).- WithGroup().WithAttachments().All(ctx)if err != nil {+ if rerr := tx.Rollback(); rerr != nil {+ log.Error().Err(rerr).Msg("failed to rollback transaction")+ }return err}- // Delete all attachments (and their files) before deleting the entities+ // Keep attachment row operations on the same transaction.+ attachments := *r.attachments+ attachments.db = tx.Client()++ // Delete all attachments (and their files) before deleting the entities.for _, it := range itm {for _, att := range it.Edges.Attachments {- if err := r.attachments.Delete(ctx, id, att.ID); err != nil {- log.Err(err).Str("attachment_id", att.ID.String()).Msg("failed to delete attachment during entity deletion")- // Continue with other attachments even if one fails+ if err := attachments.Delete(ctx, id, att.ID); err != nil {+ log.Err(err).Str("attachment_id", att.ID.String()).Msg("failed to delete attachment during group deletion")+ // Continue with other attachments even if one fails.}}}diff --git a/backend/internal/data/repo/repo_item_attachments.go b/backend/internal/data/repo/repo_item_attachments.goindex db8f5914..6c7841f9 100644--- a/backend/internal/data/repo/repo_item_attachments.go+++ b/backend/internal/data/repo/repo_item_attachments.go@@ -322,7 +322,7 @@ func (r *AttachmentRepo) Create(ctx context.Context, itemID uuid.UUID, doc ItemCif typ == attachment.TypePhoto && primary {bldr = bldr.SetPrimary(true)- err := r.db.Attachment.Update().+ err := tx.Attachment.Update().Where(attachment.HasEntityWith(entity.ID(itemID)),attachment.IDNEQ(bldrId),diff --git a/backend/internal/data/repo/repos_all.go b/backend/internal/data/repo/repos_all.goindex 31ab59d1..225b7239 100644--- a/backend/internal/data/repo/repos_all.go+++ b/backend/internal/data/repo/repos_all.go@@ -31,7 +31,7 @@ func New(db *ent.Client, bus *eventbus.EventBus, storage config.Storage, pubSubCAuthTokens: &TokenRepository{db},PasswordResetTokens: &PasswordResetTokenRepository{db},APIKeys: NewAPIKeyRepository(db),- Groups: NewGroupRepository(db),+ Groups: NewGroupRepository(db, attachments),Entities: &EntityRepository{db, bus, attachments},EntityTypes: &EntityTypeRepository{db, bus},EntityTemplates: &EntityTemplatesRepository{db, bus},diff --git a/backend/internal/sys/validate/errors.go b/backend/internal/sys/validate/errors.goindex 09fdf2cc..653e97fa 100644--- a/backend/internal/sys/validate/errors.go+++ b/backend/internal/sys/validate/errors.go@@ -3,6 +3,7 @@ package validateimport ("encoding/json""errors"+ "net/http")type UnauthorizedError struct {@@ -59,7 +60,10 @@ func NewRequestError(err error, status int) error {}func (err *RequestError) Error() string {- return err.Err.Error()+ if err.Err != nil {+ return err.Err.Error()+ }+ return http.StatusText(err.Status)}// IsRequestError checks if an error of type RequestError exists.diff --git a/backend/internal/web/mid/errors.go b/backend/internal/web/mid/errors.goindex 8cbea5a1..a5d8d59d 100644--- a/backend/internal/web/mid/errors.go+++ b/backend/internal/web/mid/errors.go@@ -25,7 +25,7 @@ func Errors(log zerolog.Logger) errchain.ErrorHandler {var resp ErrorResponsevar code int- traceID := r.Context().Value(middleware.RequestIDKey).(string)+ traceID, _ := r.Context().Value(middleware.RequestIDKey).(string)log.Err(err).Ctx(r.Context()).Stack().diff --git a/backend/internal/web/mid/logger.go b/backend/internal/web/mid/logger.goindex 24a6371a..5bace847 100644--- a/backend/internal/web/mid/logger.go+++ b/backend/internal/web/mid/logger.go@@ -20,6 +20,17 @@ func (s *spy) WriteHeader(status int) {s.ResponseWriter.WriteHeader(status)}+// Write implicitly sends a 200 status if the handler hasn't called+// WriteHeader yet, matching how http.ResponseWriter behaves. Without this+// override, a handler that writes a body without an explicit WriteHeader+// call leaves s.status at its zero value.+func (s *spy) Write(b []byte) (int, error) {+ if s.status == 0 {+ s.status = http.StatusOK+ }++ return s.ResponseWriter.Write(b)+}// Unwrap exposes the wrapped writer to http.ResponseController, which walks// the Unwrap chain to reach the underlying connection. Without it a handler// cannot clear the server's write deadline, and long streaming downloads get@@ -39,7 +50,7 @@ func (s *spy) Hijack() (net.Conn, *bufio.ReadWriter, error) {func Logger(l zerolog.Logger) func(http.Handler) http.Handler {return func(h http.Handler) http.Handler {return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- reqID := r.Context().Value(middleware.RequestIDKey).(string)+ reqID, _ := r.Context().Value(middleware.RequestIDKey).(string)l.Info().Ctx(r.Context()).Str("method", r.Method).Str("path", r.URL.Path).Str("rid", reqID).Msg("request received")