Skip to content

Commit

Permalink
Remove inappropriate use of panic.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxki committed Sep 29, 2023
1 parent 36c4b6f commit 88606b8
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions cmd/dyocsp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,27 @@ func newResponder(cfg config.DyOCSPConfig) *dyocsp.Responder {
return responder
}

func newFileDBClient(cfg config.DyOCSPConfig) db.FileDBClient {
var ErrFileDBInvalid = errors.New("invalid db file")

func newFileDBClient(cfg config.DyOCSPConfig) (db.FileDBClient, error) {
var dbClient db.FileDBClient

abs, err := filepath.Abs(cfg.FileDBFile)
if err != nil {
stdlog.Fatal(err.Error())
return dbClient, err
}
info, err := os.Stat(abs)
if err != nil {
stdlog.Fatal(err.Error())
return dbClient, err
}
if info.IsDir() {
stdlog.Fatalf("%s is not a file.", abs)
return dbClient, ErrFileDBInvalid
}

return db.NewFileDBClient(cfg.CA, cfg.FileDBFile)
return db.NewFileDBClient(cfg.CA, cfg.FileDBFile), nil
}

func newDynamoDBClient(cfg config.DyOCSPConfig) db.DynamoDBClient {
func newDynamoDBClient(cfg config.DyOCSPConfig) (db.DynamoDBClient, error) {
ca := cfg.CA
caTable := cfg.DynamoDBTableName
caGsi := cfg.DynamoDBCAGsi
Expand All @@ -84,7 +88,8 @@ func newDynamoDBClient(cfg config.DyOCSPConfig) db.DynamoDBClient {
awsconfig.WithRetryMaxAttempts(cfg.DynamoDBRetryMaxAttempts),
)
if err != nil {
panic(err)
var dynamoDBClient db.DynamoDBClient
return dynamoDBClient, err
}

var client *dynamodb.Client
Expand All @@ -96,11 +101,7 @@ func newDynamoDBClient(cfg config.DyOCSPConfig) db.DynamoDBClient {
})
}

return db.NewDynamoDBClient(client, &ca, &caTable, &caGsi, cfg.DynamoDBTimeout)
}

func printUsage() {
stdlog.Println("Usage: dyocsp -c CONFIG_FILE")
return db.NewDynamoDBClient(client, &ca, &caTable, &caGsi, cfg.DynamoDBTimeout), nil
}

const (
Expand All @@ -123,7 +124,7 @@ func chainHTTPAccessHandler(c alice.Chain) alice.Chain {
return chain
}

func run(cfg config.DyOCSPConfig, responder *dyocsp.Responder) {
func run(cfg config.DyOCSPConfig, responder *dyocsp.Responder) error {
setupLogger(cfg)

rootCtx := context.Background()
Expand All @@ -132,14 +133,18 @@ func run(cfg config.DyOCSPConfig, responder *dyocsp.Responder) {

// Create DB client
var dbClient dyocsp.CADBClient
var err error

switch cfg.DBType {
case config.FileDBType:
dbClient = newFileDBClient(cfg)
dbClient, err = newFileDBClient(cfg)
case config.DynamoDBType:
dbClient = newDynamoDBClient(cfg)
dbClient, err = newDynamoDBClient(cfg)
default:
panic(config.MissingParameterError{Param: "db.<db-type>"})
err = config.MissingParameterError{Param: "db.<db-type>"}
}
if err != nil {
return err
}

// Create cache store
Expand All @@ -163,7 +168,7 @@ func run(cfg config.DyOCSPConfig, responder *dyocsp.Responder) {
dyocsp.WithQuiteChan(quite),
)
if err != nil {
panic(err)
return err
}

// Run batch generating caches
Expand Down Expand Up @@ -203,6 +208,8 @@ func run(cfg config.DyOCSPConfig, responder *dyocsp.Responder) {
stdlog.Printf("Batch loop quited: received message from caching batch: %s", <-quite)
stdlog.Fatalf("error:listening server: %v", err)
}

return nil
}

func main() {
Expand All @@ -215,28 +222,26 @@ func main() {
flag.Parse()

if *cfgPtr == "" {
printUsage()
flag.PrintDefaults()
os.Exit(1)
}

cfgF, err := os.Open(*cfgPtr)
if err != nil {
stdlog.Fatalf("error:cfg file: %v", err)
os.Exit(1)
stdlog.Fatal(err)
}

var cfgYml config.ConfigYAML
err = yaml.NewDecoder(cfgF).Decode(&cfgYml)
if err != nil {
stdlog.Fatalf("error:cfg file: %v", err)
os.Exit(1)
stdlog.Fatal(err)
}

var cfg config.DyOCSPConfig
cfg, errs := cfgYml.Verify(cfg)
if errs != nil {
for _, err := range errs {
stdlog.Printf("error:cfg file: %v", err)
stdlog.Print(err)
}
os.Exit(1)
}
Expand All @@ -248,5 +253,8 @@ func main() {

responder := newResponder(cfg)

run(cfg, responder)
err = run(cfg, responder)
if err != nil {
stdlog.Fatal(err)
}
}

0 comments on commit 88606b8

Please sign in to comment.