From 1962ca4a827562378d3a9f82e96f20dafe399bdd Mon Sep 17 00:00:00 2001 From: youngjun827 Date: Sat, 18 Nov 2023 10:54:07 -0500 Subject: [PATCH] chore: consistent error handling --- .../thoughts-api/v1/handlers/bloggrp/bloggrp.go | 4 +++- .../thoughts-api/v1/handlers/bloggrp/route.go | 4 ++-- .../thoughts-api/v1/handlers/hackgrp/hackgrp.go | 3 ++- app/tooling/logfmt/main.go | 6 ++++-- app/tooling/thoughts-admin/main.go | 6 ++++-- business/data/dbmigrate/dbmigrate.go | 15 ++++++++++----- business/data/dbsql/pgx/pgx.go | 12 ++++++++---- business/web/v1/metrics/metrics.go | 9 ++++++--- business/web/v1/mid/errors.go | 6 ++++-- business/web/v1/mid/panics.go | 3 ++- foundation/validate/validate.go | 3 ++- foundation/web/request.go | 6 ++++-- foundation/web/web.go | 3 ++- 13 files changed, 53 insertions(+), 27 deletions(-) diff --git a/app/services/thoughts-api/v1/handlers/bloggrp/bloggrp.go b/app/services/thoughts-api/v1/handlers/bloggrp/bloggrp.go index ede2078..c13f919 100644 --- a/app/services/thoughts-api/v1/handlers/bloggrp/bloggrp.go +++ b/app/services/thoughts-api/v1/handlers/bloggrp/bloggrp.go @@ -23,7 +23,9 @@ func New(blog *blog.Core) *Handlers { func (h *Handlers) Create(ctx context.Context, w http.ResponseWriter, r *http.Request) error { var app AppNewBlog - if err := web.Decode(r, &app); err != nil { + + err := web.Decode(r, &app) + if err != nil { return response.NewError(err, http.StatusBadRequest) } diff --git a/app/services/thoughts-api/v1/handlers/bloggrp/route.go b/app/services/thoughts-api/v1/handlers/bloggrp/route.go index f6b53ec..9a77e8f 100644 --- a/app/services/thoughts-api/v1/handlers/bloggrp/route.go +++ b/app/services/thoughts-api/v1/handlers/bloggrp/route.go @@ -19,8 +19,8 @@ type Config struct { func Routes(app *web.App, cfg Config) { const version = "v1" - usrCore := blog.NewCore(cfg.Log, blogdb.NewStore(cfg.Log, cfg.DB)) + blgCore := blog.NewCore(cfg.Log, blogdb.NewStore(cfg.Log, cfg.DB)) - hdl := New(usrCore) + hdl := New(blgCore) app.Handle(http.MethodPost, version, "/blogs", hdl.Create) } \ No newline at end of file diff --git a/app/services/thoughts-api/v1/handlers/hackgrp/hackgrp.go b/app/services/thoughts-api/v1/handlers/hackgrp/hackgrp.go index 9c39795..3c5780e 100644 --- a/app/services/thoughts-api/v1/handlers/hackgrp/hackgrp.go +++ b/app/services/thoughts-api/v1/handlers/hackgrp/hackgrp.go @@ -12,7 +12,8 @@ import ( ) func Hack(ctx context.Context, w http.ResponseWriter, r *http.Request) error { - if n := rand.Intn(100) % 2; n == 0 { + n := rand.Intn(100) % 2 + if n == 0 { return response.NewError(errors.New("TRUST ERROR"), http.StatusBadRequest) } diff --git a/app/tooling/logfmt/main.go b/app/tooling/logfmt/main.go index 02a9202..a53b184 100644 --- a/app/tooling/logfmt/main.go +++ b/app/tooling/logfmt/main.go @@ -40,7 +40,8 @@ func main() { } traceID := "00000000-0000-0000-0000-000000000000" - if v, ok := m["trace_id"]; ok { + v, ok := m["trace_id"] + if ok { traceID = fmt.Sprintf("%v", v) } @@ -67,7 +68,8 @@ func main() { fmt.Println(out[:len(out)-2]) } - if err := scanner.Err(); err != nil { + err := scanner.Err() + if err != nil { log.Println(err) } } diff --git a/app/tooling/thoughts-admin/main.go b/app/tooling/thoughts-admin/main.go index cca7adc..452fcbd 100644 --- a/app/tooling/thoughts-admin/main.go +++ b/app/tooling/thoughts-admin/main.go @@ -64,7 +64,8 @@ func migrateSeed() error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - if err := dbmigrate.Migrate(ctx, db); err != nil { + err = dbmigrate.Migrate(ctx, db) + if err != nil { return fmt.Errorf("migrate database: %w", err) } @@ -72,7 +73,8 @@ func migrateSeed() error { // ------------------------------------------------------------------------- - if err := dbmigrate.Seed(ctx, db); err != nil { + err = dbmigrate.Seed(ctx, db) + if err != nil { return fmt.Errorf("seed database: %w", err) } diff --git a/business/data/dbmigrate/dbmigrate.go b/business/data/dbmigrate/dbmigrate.go index 0f5d86a..e2a79ab 100644 --- a/business/data/dbmigrate/dbmigrate.go +++ b/business/data/dbmigrate/dbmigrate.go @@ -24,7 +24,8 @@ var ( ) func Migrate(ctx context.Context, db *sqlx.DB) error { - if err := database.StatusCheck(ctx, db); err != nil { + err := database.StatusCheck(ctx, db) + if err != nil { return fmt.Errorf("status check database: %w", err) } @@ -38,7 +39,8 @@ func Migrate(ctx context.Context, db *sqlx.DB) error { } func Seed(ctx context.Context, db *sqlx.DB) (err error) { - if err := database.StatusCheck(ctx, db); err != nil { + err = database.StatusCheck(ctx, db) + if err != nil { return fmt.Errorf("status check database: %w", err) } @@ -48,7 +50,8 @@ func Seed(ctx context.Context, db *sqlx.DB) (err error) { } defer func() { - if errTx := tx.Rollback(); errTx != nil { + errTx := tx.Rollback() + if errTx != nil { if errors.Is(errTx, sql.ErrTxDone) { return } @@ -57,11 +60,13 @@ func Seed(ctx context.Context, db *sqlx.DB) (err error) { } }() - if _, err := tx.Exec(seedDoc); err != nil { + _, err = tx.Exec(seedDoc) + if err != nil { return fmt.Errorf("exec: %w", err) } - if err := tx.Commit(); err != nil { + err = tx.Commit() + if err != nil { return fmt.Errorf("commit: %w", err) } diff --git a/business/data/dbsql/pgx/pgx.go b/business/data/dbsql/pgx/pgx.go index a1c0c3b..760054d 100644 --- a/business/data/dbsql/pgx/pgx.go +++ b/business/data/dbsql/pgx/pgx.go @@ -72,7 +72,8 @@ func Open(cfg Config) (*sqlx.DB, error) { } func StatusCheck(ctx context.Context, db *sqlx.DB) error { - if _, ok := ctx.Deadline(); !ok { + _, ok := ctx.Deadline() + if !ok { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, time.Second) defer cancel() @@ -102,14 +103,17 @@ func StatusCheck(ctx context.Context, db *sqlx.DB) error { func NamedExecContext(ctx context.Context, log *logger.Logger, db sqlx.ExtContext, query string, data any) error { q := queryString(query, data) - if _, ok := data.(struct{}); ok { + _, ok := data.(struct{}) + if ok { log.Infoc(ctx, 5, "database.NamedExecContext", "query", q) } else { log.Infoc(ctx, 4, "database.NamedExecContext", "query", q) } - if _, err := sqlx.NamedExecContext(ctx, db, query, data); err != nil { - if pqerr, ok := err.(*pgconn.PgError); ok { + _, err := sqlx.NamedExecContext(ctx, db, query, data) + if err != nil { + pqerr, ok := err.(*pgconn.PgError) + if ok { switch pqerr.Code { case undefinedTable: return ErrUndefinedTable diff --git a/business/web/v1/metrics/metrics.go b/business/web/v1/metrics/metrics.go index 5b2aa5a..dc9d93f 100644 --- a/business/web/v1/metrics/metrics.go +++ b/business/web/v1/metrics/metrics.go @@ -38,7 +38,8 @@ func Set(ctx context.Context) context.Context { } func AddGoroutines(ctx context.Context) int64 { - if v, ok := ctx.Value(key).(*metrics); ok { + v, ok := ctx.Value(key).(*metrics) + if ok { if v.requests.Value()%100 == 0 { g := int64(runtime.NumGoroutine()) v.goroutines.Set(g) @@ -60,7 +61,8 @@ func AddRequests(ctx context.Context) int64 { } func AddErrors(ctx context.Context) int64 { - if v, ok := ctx.Value(key).(*metrics); ok { + v, ok := ctx.Value(key).(*metrics) + if ok { v.errors.Add(1) return v.errors.Value() } @@ -69,7 +71,8 @@ func AddErrors(ctx context.Context) int64 { } func AddPanics(ctx context.Context) int64 { - if v, ok := ctx.Value(key).(*metrics); ok { + v, ok := ctx.Value(key).(*metrics) + if ok { v.panics.Add(1) return v.panics.Value() } diff --git a/business/web/v1/mid/errors.go b/business/web/v1/mid/errors.go index 1c76b8d..4ddfd15 100644 --- a/business/web/v1/mid/errors.go +++ b/business/web/v1/mid/errors.go @@ -13,7 +13,8 @@ import ( func Errors(log *logger.Logger) web.Middleware { m := func(handler web.Handler) web.Handler { h := func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { - if err := handler(ctx, w, r); err != nil { + err := handler(ctx, w, r) + if err != nil { log.Error(ctx, "message", "msg", err) var er response.ErrorDocument @@ -43,7 +44,8 @@ func Errors(log *logger.Logger) web.Middleware { status = http.StatusInternalServerError } - if err := web.Respond(ctx, w, er, status); err != nil { + err := web.Respond(ctx, w, er, status) + if err != nil { return err } diff --git a/business/web/v1/mid/panics.go b/business/web/v1/mid/panics.go index 90825d1..ae56775 100644 --- a/business/web/v1/mid/panics.go +++ b/business/web/v1/mid/panics.go @@ -15,7 +15,8 @@ func Panics() web.Middleware { h := func(ctx context.Context, w http.ResponseWriter, r *http.Request) (err error) { defer func() { - if rec := recover(); rec != nil { + rec := recover() + if rec != nil { trace := debug.Stack() err = fmt.Errorf("PANIC [%v] TRACE[%s]", rec, string(trace)) metrics.AddPanics(ctx) diff --git a/foundation/validate/validate.go b/foundation/validate/validate.go index 35a4f55..4f5e168 100644 --- a/foundation/validate/validate.go +++ b/foundation/validate/validate.go @@ -33,7 +33,8 @@ func init() { } func Check(val any) error { - if err := validate.Struct(val); err != nil { + err := validate.Struct(val) + if err != nil { verrors, ok := err.(validator.ValidationErrors) if !ok { diff --git a/foundation/web/request.go b/foundation/web/request.go index 9b981bd..9598f59 100644 --- a/foundation/web/request.go +++ b/foundation/web/request.go @@ -13,12 +13,14 @@ type validator interface { func Decode(r *http.Request, val any) error { decoder := json.NewDecoder(r.Body) decoder.DisallowUnknownFields() - if err := decoder.Decode(val); err != nil { + err := decoder.Decode(val) + if err != nil { return fmt.Errorf("unable to decode payload: %w", err) } if v, ok := val.(validator); ok { - if err := v.Validate(); err != nil { + err := v.Validate() + if err != nil { return fmt.Errorf("unable to validate payload: %w", err) } } diff --git a/foundation/web/web.go b/foundation/web/web.go index 8dcbdfd..87e32d5 100644 --- a/foundation/web/web.go +++ b/foundation/web/web.go @@ -57,7 +57,8 @@ func (a *App) handle(method string, group string, path string, handler Handler) } ctx := SetValues(r.Context(), &v) - if err := handler(ctx, w, r); err != nil { + err := handler(ctx, w, r) + if err != nil { if validateShutdown(err) { a.SignalShutdown() return