Skip to content

Commit

Permalink
fix: output errors regarding to configuration files (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke authored Nov 29, 2023
1 parent e5c3fec commit c0cec6d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
12 changes: 11 additions & 1 deletion cmd/ghalint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package main

import (
"context"
"errors"
"os"
"os/signal"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/suzuki-shunsuke/ghalint/pkg/cli"
"github.com/suzuki-shunsuke/ghalint/pkg/controller"
"github.com/suzuki-shunsuke/logrus-error/logerr"
)

var (
Expand All @@ -16,8 +20,14 @@ var (
)

func main() {
logE := logrus.NewEntry(logrus.New())
if err := core(); err != nil {
os.Exit(1)
hasLogLevel := &controller.HasLogLevelError{}
if errors.As(err, &hasLogLevel) {
logerr.WithError(logE, hasLogLevel.Err).Log(hasLogLevel.LogLevel, "ghalint failed")
os.Exit(1)
}
logerr.WithError(logE, err).Fatal("ghalint failed")
}
}

Expand Down
14 changes: 9 additions & 5 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ func (c *Controller) Run(ctx context.Context, logE *logrus.Entry) error {
cfg := &config.Config{}
if cfgFilePath := config.Find(c.fs); cfgFilePath != "" {
if err := config.Read(c.fs, cfg, cfgFilePath); err != nil {
return fmt.Errorf("read a configuration file: %w", err)
return fmt.Errorf("read a configuration file: %w", logerr.WithFields(err, logrus.Fields{
"config_file": cfgFilePath,
}))
}
if err := config.Validate(cfg); err != nil {
return fmt.Errorf("validate a configuration file: %w", logerr.WithFields(err, logrus.Fields{
"config_file": cfgFilePath,
}))
}
}
if err := config.Validate(cfg); err != nil {
return fmt.Errorf("validate a configuration file: %w", err)
}
filePaths, err := workflow.List(c.fs)
if err != nil {
Expand All @@ -55,7 +59,7 @@ func (c *Controller) Run(ctx context.Context, logE *logrus.Entry) error {
}
}
if failed {
return errors.New("some workflow files are invalid")
return debugError(errors.New("some workflow files are invalid"))
}
return nil
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/controller/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package controller

import "github.com/sirupsen/logrus"

type HasLogLevelError struct {
LogLevel logrus.Level
Err error
}

func (e *HasLogLevelError) Error() string {
return e.Err.Error()
}

func (e *HasLogLevelError) Unwrap() error {
return e.Err
}

func debugError(err error) *HasLogLevelError {
return &HasLogLevelError{
LogLevel: logrus.DebugLevel,
Err: err,
}
}

0 comments on commit c0cec6d

Please sign in to comment.