diff --git a/api/handler.go b/api/handler.go index 1c5522b1..484df0aa 100644 --- a/api/handler.go +++ b/api/handler.go @@ -300,7 +300,7 @@ func handlerJSONPost[RequestType any, ResponseType any](f handlerFunctionPost[Re respondErrorStatus(w, nhttp.NewError("failed to marshal json: %w", err)) return } - w.Write(body) + lint.Write(w, body) } } @@ -323,7 +323,7 @@ func handlerJSONPut[RequestType any, ResponseType any](f handlerFunctionPost[Req respondErrorStatus(w, nhttp.NewError("failed to marshal json: %w", err)) return } - w.Write(body) + lint.Write(w, body) } } func handlerFormPost[RequestType any, ResponseType any](f handlerFunctionPostFormMultipart[RequestType, ResponseType]) http.HandlerFunc { diff --git a/api/twilio.go b/api/twilio.go index 63066f7a..9a4621d3 100644 --- a/api/twilio.go +++ b/api/twilio.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/Gleipnir-Technology/nidus-sync/config" + "github.com/Gleipnir-Technology/nidus-sync/lint" "github.com/Gleipnir-Technology/nidus-sync/platform/text" "github.com/rs/zerolog/log" "github.com/twilio/twilio-go/twiml" @@ -49,7 +50,7 @@ func splitPhoneSource(s string) (string, string) { func twilioMessagePost(w http.ResponseWriter, r *http.Request) { message_sid := r.PostFormValue("MessageSid") log.Info().Str("sid", message_sid).Msg("Twilio Message POST") - fmt.Fprintf(w, "") + lint.Fprintf(w, "") } func twilioCallPost(w http.ResponseWriter, r *http.Request) { called := r.PostFormValue("Called") @@ -93,7 +94,7 @@ func twilioCallPost(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) } w.Header().Set("Content-Type", "text/xml") - fmt.Fprintf(w, "%s", twimlResult) + lint.Fprintf(w, "%s", twimlResult) } func twilioCallStatusPost(w http.ResponseWriter, r *http.Request) { @@ -108,7 +109,7 @@ func twilioCallStatusPost(w http.ResponseWriter, r *http.Request) { caller_name := r.PostFormValue("CallerName") parent_call_sid := r.PostFormValue("ParentCallSid") log.Info().Str("call_sid", call_sid).Str("account_sid", account_sid).Str("from", from).Str("to", to).Str("call_status", call_status).Str("api_version", api_version).Str("direction", direction).Str("forwarded_from", forwarded_from).Str("caller_name", caller_name).Str("parent_call_sid", parent_call_sid) - fmt.Fprintf(w, "") + lint.Fprintf(w, "") } func twilioTextPost(w http.ResponseWriter, r *http.Request) { message_sid := r.PostFormValue("MessageSid") @@ -147,12 +148,12 @@ func twilioTextPost(w http.ResponseWriter, r *http.Request) { } }() w.Header().Set("Content-Type", "text/xml") - fmt.Fprintf(w, "%s", twiml) + lint.Fprintf(w, "%s", twiml) } func twilioTextStatusPost(w http.ResponseWriter, r *http.Request) { message_sid := r.PostFormValue("MessageSid") message_status := r.PostFormValue("MessageStatus") log.Info().Str("sid", message_sid).Str("status", message_status).Msg("Updated message status") text.UpdateMessageStatus(message_sid, message_status) - fmt.Fprintf(w, "") + lint.Fprintf(w, "") } diff --git a/platform/compliance.go b/platform/compliance.go index 7ce0ab89..3ea2a406 100644 --- a/platform/compliance.go +++ b/platform/compliance.go @@ -10,6 +10,7 @@ import ( "github.com/Gleipnir-Technology/bob/dialect/psql" "github.com/Gleipnir-Technology/bob/dialect/psql/sm" "github.com/Gleipnir-Technology/nidus-sync/db" + "github.com/Gleipnir-Technology/nidus-sync/lint" modelpublic "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model" "github.com/Gleipnir-Technology/nidus-sync/db/models" querypublic "github.com/Gleipnir-Technology/nidus-sync/db/query/public" @@ -25,7 +26,7 @@ func ComplianceRequestMailerCreate(ctx context.Context, user User, site_id int64 if err != nil { return 0, fmt.Errorf("start txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") site, err := querypublic.SiteFromIDForOrg(ctx, txn, site_id, int64(user.Organization.ID)) if err != nil { return 0, fmt.Errorf("find site: %w", err) diff --git a/platform/csv/flyover.go b/platform/csv/flyover.go index 9ba9478f..13f2f02c 100644 --- a/platform/csv/flyover.go +++ b/platform/csv/flyover.go @@ -14,6 +14,7 @@ import ( "github.com/Gleipnir-Technology/bob/dialect/psql/um" "github.com/Gleipnir-Technology/nidus-sync/db" "github.com/Gleipnir-Technology/nidus-sync/db/enums" + "github.com/Gleipnir-Technology/nidus-sync/lint" "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/Gleipnir-Technology/nidus-sync/h3utils" "github.com/Gleipnir-Technology/nidus-sync/platform/file" @@ -191,19 +192,25 @@ func insertFlyover(ctx context.Context, txn bob.Tx, file *models.FileuploadFile, if err == nil { setter.Condition = omit.From(condition) } else { - addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not a pool condition that we recognize. It should be one of %s", value, poolConditionValidValues())) + lint.LogOnErrCtx(func(ctx context.Context) error { + return addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not a pool condition that we recognize. It should be one of %s", value, poolConditionValidValues())) + }, ctx, "add pool condition error") continue } case headerFlyoverLatitude: lat, err = strconv.ParseFloat(value, 10) if err != nil { - addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not decimal value", value)) + lint.LogOnErrCtx(func(ctx context.Context) error { + return addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not decimal value", value)) + }, ctx, "add lat error") continue } case headerFlyoverLongitude: lng, err = strconv.ParseFloat(value, 10) if err != nil { - addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not decimal value", value)) + lint.LogOnErrCtx(func(ctx context.Context) error { + return addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not decimal value", value)) + }, ctx, "add lng error") continue } } diff --git a/platform/lead.go b/platform/lead.go index 8ba7453f..84b87cc5 100644 --- a/platform/lead.go +++ b/platform/lead.go @@ -9,6 +9,7 @@ import ( "github.com/Gleipnir-Technology/bob/dialect/psql" "github.com/Gleipnir-Technology/bob/dialect/psql/sm" "github.com/Gleipnir-Technology/nidus-sync/db" + "github.com/Gleipnir-Technology/nidus-sync/lint" "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model" "github.com/Gleipnir-Technology/nidus-sync/db/models" query "github.com/Gleipnir-Technology/nidus-sync/db/query/public" @@ -23,13 +24,15 @@ func LeadCreate(ctx context.Context, user User, signal_id int32, site_id int32, if err != nil { return model.Lead{}, fmt.Errorf("start transaction: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") lead, err := leadCreate(ctx, txn, user, signal_id, site_id, pool_location) if err != nil { return model.Lead{}, fmt.Errorf("inner leadcreate: %w", err) } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return model.Lead{}, fmt.Errorf("commit: %w", err) + } return lead, nil } diff --git a/platform/note.go b/platform/note.go index 31baa1fc..efcd04ea 100644 --- a/platform/note.go +++ b/platform/note.go @@ -6,6 +6,7 @@ import ( "strconv" "github.com/Gleipnir-Technology/nidus-sync/db" + "github.com/Gleipnir-Technology/nidus-sync/lint" "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/Gleipnir-Technology/nidus-sync/platform/event" //"github.com/google/uuid" @@ -17,7 +18,7 @@ func NoteAudioCreate(ctx context.Context, user User, setter models.NoteAudioSett if err != nil { return fmt.Errorf("create txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") note_audio, err := models.NoteAudios.Insert(&setter).One(ctx, txn) if err != nil { @@ -27,7 +28,9 @@ func NoteAudioCreate(ctx context.Context, user User, setter models.NoteAudioSett } } event.Created(event.TypeNoteAudio, user.Organization.ID, strconv.Itoa(int(note_audio.ID))) - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return fmt.Errorf("commit: %w", err) + } return nil } @@ -37,7 +40,7 @@ func NoteImageCreate(ctx context.Context, user User, setter models.NoteImageSett if err != nil { return fmt.Errorf("create txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") note_image, err := models.NoteImages.Insert(&setter).One(ctx, db.PGInstance.BobDB) if err != nil { // Just ignore this failure, it means we already have this content @@ -46,7 +49,9 @@ func NoteImageCreate(ctx context.Context, user User, setter models.NoteImageSett } } event.Created(event.TypeNoteImage, user.Organization.ID, strconv.Itoa(int(note_image.ID))) - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return fmt.Errorf("commit: %w", err) + } return err } diff --git a/rmo/report.go b/rmo/report.go index ca9ad8db..4499c222 100644 --- a/rmo/report.go +++ b/rmo/report.go @@ -10,6 +10,7 @@ import ( "github.com/Gleipnir-Technology/bob/dialect/psql" "github.com/Gleipnir-Technology/bob/dialect/psql/sm" "github.com/Gleipnir-Technology/nidus-sync/db" + "github.com/Gleipnir-Technology/nidus-sync/lint" //"github.com/gorilla/mux" "github.com/stephenafamo/scan" //"github.com/rs/zerolog/log" @@ -78,7 +79,7 @@ func getReportSuggestion(w http.ResponseWriter, r *http.Request) { return } w.Header().Set("Content-Type", "application/json") - w.Write(jsonBody) + lint.Write(w, jsonBody) } func partialSearchParam(p string) string {