From b9cf98eee807db5f8cea994fc33b397d927d84e4 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Mon, 9 Feb 2026 22:43:32 +0000 Subject: [PATCH] Consistently log internal errors --- platform/report/error_with_code.go | 6 ++++++ platform/report/report_nuisance.go | 12 ++++++------ platform/report/report_pool.go | 8 ++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/platform/report/error_with_code.go b/platform/report/error_with_code.go index 7bad9e84..a1733da0 100644 --- a/platform/report/error_with_code.go +++ b/platform/report/error_with_code.go @@ -2,6 +2,8 @@ package report import ( "fmt" + + "github.com/rs/zerolog/log" ) type ErrorWithCode struct { @@ -17,6 +19,10 @@ func (e *ErrorWithCode) Error() string { return e.message } +func newInternalError(err error, format string, args ...any) *ErrorWithCode { + log.Error().Err(err).Str("format", format).Msg("internal server error") + return newErrorWithCode("internal-error", format, args...) +} func newErrorWithCode(code string, format string, args ...any) *ErrorWithCode { if len(args) > 0 { return &ErrorWithCode{ diff --git a/platform/report/report_nuisance.go b/platform/report/report_nuisance.go index 1e0986fa..18c24e17 100644 --- a/platform/report/report_nuisance.go +++ b/platform/report/report_nuisance.go @@ -42,7 +42,7 @@ func (sr Nuisance) addNotificationEmail(ctx context.Context, txn bob.Tx, email s } _, err := models.PublicreportNotifyPhoneNuisances.Insert(&setter).Exec(ctx, txn) if err != nil { - return newErrorWithCode("internal-error", "Failed to save new notification email row") + return newInternalError(err, "Failed to save new notification email row") } return nil } @@ -56,7 +56,7 @@ func (sr Nuisance) addNotificationPhone(ctx context.Context, txn bob.Tx, phone t } _, err = models.PublicreportNotifyPhoneNuisances.Insert(&setter).Exec(ctx, txn) if err != nil { - return newErrorWithCode("internal-error", "Failed to save new notification phone row") + return newInternalError(err, "Failed to save new notification phone row") } return nil } @@ -87,7 +87,7 @@ func (sr Nuisance) updateReportCol(ctx context.Context, txn bob.Tx, setter *mode err := sr.row.Update(ctx, txn, setter) if err != nil { log.Error().Err(err).Str("public_id", sr.publicReportID).Int32("report_id", sr.id).Msg("Failed to update report") - return newErrorWithCode("internal-error", "Failed to update nuisance report in the database") + return newInternalError(err, "Failed to update nuisance report in the database") } return nil } @@ -108,11 +108,11 @@ func (sr Nuisance) updateReporterPhone(ctx context.Context, txn bob.Tx, phone te um.Where(psql.Quote("public_id").EQ(psql.Arg(sr.publicReportID))), ).Exec(ctx, txn) if err != nil { - return newErrorWithCode("internal-error", "Failed to update report: %w", err) + return newInternalError(err, "Failed to update report: %w", err) } rowcount, err := result.RowsAffected() if err != nil { - return newErrorWithCode("internal-error", "Failed to get rows affected: %w", err) + return newInternalError(err, "Failed to get rows affected: %w", err) } if rowcount != 1 { log.Warn().Str("public_report_id", sr.publicReportID).Msg("updated more than one row, which is a programmer error") @@ -123,7 +123,7 @@ func (sr Nuisance) updateReporterPhone(ctx context.Context, txn bob.Tx, phone te func newNuisance(ctx context.Context, public_id string, report_id int32) (Nuisance, *ErrorWithCode) { row, err := models.FindPublicreportNuisance(ctx, db.PGInstance.BobDB, report_id) if err != nil { - return Nuisance{}, newErrorWithCode("internal-error", "Failed to find nuisance report %d: %w", public_id, err) + return Nuisance{}, newInternalError(err, "Failed to find nuisance report %d: %w", public_id, err) } return Nuisance{ id: report_id, diff --git a/platform/report/report_pool.go b/platform/report/report_pool.go index 518dd362..ffd4e03f 100644 --- a/platform/report/report_pool.go +++ b/platform/report/report_pool.go @@ -40,7 +40,7 @@ func (sr Pool) addNotificationEmail(ctx context.Context, txn bob.Tx, email strin _, err := models.PublicreportNotifyEmailPools.Insert(&setter).Exec(ctx, txn) if err != nil { log.Error().Err(err).Msg("Failed to save new notification email row") - return newErrorWithCode("internal-error", "Failed to save new notification email row") + return newInternalError(err, "Failed to save new notification email row") } return nil } @@ -54,7 +54,7 @@ func (sr Pool) addNotificationPhone(ctx context.Context, txn bob.Tx, phone text. _, err := models.PublicreportNotifyPhonePools.Insert(&setter).Exec(ctx, txn) if err != nil { log.Error().Err(err).Msg("Failed to save new notification phone row") - return newErrorWithCode("internal-error", "Failed to save new notification phone row") + return newInternalError(err, "Failed to save new notification phone row") } return nil } @@ -96,7 +96,7 @@ func (sr Pool) updateReportCol(ctx context.Context, txn bob.Tx, setter *models.P err := sr.row.Update(ctx, txn, setter) if err != nil { log.Error().Err(err).Str("public_id", sr.publicReportID).Int32("report_id", sr.id).Msg("Failed to update report") - return newErrorWithCode("internal-error", "Failed to update pool report in the database") + return newInternalError(err, "Failed to update pool report in the database") } return nil } @@ -109,7 +109,7 @@ func newPool(ctx context.Context, public_id string, report_id int32) (Pool, *Err row, err := models.FindPublicreportPool(ctx, db.PGInstance.BobDB, report_id) if err != nil { log.Error().Err(err).Msg("Failed to find pool report") - return Pool{}, newErrorWithCode("internal-error", "Failed to find pool report %d: %w", public_id, err) + return Pool{}, newInternalError(err, "Failed to find pool report %d: %w", public_id, err) } return Pool{ id: report_id,