Get rid of loggerFatalError

This commit is contained in:
Brian Picciano 2022-05-05 19:33:40 -06:00
parent 24e9541e02
commit 0277c02767
6 changed files with 24 additions and 27 deletions

View File

@ -118,7 +118,7 @@ func New(params Params) (API, error) {
err := a.srv.Serve(l) err := a.srv.Serve(l)
if err != nil && !errors.Is(err, http.ErrServerClosed) { if err != nil && !errors.Is(err, http.ErrServerClosed) {
ctx := mctx.Annotate(context.Background(), a.params) ctx := mctx.Annotate(context.Background(), a.params)
params.Logger.Fatal(ctx, fmt.Sprintf("%s: %v", "serving http server", err)) params.Logger.Fatal(ctx, "serving http server", err)
} }
}() }()

View File

@ -3,7 +3,6 @@ package main
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"path" "path"
@ -14,10 +13,6 @@ import (
"github.com/tilinna/clock" "github.com/tilinna/clock"
) )
func loggerFatalErr(ctx context.Context, logger *mlog.Logger, descr string, err error) {
logger.Fatal(ctx, fmt.Sprintf("%s: %v", descr, err))
}
func main() { func main() {
ctx := context.Background() ctx := context.Background()
cfg := cfg.New() cfg := cfg.New()
@ -42,7 +37,7 @@ func main() {
defer logger.Info(ctx, "process exiting") defer logger.Info(ctx, "process exiting")
if err != nil { if err != nil {
loggerFatalErr(ctx, logger, "initializing", err) logger.Fatal(ctx, "initializing", err)
} }
clock := clock.Realtime() clock := clock.Realtime()
@ -57,7 +52,7 @@ func main() {
mlStore, err := mailinglist.NewStore(path.Join(*dataDir, "mailinglist.sqlite3")) mlStore, err := mailinglist.NewStore(path.Join(*dataDir, "mailinglist.sqlite3"))
if err != nil { if err != nil {
loggerFatalErr(ctx, logger, "initializing mailing list storage", err) logger.Fatal(ctx, "initializing mailing list storage", err)
} }
defer mlStore.Close() defer mlStore.Close()
@ -72,13 +67,15 @@ func main() {
ctx = mctx.Annotate(ctx, "subCmd", subCmd) ctx = mctx.Annotate(ctx, "subCmd", subCmd)
switch subCmd { switch subCmd {
case "list": case "list":
for it := mlStore.GetAll(); ; { for it := mlStore.GetAll(); ; {
email, err := it() email, err := it()
if errors.Is(err, io.EOF) { if errors.Is(err, io.EOF) {
break break
} else if err != nil { } else if err != nil {
loggerFatalErr(ctx, logger, "retrieving next email", err) logger.Fatal(ctx, "retrieving next email", err)
} }
ctx := mctx.Annotate(context.Background(), ctx := mctx.Annotate(context.Background(),
@ -94,21 +91,24 @@ func main() {
title := cfg.String("title", "", "Title of the post which was published") title := cfg.String("title", "", "Title of the post which was published")
url := cfg.String("url", "", "URL of the post which was published") url := cfg.String("url", "", "URL of the post which was published")
cfg.Init(ctx)
if err := cfg.Init(ctx); err != nil {
logger.Fatal(ctx, "initializing", err)
}
if *title == "" { if *title == "" {
logger.Fatal(ctx, "-title is required") logger.FatalString(ctx, "-title is required")
} else if *url == "" { } else if *url == "" {
logger.Fatal(ctx, "-url is required") logger.FatalString(ctx, "-url is required")
} }
err := ml.Publish(*title, *url) err := ml.Publish(*title, *url)
if err != nil { if err != nil {
loggerFatalErr(ctx, logger, "publishing", err) logger.Fatal(ctx, "publishing", err)
} }
default: default:
logger.Fatal(ctx, "invalid sub-command, must be list|publish") logger.FatalString(ctx, "invalid sub-command, must be list|publish")
} }
} }

View File

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"fmt"
"os" "os"
"os/signal" "os/signal"
"path" "path"
@ -20,10 +19,6 @@ import (
"github.com/tilinna/clock" "github.com/tilinna/clock"
) )
func loggerFatalErr(ctx context.Context, logger *mlog.Logger, descr string, err error) {
logger.Fatal(ctx, fmt.Sprintf("%s: %v", descr, err))
}
func main() { func main() {
ctx := context.Background() ctx := context.Background()
@ -64,7 +59,7 @@ func main() {
defer logger.Info(ctx, "process exiting") defer logger.Info(ctx, "process exiting")
if err != nil { if err != nil {
loggerFatalErr(ctx, logger, "initializing", err) logger.Fatal(ctx, "initializing", err)
} }
ctx = mctx.Annotate(ctx, ctx = mctx.Annotate(ctx,
@ -95,7 +90,7 @@ func main() {
mlStore, err := mailinglist.NewStore(path.Join(*dataDir, "mailinglist.sqlite3")) mlStore, err := mailinglist.NewStore(path.Join(*dataDir, "mailinglist.sqlite3"))
if err != nil { if err != nil {
loggerFatalErr(ctx, logger, "initializing mailing list storage", err) logger.Fatal(ctx, "initializing mailing list storage", err)
} }
defer mlStore.Close() defer mlStore.Close()
@ -112,7 +107,7 @@ func main() {
) )
if err != nil { if err != nil {
loggerFatalErr(ctx, logger, "initializing redis pool", err) logger.Fatal(ctx, "initializing redis pool", err)
} }
defer redis.Close() defer redis.Close()
@ -123,7 +118,7 @@ func main() {
MaxMessages: *chatGlobalRoomMaxMsgs, MaxMessages: *chatGlobalRoomMaxMsgs,
}) })
if err != nil { if err != nil {
loggerFatalErr(ctx, logger, "initializing global chat room", err) logger.Fatal(ctx, "initializing global chat room", err)
} }
defer chatGlobalRoom.Close() defer chatGlobalRoom.Close()
@ -138,20 +133,20 @@ func main() {
logger.Info(ctx, "listening") logger.Info(ctx, "listening")
a, err := api.New(apiParams) a, err := api.New(apiParams)
if err != nil { if err != nil {
loggerFatalErr(ctx, logger, "initializing api", err) logger.Fatal(ctx, "initializing api", err)
} }
defer func() { defer func() {
shutdownCtx, cancel := context.WithTimeout(ctx, 5*time.Second) shutdownCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel() defer cancel()
if err := a.Shutdown(shutdownCtx); err != nil { if err := a.Shutdown(shutdownCtx); err != nil {
loggerFatalErr(ctx, logger, "shutting down api", err) logger.Fatal(ctx, "shutting down api", err)
} }
}() }()
// wait // wait
sigCh := make(chan os.Signal) sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh <-sigCh

View File

@ -8,7 +8,7 @@ require (
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect
github.com/mattn/go-sqlite3 v1.14.8 github.com/mattn/go-sqlite3 v1.14.8
github.com/mediocregopher/mediocre-go-lib/v2 v2.0.0-beta.0 github.com/mediocregopher/mediocre-go-lib/v2 v2.0.0-beta.0.0.20220506011745-cbeee71cb1ee
github.com/mediocregopher/radix/v4 v4.0.0-beta.1.0.20210726230805-d62fa1b2e3cb // indirect github.com/mediocregopher/radix/v4 v4.0.0-beta.1.0.20210726230805-d62fa1b2e3cb // indirect
github.com/rubenv/sql-migrate v0.0.0-20210614095031-55d5740dbbcc github.com/rubenv/sql-migrate v0.0.0-20210614095031-55d5740dbbcc
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0

View File

@ -108,6 +108,8 @@ github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mediocregopher/mediocre-go-lib/v2 v2.0.0-beta.0 h1:i9FBkcCaWXxteJ8458AD8dBL2YqSxVlpsHOMWg5N9Dc= github.com/mediocregopher/mediocre-go-lib/v2 v2.0.0-beta.0 h1:i9FBkcCaWXxteJ8458AD8dBL2YqSxVlpsHOMWg5N9Dc=
github.com/mediocregopher/mediocre-go-lib/v2 v2.0.0-beta.0/go.mod h1:wOZVlnKYvIbkzyCJ3dxy1k40XkirvCd1pisX2O91qoQ= github.com/mediocregopher/mediocre-go-lib/v2 v2.0.0-beta.0/go.mod h1:wOZVlnKYvIbkzyCJ3dxy1k40XkirvCd1pisX2O91qoQ=
github.com/mediocregopher/mediocre-go-lib/v2 v2.0.0-beta.0.0.20220506011745-cbeee71cb1ee h1:AWRuhgn7iumyhPuxKwed1F1Ri2dXMwxKfp5YIdpnQIY=
github.com/mediocregopher/mediocre-go-lib/v2 v2.0.0-beta.0.0.20220506011745-cbeee71cb1ee/go.mod h1:wOZVlnKYvIbkzyCJ3dxy1k40XkirvCd1pisX2O91qoQ=
github.com/mediocregopher/radix/v4 v4.0.0-beta.1.0.20210726230805-d62fa1b2e3cb h1:7Y2vAC5q44VJzbBUdxRUEqfz88ySJ/6yXXkpQ+sxke4= github.com/mediocregopher/radix/v4 v4.0.0-beta.1.0.20210726230805-d62fa1b2e3cb h1:7Y2vAC5q44VJzbBUdxRUEqfz88ySJ/6yXXkpQ+sxke4=
github.com/mediocregopher/radix/v4 v4.0.0-beta.1.0.20210726230805-d62fa1b2e3cb/go.mod h1:ajchozX/6ELmydxWeWM6xCFHVpZ4+67LXHOTOVR0nCE= github.com/mediocregopher/radix/v4 v4.0.0-beta.1.0.20210726230805-d62fa1b2e3cb/go.mod h1:ajchozX/6ELmydxWeWM6xCFHVpZ4+67LXHOTOVR0nCE=
github.com/mitchellh/cli v1.1.2/go.mod h1:6iaV0fGdElS6dPBx0EApTxHrcWvmJphyh2n8YBLPPZ4= github.com/mitchellh/cli v1.1.2/go.mod h1:6iaV0fGdElS6dPBx0EApTxHrcWvmJphyh2n8YBLPPZ4=