Skip to content

Commit

Permalink
chore: replaced returns with fatal/panic comments
Browse files Browse the repository at this point in the history
  • Loading branch information
k4lizen committed Dec 6, 2023
1 parent bdde9ac commit 3a16066
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 23 deletions.
9 changes: 9 additions & 0 deletions generate/searcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func main() {
} else {
if len(tags) != 0 {
log.Fatal("-tags option applies only to directories, not when files are specified")
// ^FATAL
}
dir = path.Dir(args[0])
}
Expand Down Expand Up @@ -108,6 +109,7 @@ func main() {
err := os.WriteFile(outputName, src, 0644)
if err != nil {
log.Fatalf("writing output: %s", err)
// ^FATAL
}
}

Expand All @@ -127,9 +129,11 @@ func (g *Generator) parsePackage(patterns []string, tags []string) {
pkgs, err := packages.Load(cfg, patterns...)
if err != nil {
log.Fatal(err)
// ^FATAL
}
if len(pkgs) != 1 {
log.Fatalf("error: %d packages matching %v", len(pkgs), strings.Join(patterns, " "))
// ^FATAL
}
g.addPackage(pkgs[0])
}
Expand Down Expand Up @@ -167,6 +171,7 @@ func (g *Generator) generate(typeName string) {

if len(values) == 0 {
log.Fatalf("no values defined for type %s", typeName)
// ^FATAL
}

// Generate code for importing engines
Expand Down Expand Up @@ -271,19 +276,23 @@ func (f *File) genDecl(node ast.Node) bool {
obj, ok := f.pkg.defs[name]
if !ok {
log.Fatalf("no value for constant %s", name)
// ^FATAL
}
info := obj.Type().Underlying().(*types.Basic).Info()
if info&types.IsInteger == 0 {
log.Fatalf("can't handle non-integer constant type %s", typ)
// ^FATAL
}
value := obj.(*types.Const).Val() // Guaranteed to succeed as this is CONST.
if value.Kind() != constant.Int {
log.Fatalf("can't happen: constant is not an integer %s", name)
// ^FATAL
}
i64, isInt := constant.Int64Val(value)
u64, isUint := constant.Uint64Val(value)
if !isInt && !isUint {
log.Fatalf("internal error: value of %s is not an integer: %s", name, value.String())
// ^FATAL
}
if !isInt {
u64 = uint64(i64)
Expand Down
1 change: 1 addition & 0 deletions generate/searcher/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func isDirectoryFatal(path string) bool {
info, err := os.Stat(path)
if err != nil {
log.Fatal(err)
// ^FATAL
}
return info.IsDir()
}
10 changes: 5 additions & 5 deletions src/cache/pebble/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func New(dataDirPath string) *DB {

if err != nil {
log.Fatal().Err(err).Msgf("pebble.New(): error opening pebble at path: %v", pebblePath)
return nil
// ^FATAL
} else {
log.Info().Msgf("Successfully opened pebble (path: %v)", pebblePath)
}
Expand All @@ -32,7 +32,7 @@ func New(dataDirPath string) *DB {
func (db *DB) Close() {
if err := db.pdb.Close(); err != nil {
log.Fatal().Err(err).Msg("pebble.Close(): error closing pebble")
return
// ^FATAL
} else {
log.Debug().Msg("Successfully closed pebble")
}
Expand All @@ -46,7 +46,7 @@ func (db *DB) Set(k string, v cache.Value) error {
return fmt.Errorf("pebble.Set(): error marshaling value: %w", err)
} else if err := db.pdb.Set([]byte(k), val, pebble.NoSync); err != nil {
log.Fatal().Err(err).Msg("pebble.Set(): error setting KV to pebble")
return nil
// ^FATAL
} else {
cacheTimeSince := time.Since(cacheTimer)
log.Debug().Msgf("Cached results in %vms (%vns)", cacheTimeSince.Milliseconds(), cacheTimeSince.Nanoseconds())
Expand All @@ -62,10 +62,10 @@ func (db *DB) Get(k string, o cache.Value) error {
log.Trace().Msgf("Found no value in pebble for key: \"%v\"", k)
} else if err != nil {
log.Fatal().Err(err).Msgf("pebble.Get(): error getting value from pebble for key %v", k)
return nil
// ^FATAL
} else if err := c.Close(); err != nil {
log.Fatal().Err(err).Msgf("pebble.Get(): error closing io to pebble for key %v", k)
return nil
// ^FATAL
} else if err := cbor.Unmarshal(val, o); err != nil {
return fmt.Errorf("pebble.Get(): failed unmarshaling value from pebble for key %v: %w", k, err)
}
Expand Down
8 changes: 4 additions & 4 deletions src/cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func New(ctx context.Context, config config.Redis) *DB {

if err := rdb.Ping(ctx).Err(); err != nil {
log.Fatal().Err(err).Msgf("redis.New(): error connecting to redis with addr: %v:%v/%v", config.Host, config.Port, config.Database)
return nil
// ^FATAL
} else {
log.Info().Msgf("Successful connection to redis (addr: %v:%v/%v)", config.Host, config.Port, config.Database)
}
Expand All @@ -37,7 +37,7 @@ func New(ctx context.Context, config config.Redis) *DB {
func (db *DB) Close() {
if err := db.rdb.Close(); err != nil {
log.Fatal().Err(err).Msg("redis.Close(): error disconnecting from redis")
return
// ^FATAL
} else {
log.Debug().Msg("Successfully disconnected from redis")
}
Expand All @@ -51,7 +51,7 @@ func (db *DB) Set(k string, v cache.Value) error {
return fmt.Errorf("redis.Set(): error marshaling value: %w", err)
} else if err := db.rdb.Set(db.ctx, k, val, 0).Err(); err != nil {
log.Fatal().Err(err).Msg("redis.Set(): error setting KV to redis")
return nil
// ^FATAL
} else {
cacheTimeSince := time.Since(cacheTimer)
log.Debug().Msgf("Cached results in %vms (%vns)", cacheTimeSince.Milliseconds(), cacheTimeSince.Nanoseconds())
Expand All @@ -67,7 +67,7 @@ func (db *DB) Get(k string, o cache.Value) error {
log.Trace().Msgf("found no value in redis for key \"%v\"", k)
} else if err != nil {
log.Fatal().Err(err).Msgf("redis.Get(): error getting value from redis for key %v", k)
return nil
// ^FATAL
} else if err := cbor.Unmarshal(val, o); err != nil {
return fmt.Errorf("redis.Set(): failed unmarshaling value from redis for key %v: %w", k, err)
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/climode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"fmt"
"time"

"github.com/rs/zerolog/log"
"github.com/hearchco/hearchco/src/bucket/result"
"github.com/hearchco/hearchco/src/cache"
"github.com/hearchco/hearchco/src/category"
"github.com/hearchco/hearchco/src/config"
"github.com/hearchco/hearchco/src/engines"
"github.com/hearchco/hearchco/src/search"
"github.com/rs/zerolog/log"
)

func printResults(results []result.Result) {
Expand Down Expand Up @@ -47,7 +47,7 @@ func Run(flags Flags, db cache.DB, conf *config.Config) {
gerr := db.Get(flags.Query, &results)
if gerr != nil {
log.Fatal().Err(gerr).Msgf("cli.Run(): failed accessing cache for query %v", flags.Query)
return
// ^FATAL
}

if results != nil {
Expand Down
1 change: 1 addition & 0 deletions src/cli/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func Setup() Flags {

if err := ctx.Validate(); err != nil {
log.Panic().Err(err).Msg("cli.Setup(): failed parsing cli") // panic is also run inside the library. when does this happen?
// ^PANIC
}

return cli
Expand Down
14 changes: 7 additions & 7 deletions src/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *Config) fromReader(rc *ReaderConfig) {
keyName, err := engines.NameString(key)
if err != nil {
log.Panic().Err(err).Msgf("failed reading config. invalid engine name: %v", key)
return
// ^PANIC
}
nc.Settings[keyName] = val
}
Expand All @@ -42,7 +42,7 @@ func (c *Config) fromReader(rc *ReaderConfig) {
engineName, nameErr := engines.NameString(name)
if nameErr != nil {
log.Panic().Err(nameErr).Msg("failed converting string to engine name")
return
// ^PANIC
}

engArr = append(engArr, engineName)
Expand Down Expand Up @@ -113,7 +113,7 @@ func (c *Config) Load(dataDirPath string, logDirPath string) {
// provider.
if err := k.Load(structs.Provider(&rc, "koanf"), nil); err != nil {
log.Panic().Err(err).Msg("config.Load(): failed loading default values")
return
// ^PANIC
}

// Load YAML config
Expand All @@ -125,25 +125,25 @@ func (c *Config) Load(dataDirPath string, logDirPath string) {
log.Trace().Msgf("config.Load(): no yaml config present at path: %v", yamlPath)
} else if errr := k.Load(file.Provider(yamlPath), yaml.Parser()); errr != nil {
log.Panic().Err(err).Msg("config.Load(): error loading yaml config")
return
// ^PANIC
}
} else if err := k.Load(file.Provider(yamlPath), yaml.Parser()); err != nil {
log.Panic().Err(err).Msg("config.Load(): error loading yaml config")
return
// ^PANIC
}

// Load ENV config
if err := k.Load(env.Provider("HEARCHCO_", ".", func(s string) string {
return strings.Replace(strings.ToLower(strings.TrimPrefix(s, "HEARCHCO_")), "_", ".", -1)
}), nil); err != nil {
log.Panic().Err(err).Msg("config.Load(): error loading env config")
return
// ^PANIC
}

// Unmarshal config into struct
if err := k.Unmarshal("", &rc); err != nil {
log.Panic().Err(err).Msg("config.Load(): failed unmarshaling koanf config")
return
// ^PANIC
}

c.fromReader(&rc)
Expand Down
4 changes: 2 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"syscall"
"time"

"github.com/rs/zerolog/log"
"github.com/hearchco/hearchco/src/cache"
"github.com/hearchco/hearchco/src/cache/nocache"
"github.com/hearchco/hearchco/src/cache/pebble"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/hearchco/hearchco/src/config"
"github.com/hearchco/hearchco/src/logger"
"github.com/hearchco/hearchco/src/router"
"github.com/rs/zerolog/log"
)

func main() {
Expand Down Expand Up @@ -55,7 +55,7 @@ func main() {
} else {
if rw, err := router.New(conf, cliFlags.Verbosity); err != nil {
log.Fatal().Err(err).Msg("main.main(): failed creating a router")
return
// ^FATAL
} else {
rw.Start(ctx, db, cliFlags.ServeProfiler)
}
Expand Down
4 changes: 2 additions & 2 deletions src/profiling.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"log"

"github.com/pkg/profile"
"github.com/hearchco/hearchco/src/cli"
"github.com/pkg/profile"
)

type profiler struct {
Expand Down Expand Up @@ -55,7 +55,7 @@ func runProfiler(cliFlags *cli.Flags) (bool, func()) {
for _, p := range profilers {
if profilerToRun.enabled && p.enabled {
log.Fatal("main.runProfiler(): only one profiler can be run at a time.")
return false, func() {}
// ^FATAL
} else if p.enabled {
profilerToRun = p
}
Expand Down
3 changes: 2 additions & 1 deletion src/sedefaults/sedefaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func Prepare(seName engines.Name, options *engines.Options, settings *config.Set
// TODO: move to config.SetupConfig
if settings.RequestedResultsPerPage != 0 && !support.RequestedResultsPerPage {
log.Panic().Msgf("sedefaults.Prepare() from %v: setting not supported. variable settings.RequestedResultsPerPage is set in the config for %v. that setting is not supported for this search engine. the settings value is: %v", seName, seName, settings.RequestedResultsPerPage)
return nil
// ^PANIC
}
if settings.RequestedResultsPerPage == 0 && support.RequestedResultsPerPage {
// If its used in the code but not set, give it the default value.
Expand Down Expand Up @@ -164,6 +164,7 @@ func PageFromContext(ctx *colly.Context, seName engines.Name) int {
page, converr := strconv.Atoi(pageStr)
if converr != nil {
log.Panic().Err(converr).Msgf("sedefaults.PageFromContext from %v: failed to convert page number to int. pageStr: %v", seName, pageStr)
// ^PANIC
}
return page
}

0 comments on commit 3a16066

Please sign in to comment.