diff --git a/api/district.go b/api/district.go index 95b938a5..687ce2a8 100644 --- a/api/district.go +++ b/api/district.go @@ -33,12 +33,16 @@ func apiGetDistrict(w http.ResponseWriter, r *http.Request) { } lng, err := strconv.ParseFloat(lngStr, 64) if err != nil { - renderShim(w, r, errRender(fmt.Errorf("Failed to parse lng as float: %w", err))) + if err := renderShim(w, r, errRender(fmt.Errorf("Failed to parse lng as float: %w", err))); err != nil { + http.Error(w, fmt.Sprintf("render shim: %v", err), http.StatusInternalServerError) + } return } org, err := platform.DistrictForLocation(r.Context(), lng, lat) if err != nil { - renderShim(w, r, errRender(fmt.Errorf("Failed to get district: %w", err))) + if err := renderShim(w, r, errRender(fmt.Errorf("Failed to get district: %w", err))); err != nil { + http.Error(w, fmt.Sprintf("render shim: %v", err), http.StatusInternalServerError) + } return } if org == nil { @@ -52,7 +56,9 @@ func apiGetDistrict(w http.ResponseWriter, r *http.Request) { Website: org.Website.GetOr(""), } if err := renderShim(w, r, d); err != nil { - renderShim(w, r, errRender(err)) + if err := renderShim(w, r, errRender(err)); err != nil { + http.Error(w, fmt.Sprintf("render shim: %v", err), http.StatusInternalServerError) + } } } diff --git a/api/handler.go b/api/handler.go index 484df0aa..70ba2772 100644 --- a/api/handler.go +++ b/api/handler.go @@ -351,7 +351,7 @@ func handlerFormPost[RequestType any, ResponseType any](f handlerFunctionPostFor respondErrorStatus(w, nhttp.NewError("failed to marshal json: %w", err)) return } - w.Write(body) + lint.Write(w, body) } } func parseRequest[RequestType any](r *http.Request) (*RequestType, *nhttp.ErrorWithStatus) { diff --git a/api/image.go b/api/image.go index 68bedf32..c3d52b8b 100644 --- a/api/image.go +++ b/api/image.go @@ -2,6 +2,7 @@ package api import ( "encoding/json" + "fmt" "io/ioutil" "net/http" @@ -48,7 +49,9 @@ func apiImagePost(w http.ResponseWriter, r *http.Request, u platform.User) { } err = platform.NoteImageCreate(ctx, u, setter) if err != nil { - renderShim(w, r, errRender(err)) + if err := renderShim(w, r, errRender(err)); err != nil { + http.Error(w, fmt.Sprintf("render shim: %v", err), http.StatusInternalServerError) + } return } w.WriteHeader(http.StatusAccepted) @@ -75,7 +78,9 @@ func apiImageContentPost(w http.ResponseWriter, r *http.Request, u platform.User } err = file.ImageFileFromReader(file.CollectionImageRaw, imageUUID, r.Body) if err != nil { - renderShim(w, r, errRender(err)) + if err := renderShim(w, r, errRender(err)); err != nil { + http.Error(w, fmt.Sprintf("render shim: %v", err), http.StatusInternalServerError) + } return } w.WriteHeader(http.StatusOK) diff --git a/api/voipms.go b/api/voipms.go index c9b5218f..57ce6b35 100644 --- a/api/voipms.go +++ b/api/voipms.go @@ -3,11 +3,11 @@ package api import ( "context" "encoding/json" - "fmt" "io/ioutil" "net/http" //"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" ) @@ -101,5 +101,5 @@ func voipmsTextPost(w http.ResponseWriter, r *http.Request) { log.Error().Err(err).Msg("failed to handle VoIP.ms incoming text") } }() - fmt.Fprintf(w, "ok") + lint.Fprintf(w, "ok") } diff --git a/middleware/logger.go b/middleware/logger.go index 57f55820..694c2f29 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -106,16 +106,16 @@ func (l *DefaultLogFormatter) NewLogEntry(r *http.Request) LogEntry { reqID := GetReqID(r.Context()) if reqID != "" { - cW(entry.buf, useColor, nYellow, "[%s] ", reqID) + _ = cW(entry.buf, useColor, nYellow, "[%s] ", reqID) } - cW(entry.buf, useColor, nCyan, "\"") - cW(entry.buf, useColor, bMagenta, "%s ", r.Method) + _ = cW(entry.buf, useColor, nCyan, "\"") + _ = cW(entry.buf, useColor, bMagenta, "%s ", r.Method) scheme := "http" if r.TLS != nil { scheme = "https" } - cW(entry.buf, useColor, nCyan, "%s://%s%s %s\" ", scheme, r.Host, r.RequestURI, r.Proto) + _ = cW(entry.buf, useColor, nCyan, "%s://%s%s %s\" ", scheme, r.Host, r.RequestURI, r.Proto) entry.buf.WriteString("from ") entry.buf.WriteString(r.RemoteAddr) @@ -134,26 +134,26 @@ type defaultLogEntry struct { func (l *defaultLogEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) { switch { case status < 200: - cW(l.buf, l.useColor, bBlue, "%03d", status) + _ = cW(l.buf, l.useColor, bBlue, "%03d", status) case status < 300: - cW(l.buf, l.useColor, bGreen, "%03d", status) + _ = cW(l.buf, l.useColor, bGreen, "%03d", status) case status < 400: - cW(l.buf, l.useColor, bCyan, "%03d", status) + _ = cW(l.buf, l.useColor, bCyan, "%03d", status) case status < 500: - cW(l.buf, l.useColor, bYellow, "%03d", status) + _ = cW(l.buf, l.useColor, bYellow, "%03d", status) default: - cW(l.buf, l.useColor, bRed, "%03d", status) + _ = cW(l.buf, l.useColor, bRed, "%03d", status) } - cW(l.buf, l.useColor, bBlue, " %dB", bytes) + _ = cW(l.buf, l.useColor, bBlue, " %dB", bytes) l.buf.WriteString(" in ") if elapsed < 500*time.Millisecond { - cW(l.buf, l.useColor, nGreen, "%s", elapsed) + _ = cW(l.buf, l.useColor, nGreen, "%s", elapsed) } else if elapsed < 5*time.Second { - cW(l.buf, l.useColor, nYellow, "%s", elapsed) + _ = cW(l.buf, l.useColor, nYellow, "%s", elapsed) } else { - cW(l.buf, l.useColor, nRed, "%s", elapsed) + _ = cW(l.buf, l.useColor, nRed, "%s", elapsed) } l.Logger.Print(l.buf.String()) diff --git a/platform/csv/flyover.go b/platform/csv/flyover.go index 13f2f02c..9cc5719e 100644 --- a/platform/csv/flyover.go +++ b/platform/csv/flyover.go @@ -317,7 +317,9 @@ func insertPoollistRow(ctx context.Context, txn bob.Tx, file *models.FileuploadF 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 } } diff --git a/platform/csv/pool.go b/platform/csv/pool.go index efc30f8c..dafc3f5d 100644 --- a/platform/csv/pool.go +++ b/platform/csv/pool.go @@ -153,11 +153,15 @@ func geocodePool(ctx context.Context, txn bob.Tx, client *stadia.StadiaMaps, job } geo, err := geocode.GeocodeStructured(ctx, job.org, a) if err != nil { - addError(ctx, txn, job.csv, pool.LineNumber, 0, err.Error()) + lint.LogOnErrCtx(func(ctx context.Context) error { + return addError(ctx, txn, job.csv, pool.LineNumber, 0, err.Error()) + }, ctx, "add geocode error") return nil } if geo.Address.Location == nil { - addError(ctx, txn, job.csv, pool.LineNumber, 0, "nil location from geocoding") + lint.LogOnErrCtx(func(ctx context.Context) error { + return addError(ctx, txn, job.csv, pool.LineNumber, 0, "nil location from geocoding") + }, ctx, "add nil location error") return nil } geom_query := geom.PostgisPointQuery(*geo.Address.Location) @@ -187,7 +191,9 @@ func parseCSVPoollist(ctx context.Context, txn bob.Tx, f *models.FileuploadFile, header_types, header_names := parseHeaders(h) missing_headers := missingRequiredHeaders(header_types) for _, mh := range missing_headers { - errorMissingHeader(ctx, txn, c, mh) + lint.LogOnErrCtx(func(ctx context.Context) error { + return errorMissingHeader(ctx, txn, c, mh) + }, ctx, "error missing header") err = f.Update(ctx, txn, &models.FileuploadFileSetter{ Status: omit.From(enums.FileuploadFilestatustypeError), }) @@ -253,7 +259,9 @@ func parseCSVPoollist(ctx context.Context, txn bob.Tx, f *models.FileuploadFile, // This type of spreadsheet normally has '123 Main Str' parts := strings.SplitN(col, " ", 2) if len(parts) != 2 { - addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not a house number and street. It needs to be in the form '123 main'", col)) + lint.LogOnErrCtx(func(ctx context.Context) error { + return addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not a house number and street. It needs to be in the form '123 main'", col)) + }, ctx, "add address parse error") continue } setter.AddressNumber = omit.From(parts[0]) @@ -268,7 +276,9 @@ func parseCSVPoollist(ctx context.Context, txn bob.Tx, f *models.FileuploadFile, } err := condition.Scan(col_translated) if err != nil { - 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", col, 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", col, poolConditionValidValues())) + }, ctx, "add pool condition error") setter.Condition = omit.From(enums.PoolconditiontypeUnknown) continue } @@ -280,7 +290,9 @@ func parseCSVPoollist(ctx context.Context, txn bob.Tx, f *models.FileuploadFile, case headerPoolPropertyOwnerPhone: phone, err := text.ParsePhoneNumber(col) if err != nil { - addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not a phone number that we recognize. Ideally it should be of the form '+12223334444'", col)) + lint.LogOnErrCtx(func(ctx context.Context) error { + return addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not a phone number that we recognize. Ideally it should be of the form '+12223334444'", col)) + }, ctx, "add phone number error") continue } err = text.EnsureInDB(ctx, txn, *phone) @@ -292,14 +304,18 @@ func parseCSVPoollist(ctx context.Context, txn bob.Tx, f *models.FileuploadFile, case headerPoolResidentOwned: boolValue, err := parseBool(col) if err != nil { - addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not something that we recognize as a true/false value. Please use either 'true' or 'false'", col)) + lint.LogOnErrCtx(func(ctx context.Context) error { + return addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not something that we recognize as a true/false value. Please use either 'true' or 'false'", col)) + }, ctx, "add bool error") continue } setter.ResidentOwned = omitnull.From(boolValue) case headerPoolResidentPhone: phone, err := text.ParsePhoneNumber(col) if err != nil { - addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not a phone number that we recognize. Ideally it should be of the form '+12223334444'", col)) + lint.LogOnErrCtx(func(ctx context.Context) error { + return addError(ctx, txn, c, int32(line_number), int32(i), fmt.Sprintf("'%s' is not a phone number that we recognize. Ideally it should be of the form '+12223334444'", col)) + }, ctx, "add phone number error") continue } err = text.EnsureInDB(ctx, txn, *phone) diff --git a/platform/publicreport.go b/platform/publicreport.go index 4b5fe3ef..c2674487 100644 --- a/platform/publicreport.go +++ b/platform/publicreport.go @@ -11,6 +11,7 @@ import ( "github.com/Gleipnir-Technology/jet/postgres" "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" tablepublic "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/table" modelpublicreport "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model" @@ -109,7 +110,7 @@ func PublicReportMessageCreate(ctx context.Context, user User, public_id, messag if err != nil { return nil, fmt.Errorf("create txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") report, err := querypublicreport.ReportFromPublicID(ctx, db.PGInstance.PGXPool, public_id) if err != nil { @@ -128,7 +129,9 @@ func PublicReportMessageCreate(ctx context.Context, user User, public_id, messag if err != nil { return nil, fmt.Errorf("send text: %w", err) } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return nil, fmt.Errorf("commit: %w", err) + } //log.Debug().Int32("msg_id", *msg_id).Msg("Created text.ReportMessage") return msg_id, nil } else if report.ReporterEmail != "" { @@ -136,7 +139,9 @@ func PublicReportMessageCreate(ctx context.Context, user User, public_id, messag if err != nil { return nil, fmt.Errorf("send email: %w", err) } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return nil, fmt.Errorf("commit: %w", err) + } return msg_id, nil } else { log.Debug().Str("public_id", public_id).Msg("contacting via email") @@ -149,7 +154,7 @@ func PublicReportUpdateCompliance(ctx context.Context, public_id string, report_ if err != nil { return fmt.Errorf("create txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") report, err := querypublicreport.ReportFromPublicID(ctx, db.PGInstance.PGXPool, public_id) if err != nil { return fmt.Errorf("query report existence: %w", err) @@ -213,7 +218,9 @@ func PublicReportUpdateCompliance(ctx context.Context, public_id string, report_ return fmt.Errorf("update location: %w", err) } } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return fmt.Errorf("commit: %w", err) + } return nil } func PublicReportReporterUpdated(ctx context.Context, org_id int32, report_id string) { @@ -240,7 +247,7 @@ func PublicReportImageCreate(ctx context.Context, public_id string, images []Ima if err != nil { return fmt.Errorf("create txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") report, err := querypublicreport.ReportFromPublicID(ctx, db.PGInstance.PGXPool, public_id) if err != nil { @@ -264,7 +271,9 @@ func PublicReportImageCreate(ctx context.Context, public_id string, images []Ima } log.Info().Int("len", len(images)).Msg("saved uploaded images") } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return fmt.Errorf("commit: %w", err) + } return nil } func PublicReportNuisanceCreate(ctx context.Context, setter_report modelpublicreport.Report, setter_nuisance modelpublicreport.Nuisance, location types.Location, address Address, images []ImageUpload) (modelpublicreport.Report, error) { @@ -303,7 +312,7 @@ func publicReportCreate(ctx context.Context, setter_report modelpublicreport.Rep if err != nil { return result, fmt.Errorf("create txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") if setter_report.PublicID == "" { public_id, err := GenerateReportID() @@ -354,7 +363,9 @@ func publicReportCreate(ctx context.Context, setter_report modelpublicreport.Rep if location != nil { l := *location if l.Latitude != 0 && l.Longitude != 0 { - publicReportUpdateLocation(ctx, txn, result.ID, l) + lint.LogOnErrCtx(func(ctx context.Context) error { + return publicReportUpdateLocation(ctx, txn, result.ID, l) + }, ctx, "update location") } } log.Info().Str("public_id", setter_report.PublicID).Int32("id", result.ID).Msg("Created base report") @@ -403,7 +414,9 @@ func publicReportCreate(ctx context.Context, setter_report modelpublicreport.Rep log.Debug().Int32("id", comm.ID).Msg("inserted new communication") } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return result, fmt.Errorf("commit: %w", err) + } event.Created( event.TypeRMOPublicReport, diff --git a/platform/publicreport_notification.go b/platform/publicreport_notification.go index d983cf82..f60dfb38 100644 --- a/platform/publicreport_notification.go +++ b/platform/publicreport_notification.go @@ -5,6 +5,7 @@ import ( "fmt" "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/report" "github.com/Gleipnir-Technology/nidus-sync/platform/types" @@ -26,7 +27,7 @@ func PublicreportNotificationCreate(ctx context.Context, pn PublicreportNotifica if err != nil { return fmt.Errorf("begin txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") rep, err := models.PublicreportReports.Query( models.SelectWhere.PublicreportReports.PublicID.EQ(pn.ReportID), ).One(ctx, db.PGInstance.BobDB) @@ -66,7 +67,9 @@ func PublicreportNotificationCreate(ctx context.Context, pn PublicreportNotifica } } } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return fmt.Errorf("commit: %w", err) + } PublicReportReporterUpdated(ctx, rep.OrganizationID, pn.ReportID) return nil } diff --git a/platform/review.go b/platform/review.go index 53062558..6eaab28d 100644 --- a/platform/review.go +++ b/platform/review.go @@ -12,6 +12,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" nhttp "github.com/Gleipnir-Technology/nidus-sync/http" "github.com/Gleipnir-Technology/nidus-sync/platform/event" @@ -31,7 +32,7 @@ func ReviewPoolCreate(ctx context.Context, user User, task_id int32, status stri if err != nil { return 0, nhttp.NewError("start txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") review_task, err := models.ReviewTasks.Query( models.SelectWhere.ReviewTasks.ID.EQ(task_id), models.SelectWhere.ReviewTasks.OrganizationID.EQ(user.Organization.ID), @@ -70,7 +71,9 @@ func ReviewPoolCreate(ctx context.Context, user User, task_id int32, status stri return 0, err } event.Updated(event.TypeReviewTask, user.Organization.ID, strconv.Itoa(int(review_task.ID))) - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return 0, nhttp.NewError("commit: %w", err) + } log.Info().Int32("id", review_task.ID).Str("status", status).Msg("review completed") return review_task.ID, err } diff --git a/platform/signal.go b/platform/signal.go index 1bff3a7d..e8ba5db1 100644 --- a/platform/signal.go +++ b/platform/signal.go @@ -11,6 +11,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" modelpublicreport "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model" tablepublicreport "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/table" @@ -69,7 +70,7 @@ func SignalCreateFromPool(ctx context.Context, txn db.Ex, user User, site_id int // Create a lead from the given signal and site func SignalCreateFromPublicreport(ctx context.Context, user User, report_id string) (*int32, error) { txn, err := db.BeginTxn(ctx) - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") if err != nil { return nil, fmt.Errorf("start transaction: %w", err) } @@ -166,7 +167,9 @@ func SignalCreateFromPublicreport(ctx context.Context, user User, report_id stri return nil, fmt.Errorf("failed to update report %d: %w", report_id, err) } event.Created(event.TypeSignal, user.Organization.ID, strconv.Itoa(int(signal.ID))) - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return nil, fmt.Errorf("commit: %w", err) + } return &signal.ID, nil } diff --git a/platform/text/send.go b/platform/text/send.go index 53c66641..10f7e371 100644 --- a/platform/text/send.go +++ b/platform/text/send.go @@ -10,6 +10,7 @@ import ( "github.com/Gleipnir-Technology/nidus-sync/config" "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/platform/background" "github.com/Gleipnir-Technology/nidus-sync/platform/event" @@ -105,7 +106,7 @@ func sendTextComplete(ctx context.Context, job *models.CommsTextJob) error { if err != nil { return fmt.Errorf("begin tx: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") dst, err := ParsePhoneNumber(job.Destination) if err != nil { return fmt.Errorf("parse phone: %w", err) @@ -134,7 +135,9 @@ func sendTextComplete(ctx context.Context, job *models.CommsTextJob) error { //case enums.CommsPhonestatustypeOkToSend: // allow to drop through case enums.CommsPhonestatustypeStopped: - resendInitialText(ctx, txn, *dst) + lint.LogOnErrCtx(func(ctx context.Context) error { + return resendInitialText(ctx, txn, *dst) + }, ctx, "resend initial text") return nil } text_log, err := sendTextDirect(ctx, txn, origin, job.Destination, job.Content, true, false) @@ -179,7 +182,9 @@ func sendTextComplete(ctx context.Context, job *models.CommsTextJob) error { } else { log.Debug().Msg("no report info on text") } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return fmt.Errorf("commit: %w", err) + } return nil } diff --git a/platform/text/text.go b/platform/text/text.go index a8716ae7..8766a302 100644 --- a/platform/text/text.go +++ b/platform/text/text.go @@ -9,6 +9,7 @@ import ( "github.com/Gleipnir-Technology/nidus-sync/config" "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/platform/background" "github.com/Gleipnir-Technology/nidus-sync/platform/event" @@ -32,7 +33,7 @@ func HandleTextMessage(ctx context.Context, source string, destination string, c if err != nil { return fmt.Errorf("start txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") status, err := phoneStatus(ctx, *src) if err != nil { @@ -60,7 +61,9 @@ func HandleTextMessage(ctx context.Context, source string, destination string, c if err != nil { return fmt.Errorf("text respond: %w", err) } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return fmt.Errorf("commit: %w", err) + } return err } @@ -69,7 +72,7 @@ func respondText(ctx context.Context, log_id int32) error { if err != nil { return fmt.Errorf("begin tx: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") l, err := models.FindCommsTextLog(ctx, txn, log_id) if err != nil { return fmt.Errorf("find comms: %w", err) @@ -98,7 +101,9 @@ func respondText(ctx context.Context, log_id int32) error { if err != nil { return fmt.Errorf("send response: %w", err) } - handleWaitingTextJobs(ctx, *src) + lint.LogOnErrCtx(func(ctx context.Context) error { + return handleWaitingTextJobs(ctx, *src) + }, ctx, "handle waiting text jobs") // We don't handle 'stop' here because we allow them to say 'stop' at any time, regardless of // phone status. //case "stop": @@ -118,7 +123,9 @@ func respondText(ctx context.Context, log_id int32) error { if err != nil { log.Error().Err(err).Msg("Failed to send unsubscribe acknowledgement.") } - setPhoneStatus(ctx, txn, *src, enums.CommsPhonestatustypeStopped) + lint.LogOnErrCtx(func(ctx context.Context) error { + return setPhoneStatus(ctx, txn, *src, enums.CommsPhonestatustypeStopped) + }, ctx, "set phone status") return nil case "reset conversation": err = handleResetConversation(ctx, txn, *src) @@ -172,12 +179,14 @@ func respondTextLLM(ctx context.Context, src types.E164) error { if err != nil { return fmt.Errorf("start txn: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") _, err = sendTextDirect(ctx, txn, enums.CommsTextoriginLLM, src.PhoneString(), next_message.Content, true, false) if err != nil { return fmt.Errorf("Failed to send response text: %w", err) } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return fmt.Errorf("commit: %w", err) + } return nil } diff --git a/platform/upload.go b/platform/upload.go index e40878c0..a79dd7ad 100644 --- a/platform/upload.go +++ b/platform/upload.go @@ -11,6 +11,7 @@ import ( "github.com/Gleipnir-Technology/bob/dialect/psql/sm" "github.com/Gleipnir-Technology/bob/dialect/psql/um" "github.com/Gleipnir-Technology/nidus-sync/db" + "github.com/Gleipnir-Technology/nidus-sync/lint" "github.com/Gleipnir-Technology/nidus-sync/db/enums" "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/Gleipnir-Technology/nidus-sync/platform/background" @@ -86,7 +87,7 @@ func NewUpload(ctx context.Context, u User, upload file.Upload, t enums.Fileuplo if err != nil { return nil, fmt.Errorf("Failed to begin transaction: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") file, err := models.FileuploadFiles.Insert(&models.FileuploadFileSetter{ ContentType: omit.From(upload.ContentType), @@ -117,7 +118,9 @@ func NewUpload(ctx context.Context, u User, upload file.Upload, t enums.Fileuplo if err != nil { return nil, fmt.Errorf("background job create: %w", err) } - txn.Commit(ctx) + if err := txn.Commit(ctx); err != nil { + return nil, fmt.Errorf("commit: %w", err) + } return &file.ID, nil } func UploadCommit(ctx context.Context, org Organization, file_id int32, committer User) error { @@ -125,7 +128,7 @@ func UploadCommit(ctx context.Context, org Organization, file_id int32, committe if err != nil { return fmt.Errorf("Failed to begin transaction: %w", err) } - defer txn.Rollback(ctx) + defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback") _, err = psql.Update( um.Table(models.FileuploadFiles.Alias()), diff --git a/resource/publicreport_nuisance.go b/resource/publicreport_nuisance.go index 12f808ab..1a9a6539 100644 --- a/resource/publicreport_nuisance.go +++ b/resource/publicreport_nuisance.go @@ -10,6 +10,7 @@ import ( //tablepublicreport "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/table" //querypublicreport "github.com/Gleipnir-Technology/nidus-sync/db/query/publicreport" "github.com/Gleipnir-Technology/nidus-sync/html" + "github.com/Gleipnir-Technology/nidus-sync/lint" nhttp "github.com/Gleipnir-Technology/nidus-sync/http" "github.com/Gleipnir-Technology/nidus-sync/platform" "github.com/Gleipnir-Technology/nidus-sync/platform/types" @@ -150,7 +151,11 @@ func (res *nuisanceR) byID(ctx context.Context, r *http.Request, is_public bool) if err != nil { return nil, nhttp.NewError("get report: %w", err) } - populateDistrictURI(&report.PublicReport, res.router) - populateReportURI(&report.PublicReport, res.router, is_public) + lint.LogOnErr(func() error { + return populateDistrictURI(&report.PublicReport, res.router) + }, "populate district URI") + lint.LogOnErr(func() error { + return populateReportURI(&report.PublicReport, res.router, is_public) + }, "populate report URI") return report, nil } diff --git a/resource/publicreport_water.go b/resource/publicreport_water.go index 8bd2742b..2193c229 100644 --- a/resource/publicreport_water.go +++ b/resource/publicreport_water.go @@ -9,6 +9,7 @@ import ( //tablepublicreport "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/table" //querypublicreport "github.com/Gleipnir-Technology/nidus-sync/db/query/publicreport" "github.com/Gleipnir-Technology/nidus-sync/html" + "github.com/Gleipnir-Technology/nidus-sync/lint" nhttp "github.com/Gleipnir-Technology/nidus-sync/http" "github.com/Gleipnir-Technology/nidus-sync/platform" "github.com/Gleipnir-Technology/nidus-sync/platform/types" @@ -149,7 +150,11 @@ func (res *waterR) byID(ctx context.Context, r *http.Request, is_public bool) (* if err != nil { return nil, nhttp.NewError("get report: %w", err) } - populateDistrictURI(&report.PublicReport, res.router) - populateReportURI(&report.PublicReport, res.router, is_public) + lint.LogOnErr(func() error { + return populateDistrictURI(&report.PublicReport, res.router) + }, "populate district URI") + lint.LogOnErr(func() error { + return populateReportURI(&report.PublicReport, res.router, is_public) + }, "populate report URI") return report, nil }