diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-08-15 21:10:14 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-08-15 21:10:14 +0900 |
commit | 05738faa189e50b6c0ecc52f0e2dbad6bcedb218 (patch) | |
tree | 6c26613bffbbf8bc383d89932dc845759ab50441 | |
parent | Support LALR(1) class (diff) | |
download | urubu-05738faa189e50b6c0ecc52f0e2dbad6bcedb218.tar.gz urubu-05738faa189e50b6c0ecc52f0e2dbad6bcedb218.tar.xz |
Print a stack trace only when a panic occured
-rw-r--r-- | cmd/vartan/compile.go | 8 | ||||
-rw-r--r-- | cmd/vartan/parse.go | 8 |
2 files changed, 14 insertions, 2 deletions
diff --git a/cmd/vartan/compile.go b/cmd/vartan/compile.go index 6fdc6a9..9ffd1c9 100644 --- a/cmd/vartan/compile.go +++ b/cmd/vartan/compile.go @@ -45,6 +45,7 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) { grmPath := *compileFlags.grammar defer func() { + panicked := false v := recover() if v != nil { err, ok := v.(error) @@ -55,6 +56,7 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) { } retErr = err + panicked = true } if retErr != nil { @@ -71,7 +73,11 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) { } } - fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack())) + if panicked { + fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack())) + } else { + fmt.Fprintf(os.Stderr, "%v\n", retErr) + } } }() diff --git a/cmd/vartan/parse.go b/cmd/vartan/parse.go index 2e9c608..89de6f0 100644 --- a/cmd/vartan/parse.go +++ b/cmd/vartan/parse.go @@ -34,6 +34,7 @@ func init() { func runParse(cmd *cobra.Command, args []string) (retErr error) { defer func() { + panicked := false v := recover() if v != nil { err, ok := v.(error) @@ -44,10 +45,15 @@ func runParse(cmd *cobra.Command, args []string) (retErr error) { } retErr = err + panicked = true } if retErr != nil { - fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack())) + if panicked { + fmt.Fprintf(os.Stderr, "%v:\n%v", retErr, string(debug.Stack())) + } else { + fmt.Fprintf(os.Stderr, "%v\n", retErr) + } } }() |