From c13c4e90b669b773360c011ec55d88433b220233 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 30 Dec 2023 04:27:08 +0100 Subject: [PATCH] don't ignore errors in tx.Rollback() --- .golangci.yml | 2 -- backend/db/export.go | 2 +- backend/db/invites.go | 8 +++++++- backend/db/member.go | 8 +++++++- backend/main.go | 3 ++- backend/routes/v1/auth/discord.go | 8 +++++++- backend/routes/v1/auth/fedi_mastodon.go | 8 +++++++- backend/routes/v1/auth/fedi_misskey.go | 8 +++++++- backend/routes/v1/auth/google.go | 8 +++++++- backend/routes/v1/auth/tokens.go | 9 ++++++++- backend/routes/v1/auth/tumblr.go | 8 +++++++- backend/routes/v1/member/create_member.go | 8 +++++++- backend/routes/v1/member/patch_member.go | 8 +++++++- backend/routes/v1/mod/resolve_report.go | 8 +++++++- backend/routes/v1/user/delete_user.go | 9 ++++++++- backend/routes/v1/user/flags.go | 15 ++++++++++++-- backend/routes/v1/user/patch_user.go | 8 +++++++- backend/server/server.go | 24 +++++++++++------------ scripts/cleandb/main.go | 10 +++++++++- scripts/seeddb/main.go | 9 ++++++++- scripts/snowflakes/main.go | 8 +++++++- 21 files changed, 145 insertions(+), 34 deletions(-) delete mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml deleted file mode 100644 index 29471e1..0000000 --- a/.golangci.yml +++ /dev/null @@ -1,2 +0,0 @@ -run: - issues-exit-code: 0 # lol diff --git a/backend/db/export.go b/backend/db/export.go index 6141aac..5095445 100644 --- a/backend/db/export.go +++ b/backend/db/export.go @@ -79,7 +79,7 @@ func (db *DB) CreateExport(ctx context.Context, userID xid.ID, filename string, return de, errors.Wrap(err, "building query") } - pgxscan.Get(ctx, db, &de, sql, args...) + err = pgxscan.Get(ctx, db, &de, sql, args...) if err != nil { return de, errors.Wrap(err, "executing sql") } diff --git a/backend/db/invites.go b/backend/db/invites.go index d56662e..d13ab0e 100644 --- a/backend/db/invites.go +++ b/backend/db/invites.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "time" + "codeberg.org/pronounscc/pronouns.cc/backend/log" "emperror.dev/errors" "github.com/georgysavva/scany/v2/pgxscan" "github.com/jackc/pgx/v5" @@ -43,7 +44,12 @@ func (db *DB) CreateInvite(ctx context.Context, userID xid.ID) (i Invite, err er if err != nil { return i, errors.Wrap(err, "beginning transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() var maxInvites, inviteCount int err = tx.QueryRow(ctx, "SELECT max_invites FROM users WHERE id = $1", userID).Scan(&maxInvites) diff --git a/backend/db/member.go b/backend/db/member.go index 6eb444c..a50c431 100644 --- a/backend/db/member.go +++ b/backend/db/member.go @@ -7,6 +7,7 @@ import ( "time" "codeberg.org/pronounscc/pronouns.cc/backend/common" + "codeberg.org/pronounscc/pronouns.cc/backend/log" "emperror.dev/errors" "github.com/Masterminds/squirrel" "github.com/georgysavva/scany/v2/pgxscan" @@ -287,7 +288,12 @@ func (db *DB) RerollMemberSID(ctx context.Context, userID, memberID xid.ID) (new if err != nil { return "", errors.Wrap(err, "beginning transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() sql, args, err := sq.Update("members"). Set("sid", squirrel.Expr("find_free_member_sid()")). diff --git a/backend/main.go b/backend/main.go index d9efd98..c17679e 100644 --- a/backend/main.go +++ b/backend/main.go @@ -26,7 +26,8 @@ var Command = &cli.Command{ func run(c *cli.Context) error { // initialize sentry if dsn := os.Getenv("SENTRY_DSN"); dsn != "" { - sentry.Init(sentry.ClientOptions{ + // We don't need to check the error here--it's fine if no DSN is set. + _ = sentry.Init(sentry.ClientOptions{ Dsn: dsn, Debug: os.Getenv("DEBUG") == "true", Release: server.Tag, diff --git a/backend/routes/v1/auth/discord.go b/backend/routes/v1/auth/discord.go index 2084b52..b8a72f3 100644 --- a/backend/routes/v1/auth/discord.go +++ b/backend/routes/v1/auth/discord.go @@ -11,6 +11,7 @@ import ( "emperror.dev/errors" "github.com/bwmarrin/discordgo" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" "github.com/mediocregopher/radix/v4" "github.com/rs/xid" "golang.org/x/oauth2" @@ -291,7 +292,12 @@ func (s *Server) discordSignup(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.Wrap(err, "beginning transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() du := new(discordgo.User) err = s.DB.GetJSON(ctx, "discord:"+req.Ticket, &du) diff --git a/backend/routes/v1/auth/fedi_mastodon.go b/backend/routes/v1/auth/fedi_mastodon.go index bb07958..f69a2b3 100644 --- a/backend/routes/v1/auth/fedi_mastodon.go +++ b/backend/routes/v1/auth/fedi_mastodon.go @@ -11,6 +11,7 @@ import ( "codeberg.org/pronounscc/pronouns.cc/backend/server" "emperror.dev/errors" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" "github.com/mediocregopher/radix/v4" "github.com/rs/xid" ) @@ -319,7 +320,12 @@ func (s *Server) mastodonSignup(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.Wrap(err, "beginning transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() mu := new(partialMastodonAccount) err = s.DB.GetJSON(ctx, "mastodon:"+req.Ticket, &mu) diff --git a/backend/routes/v1/auth/fedi_misskey.go b/backend/routes/v1/auth/fedi_misskey.go index ebf6e22..d162f77 100644 --- a/backend/routes/v1/auth/fedi_misskey.go +++ b/backend/routes/v1/auth/fedi_misskey.go @@ -12,6 +12,7 @@ import ( "codeberg.org/pronounscc/pronouns.cc/backend/server" "emperror.dev/errors" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" "github.com/mediocregopher/radix/v4" "github.com/rs/xid" ) @@ -247,7 +248,12 @@ func (s *Server) misskeySignup(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.Wrap(err, "beginning transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() mu := new(partialMisskeyAccount) err = s.DB.GetJSON(ctx, "misskey:"+req.Ticket, &mu) diff --git a/backend/routes/v1/auth/google.go b/backend/routes/v1/auth/google.go index 026a6d1..7904889 100644 --- a/backend/routes/v1/auth/google.go +++ b/backend/routes/v1/auth/google.go @@ -10,6 +10,7 @@ import ( "codeberg.org/pronounscc/pronouns.cc/backend/server" "emperror.dev/errors" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" "github.com/mediocregopher/radix/v4" "github.com/rs/xid" "golang.org/x/oauth2" @@ -294,7 +295,12 @@ func (s *Server) googleSignup(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.Wrap(err, "beginning transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() gu := new(partialGoogleUser) err = s.DB.GetJSON(ctx, "google:"+req.Ticket, &gu) diff --git a/backend/routes/v1/auth/tokens.go b/backend/routes/v1/auth/tokens.go index e48662d..0f8b8b8 100644 --- a/backend/routes/v1/auth/tokens.go +++ b/backend/routes/v1/auth/tokens.go @@ -5,9 +5,11 @@ import ( "time" "codeberg.org/pronounscc/pronouns.cc/backend/db" + "codeberg.org/pronounscc/pronouns.cc/backend/log" "codeberg.org/pronounscc/pronouns.cc/backend/server" "emperror.dev/errors" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" "github.com/rs/xid" ) @@ -63,7 +65,12 @@ func (s *Server) deleteToken(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.Wrap(err, "beginning transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() err = s.DB.InvalidateAllTokens(ctx, tx, claims.UserID) if err != nil { diff --git a/backend/routes/v1/auth/tumblr.go b/backend/routes/v1/auth/tumblr.go index 52dcdec..3463c9d 100644 --- a/backend/routes/v1/auth/tumblr.go +++ b/backend/routes/v1/auth/tumblr.go @@ -12,6 +12,7 @@ import ( "codeberg.org/pronounscc/pronouns.cc/backend/server" "emperror.dev/errors" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" "github.com/mediocregopher/radix/v4" "github.com/rs/xid" "golang.org/x/oauth2" @@ -327,7 +328,12 @@ func (s *Server) tumblrSignup(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.Wrap(err, "beginning transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() tui := new(tumblrUserInfo) err = s.DB.GetJSON(ctx, "tumblr:"+req.Ticket, &tui) diff --git a/backend/routes/v1/member/create_member.go b/backend/routes/v1/member/create_member.go index c94915a..288aafc 100644 --- a/backend/routes/v1/member/create_member.go +++ b/backend/routes/v1/member/create_member.go @@ -11,6 +11,7 @@ import ( "codeberg.org/pronounscc/pronouns.cc/backend/server" "emperror.dev/errors" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" ) type CreateMemberRequest struct { @@ -119,7 +120,12 @@ func (s *Server) createMember(w http.ResponseWriter, r *http.Request) (err error if err != nil { return errors.Wrap(err, "starting transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() m, err := s.DB.CreateMember(ctx, tx, claims.UserID, cmr.Name, cmr.DisplayName, cmr.Bio, cmr.Links) if err != nil { diff --git a/backend/routes/v1/member/patch_member.go b/backend/routes/v1/member/patch_member.go index f8cee31..c26cf37 100644 --- a/backend/routes/v1/member/patch_member.go +++ b/backend/routes/v1/member/patch_member.go @@ -13,6 +13,7 @@ import ( "emperror.dev/errors" "github.com/go-chi/chi/v5" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" "github.com/rs/xid" ) @@ -246,7 +247,12 @@ func (s *Server) patchMember(w http.ResponseWriter, r *http.Request) error { log.Errorf("creating transaction: %v", err) return errors.Wrap(err, "creating transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() m, err = s.DB.UpdateMember(ctx, tx, m.ID, req.Name, req.DisplayName, req.Bio, req.Unlisted, req.Links, avatarHash) if err != nil { diff --git a/backend/routes/v1/mod/resolve_report.go b/backend/routes/v1/mod/resolve_report.go index 77e4b8d..c36cb86 100644 --- a/backend/routes/v1/mod/resolve_report.go +++ b/backend/routes/v1/mod/resolve_report.go @@ -10,6 +10,7 @@ import ( "emperror.dev/errors" "github.com/go-chi/chi/v5" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" ) type resolveReportRequest struct { @@ -43,7 +44,12 @@ func (s *Server) resolveReport(w http.ResponseWriter, r *http.Request) error { log.Errorf("creating transaction: %v", err) return errors.Wrap(err, "creating transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() report, err := s.DB.Report(ctx, tx, id) if err != nil { diff --git a/backend/routes/v1/user/delete_user.go b/backend/routes/v1/user/delete_user.go index 1523978..1132f05 100644 --- a/backend/routes/v1/user/delete_user.go +++ b/backend/routes/v1/user/delete_user.go @@ -3,9 +3,11 @@ package user import ( "net/http" + "codeberg.org/pronounscc/pronouns.cc/backend/log" "codeberg.org/pronounscc/pronouns.cc/backend/server" "emperror.dev/errors" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" ) func (s *Server) deleteUser(w http.ResponseWriter, r *http.Request) error { @@ -20,7 +22,12 @@ func (s *Server) deleteUser(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.Wrap(err, "creating transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() err = s.DB.DeleteUser(ctx, tx, claims.UserID, true, "") if err != nil { diff --git a/backend/routes/v1/user/flags.go b/backend/routes/v1/user/flags.go index b9c3ced..c7b6ad7 100644 --- a/backend/routes/v1/user/flags.go +++ b/backend/routes/v1/user/flags.go @@ -13,6 +13,7 @@ import ( "emperror.dev/errors" "github.com/go-chi/chi/v5" "github.com/go-chi/render" + "github.com/jackc/pgx/v5" "github.com/rs/xid" ) @@ -80,7 +81,12 @@ func (s *Server) postUserFlag(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.Wrap(err, "starting transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() flag, err := s.DB.CreateFlag(ctx, tx, claims.UserID, req.Name, req.Description) if err != nil { @@ -192,7 +198,12 @@ func (s *Server) patchUserFlag(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.Wrap(err, "beginning transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() flag, err = s.DB.EditFlag(ctx, tx, flag.ID, req.Name, req.Description, nil) if err != nil { diff --git a/backend/routes/v1/user/patch_user.go b/backend/routes/v1/user/patch_user.go index ca489ce..6e4f6ab 100644 --- a/backend/routes/v1/user/patch_user.go +++ b/backend/routes/v1/user/patch_user.go @@ -12,6 +12,7 @@ import ( "emperror.dev/errors" "github.com/go-chi/render" "github.com/google/uuid" + "github.com/jackc/pgx/v5" "github.com/rs/xid" ) @@ -221,7 +222,12 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error { log.Errorf("creating transaction: %v", err) return errors.Wrap(err, "creating transaction") } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() // update username if req.Username != nil && *req.Username != u.Username { diff --git a/backend/server/server.go b/backend/server/server.go index bfc785d..30cd552 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -100,23 +100,23 @@ func New() (*Server, error) { // set scopes // users - rateLimiter.Scope("GET", "/users/*", 60) - rateLimiter.Scope("PATCH", "/users/@me", 10) + _ = rateLimiter.Scope("GET", "/users/*", 60) + _ = rateLimiter.Scope("PATCH", "/users/@me", 10) // members - rateLimiter.Scope("GET", "/users/*/members", 60) - rateLimiter.Scope("GET", "/users/*/members/*", 60) + _ = rateLimiter.Scope("GET", "/users/*/members", 60) + _ = rateLimiter.Scope("GET", "/users/*/members/*", 60) - rateLimiter.Scope("POST", "/members", 10) - rateLimiter.Scope("GET", "/members/*", 60) - rateLimiter.Scope("PATCH", "/members/*", 20) - rateLimiter.Scope("DELETE", "/members/*", 5) + _ = rateLimiter.Scope("POST", "/members", 10) + _ = rateLimiter.Scope("GET", "/members/*", 60) + _ = rateLimiter.Scope("PATCH", "/members/*", 20) + _ = rateLimiter.Scope("DELETE", "/members/*", 5) // auth - rateLimiter.Scope("*", "/auth/*", 20) - rateLimiter.Scope("*", "/auth/tokens", 10) - rateLimiter.Scope("*", "/auth/invites", 10) - rateLimiter.Scope("POST", "/auth/discord/*", 10) + _ = rateLimiter.Scope("*", "/auth/*", 20) + _ = rateLimiter.Scope("*", "/auth/tokens", 10) + _ = rateLimiter.Scope("*", "/auth/invites", 10) + _ = rateLimiter.Scope("POST", "/auth/discord/*", 10) s.Router.Use(rateLimiter.Handler()) diff --git a/scripts/cleandb/main.go b/scripts/cleandb/main.go index 317951b..9049861 100644 --- a/scripts/cleandb/main.go +++ b/scripts/cleandb/main.go @@ -2,12 +2,15 @@ package cleandb import ( "context" + "errors" "fmt" "os" "time" dbpkg "codeberg.org/pronounscc/pronouns.cc/backend/db" + "codeberg.org/pronounscc/pronouns.cc/backend/log" "github.com/georgysavva/scany/v2/pgxscan" + "github.com/jackc/pgx/v5" "github.com/joho/godotenv" "github.com/rs/xid" "github.com/urfave/cli/v2" @@ -77,7 +80,12 @@ func run(c *cli.Context) error { fmt.Printf("error starting transaction: %v\n", err) return err } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() inactiveUsers, err := db.InactiveUsers(ctx, tx) if err != nil { diff --git a/scripts/seeddb/main.go b/scripts/seeddb/main.go index c2e9f26..bbe20f5 100644 --- a/scripts/seeddb/main.go +++ b/scripts/seeddb/main.go @@ -1,10 +1,12 @@ package seeddb import ( + "errors" "log" "os" "codeberg.org/pronounscc/pronouns.cc/backend/db" + "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "github.com/joho/godotenv" "github.com/urfave/cli/v2" @@ -82,7 +84,12 @@ func run(c *cli.Context) error { log.Println("error beginning transaction:", err) return err } - defer tx.Rollback(ctx) + defer func() { + err := tx.Rollback(ctx) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Println("error rolling back transaction:", err) + } + }() for i, su := range seed.Users { u, err := pg.CreateUser(ctx, tx, su.Username) diff --git a/scripts/snowflakes/main.go b/scripts/snowflakes/main.go index 382035a..e88fe26 100644 --- a/scripts/snowflakes/main.go +++ b/scripts/snowflakes/main.go @@ -1,6 +1,7 @@ package snowflakes import ( + "errors" "os" "time" @@ -39,7 +40,12 @@ func run(c *cli.Context) error { log.Error("creating transaction:", err) return err } - defer tx.Rollback(c.Context) + defer func() { + err := tx.Rollback(c.Context) + if err != nil && !errors.Is(err, pgx.ErrTxClosed) { + log.Error("rolling back transaction:", err) + } + }() var userIDs []xid.ID err = pgxscan.Select(c.Context, conn, &userIDs, "SELECT id FROM users WHERE snowflake_id IS NULL")