Skip to content

Commit

Permalink
feat: Improve error handling (#156)
Browse files Browse the repository at this point in the history
- Make cmd's return `*errors.WakuError`
- Fixup some logging
- Have `Print` operations respect `log.LogLevel`

---------

Closes #155
Signed-off-by: AlexNg <contact@ngjx.org>
  • Loading branch information
caffeine-addictt authored Oct 4, 2024
2 parents cf030ff + 1f9dad2 commit d2999a8
Show file tree
Hide file tree
Showing 18 changed files with 208 additions and 132 deletions.
81 changes: 81 additions & 0 deletions cmd/cleanup/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// For async cleanup in cases of interrupts
package cleanup

import (
"os"
"os/signal"
"sync"
"syscall"

"github.com/caffeine-addictt/waku/internal/log"
)

var (
cleanupStack []func() error
cleanupRwLock sync.RWMutex
errorCleanupStack []func() error
errorRwLock sync.RWMutex
)

// Schedule adds a function to the cleanup stack
//
// Cleanups are ran no matter what, before error cleanup
func Schedule(fn func() error) {
cleanupRwLock.Lock()
defer cleanupRwLock.Unlock()
cleanupStack = append(cleanupStack, fn)
}

// ScheduleError adds a function to the error cleanup stack
//
// Error cleanups are ran ONLY when an error occurs, and
// are ran after all regular cleanups
func ScheduleError(fn func() error) {
errorRwLock.Lock()
defer errorRwLock.Unlock()
errorCleanupStack = append(errorCleanupStack, fn)
}

// Runs all cleanup functions
func Cleanup() {
cleanupRwLock.Lock()
defer cleanupRwLock.Unlock()

log.Debugf("cleaning up %d items...\n", len(cleanupStack))
for i := len(cleanupStack) - 1; i >= 0; i-- {
if err := cleanupStack[i](); err != nil {
log.Errorf("error while cleaning up: %v\n", err)
}
}

cleanupStack = []func() error{}
}

// Runs all error cleanup functions
func CleanupError() {
errorRwLock.Lock()
defer errorRwLock.Unlock()

log.Debugf("cleaning up %d items due to error...\n", len(errorCleanupStack))
for i := len(errorCleanupStack) - 1; i >= 0; i-- {
if err := errorCleanupStack[i](); err != nil {
log.Errorf("error while cleaning up: %v\n", err)
}
}

errorCleanupStack = []func() error{}
}

// Watches for interrupts and runs all cleanup functions.
func On() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

go func() {
sig := <-sigs
log.Printf("%v received, cleaning up...\n", sig)

Cleanup()
CleanupError()
}()
}
22 changes: 11 additions & 11 deletions cmd/commands/check.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package commands

import (
"os"
"path/filepath"
"strings"

"github.com/caffeine-addictt/waku/internal/errors"
"github.com/caffeine-addictt/waku/internal/log"
"github.com/caffeine-addictt/waku/internal/template"
"github.com/caffeine-addictt/waku/internal/utils"
"github.com/spf13/cobra"
Expand All @@ -18,11 +19,10 @@ var CheckCmd = &cobra.Command{
Args: cobra.MaximumNArgs(1),
SilenceErrors: true,
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
// Check for naming
if len(args) == 1 && !strings.HasSuffix(args[0], "template.json") {
cmd.PrintErrln("name your file template.json")
os.Exit(1)
return errors.NewWakuErrorf("name your file template.json")
}

// Resolve file path
Expand All @@ -34,21 +34,21 @@ var CheckCmd = &cobra.Command{
}
filePath = filepath.Clean(filePath)

log.Debugf("checking if %s is a file\n", filePath)
ok, err := utils.IsFile(filePath)
if err != nil {
cmd.PrintErrln(err)
os.Exit(1)
return errors.NewWakuErrorf("failed to check if %s is a file: %v", filePath, err)
}
if !ok {
cmd.PrintErrln("template.json not found")
os.Exit(1)
return errors.NewWakuErrorf("%s does not exist or is not a file", filePath)
}

log.Debugf("checking if %s is a valid template\n", filePath)
if _, err := template.ParseConfig(filePath); err != nil {
cmd.PrintErrln(err)
os.Exit(1)
return errors.ToWakuError(err)
}

cmd.Println("Seems ok!")
log.Println("Seems ok!")
return nil
},
}
3 changes: 2 additions & 1 deletion cmd/commands/healthcheck.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"github.com/caffeine-addictt/waku/internal/errors"
"github.com/caffeine-addictt/waku/internal/git"
"github.com/caffeine-addictt/waku/internal/log"
"github.com/spf13/cobra"
Expand All @@ -15,7 +16,7 @@ var HealthcheckCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
ok, err := git.HasGit()
if err != nil {
return err
return errors.ToWakuError(err)
}

if !ok {
Expand Down
Loading

0 comments on commit d2999a8

Please sign in to comment.