Skip to content

Commit

Permalink
fix: Improve error handling (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Morozko authored Dec 16, 2024
1 parent 8c040aa commit 72012c6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 41 deletions.
10 changes: 2 additions & 8 deletions cmd/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,11 @@ var dataCmd = &cobra.Command{
engine.WithBuiltIn(builtin.Plugin(version, slog.Default(), tracer)),
)
defer func() {
diags.Extend(eng.Cleanup())
if diags.HasErrors() {
err = diags
cmd.SilenceErrors = true
cmd.SilenceUsage = true
}
eng.PrintDiagnostics(os.Stderr, diags, cliArgs.colorize)
err = exitCommand(eng, cmd, diags)
}()
diag := eng.ParseDir(ctx, os.DirFS(cliArgs.sourceDir))
if diags.Extend(diag) {
return diags
return
}
if diags.Extend(eng.LoadPluginResolver(ctx, false)) {
return
Expand Down
16 changes: 5 additions & 11 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,20 @@ var installCmd = &cobra.Command{
engine.WithBuiltIn(builtin.Plugin(version, slog.Default(), tracer)),
)
defer func() {
diags.Extend(eng.Cleanup())
if diags.HasErrors() {
err = diags
cmd.SilenceErrors = true
cmd.SilenceUsage = true
}
eng.PrintDiagnostics(os.Stderr, diags, cliArgs.colorize)
err = exitCommand(eng, cmd, diags)
}()
diag := eng.ParseDir(ctx, os.DirFS(cliArgs.sourceDir))
if diags.Extend(diag) {
return diags
return
}
diag = eng.LoadPluginResolver(ctx, true)
if diags.Extend(diag) {
return diags
return
}
diag = eng.Install(ctx, installUpgrade)
if diags.Extend(diag) {
return diags
return
}
return nil
return
},
}
12 changes: 3 additions & 9 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,11 @@ var lintCmd = &cobra.Command{
engine.WithBuiltIn(builtin.Plugin(version, slog.Default(), tracer)),
)
defer func() {
diags.Extend(eng.Cleanup())
if diags.HasErrors() {
err = diags
cmd.SilenceErrors = true
cmd.SilenceUsage = true
}
eng.PrintDiagnostics(os.Stderr, diags, cliArgs.colorize)
err = exitCommand(eng, cmd, diags)
}()
diag := eng.ParseDir(ctx, os.DirFS(cliArgs.sourceDir))
if diags.Extend(diag) {
return diags
return
}
if fullLint {
if diags.Extend(eng.LoadPluginResolver(ctx, false)) {
Expand All @@ -53,6 +47,6 @@ var lintCmd = &cobra.Command{
}
diag = eng.Lint(ctx, fullLint)
diags.Extend(diag)
return nil
return
},
}
22 changes: 9 additions & 13 deletions cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,31 @@ var renderCmd = &cobra.Command{
engine.WithBuiltIn(builtin.Plugin(version, slog.Default(), tracer)),
)
defer func() {
diags.Extend(eng.Cleanup())
if diags.HasErrors() {
err = diags
cmd.SilenceErrors = true
cmd.SilenceUsage = true
}
eng.PrintDiagnostics(os.Stderr, diags, cliArgs.colorize)
err = exitCommand(eng, cmd, diags)
}()
diag := eng.ParseDir(ctx, os.DirFS(cliArgs.sourceDir))
if diags.Extend(diag) {
return diags
return
}
diag = eng.LoadPluginResolver(ctx, false)
if diags.Extend(diag) {
return diags
return
}
diag = eng.LoadPluginRunner(ctx)
if diags.Extend(diag) {
return diags
return
}

doc, content, dataCtx, diag := eng.RenderContent(ctx, target, requiredTags)
if diags.Extend(diag) {
return diags
return
}

if publish {
diags.Extend(eng.PublishContent(ctx, target, doc, content, dataCtx))
return diags
diag = eng.PublishContent(ctx, target, doc, content, dataCtx)
if diags.Extend(diag) {
return
}
}

logger.InfoContext(ctx, "Printing to stdout", "format", format)
Expand Down
15 changes: 15 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/blackstork-io/fabric/cmd/fabctx"
"github.com/blackstork-io/fabric/cmd/internal/multilog"
"github.com/blackstork-io/fabric/cmd/internal/telemetry"
"github.com/blackstork-io/fabric/engine"
"github.com/blackstork-io/fabric/pkg/diagnostics"
"github.com/blackstork-io/fabric/pkg/utils/slogutil"
)

Expand Down Expand Up @@ -206,3 +208,16 @@ func recoverExecute(ctx context.Context, cmd *cobra.Command) (err error) {
}()
return cmd.ExecuteContext(ctx)
}

func exitCommand(eng *engine.Engine, cmd *cobra.Command, diags diagnostics.Diag) (err error) {
diags.Extend(eng.Cleanup())
if diags.HasErrors() {
err = diags
cmd.SilenceErrors = true
cmd.SilenceUsage = true
} else {
err = nil
}
eng.PrintDiagnostics(os.Stderr, diags, cliArgs.colorize)
return err
}

0 comments on commit 72012c6

Please sign in to comment.