Skip to content

Commit

Permalink
print error values on halt_error (close #109)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Aug 2, 2021
1 parent 36aaea9 commit 9a29837
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
13 changes: 12 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,18 @@ func (cli *cli) funcStderr(v interface{}, _ []interface{}) interface{} {

func (cli *cli) printError(err error) {
if er, ok := err.(interface{ IsEmptyError() bool }); !ok || !er.IsEmptyError() {
fmt.Fprintf(cli.errStream, "%s: %s\n", name, err)
if er, ok := err.(interface{ IsHaltError() bool }); !ok || !er.IsHaltError() {
fmt.Fprintf(cli.errStream, "%s: %s\n", name, err)
} else if er, ok := err.(gojq.ValueError); ok {
v := er.Value()
if str, ok := v.(string); ok {
cli.errStream.Write([]byte(str))
} else {
bs, _ := gojq.Marshal(v)
cli.errStream.Write(bs)
cli.errStream.Write([]byte{'\n'})
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestCliRun(t *testing.T) {
if got, expected := errorReplacer.Replace(errStr), strings.TrimSpace(tc.Error); !strings.Contains(got, expected) {
t.Error("standard error output:\n" + cmp.Diff(expected, got))
}
if !strings.HasSuffix(errStr, "\n") && !strings.Contains(tc.Name, "stderr") {
if !strings.HasSuffix(errStr, "\n") && !strings.Contains(tc.Name, "stderr") && !strings.Contains(tc.Name, "halt_error") {
t.Error(`standard error output should end with "\n"`)
}
if strings.HasSuffix(errStr, "\n\n") {
Expand Down
23 changes: 17 additions & 6 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4366,23 +4366,23 @@
- 'halt_error'
input: '1'
error: |
error: 1
1
exit_code: 5

- name: halt_error/0 function
args:
- '0/0 | halt_error'
input: 'null'
error: |
error: null
null
exit_code: 5

- name: halt_error/0 function and try catch
args:
- 'range(5) | try halt_error catch .'
input: 'null'
error: |
error: 0
0
exit_code: 5

- name: halt_error/0 function with null input and try catch
Expand All @@ -4391,6 +4391,19 @@
input: 'null'
exit_code: 5

- name: halt_error/0 function with multiple input values
args:
- 'halt_error'
input: |
1
2
3
error: |
1
2
3
exit_code: 5

- name: halt_error/0 function and raw strings input option
args:
- -R
Expand All @@ -4400,9 +4413,7 @@
2
3
error: |
error: 1
error: 2
error: 3
123
exit_code: 5

- name: halt_error/1 function
Expand Down
8 changes: 6 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ func (err *exitCodeError) IsEmptyError() bool {
return err.value == nil
}

func (err *exitCodeError) Value() interface{} {
return err.value
}

func (err *exitCodeError) ExitCode() int {
return err.code
}

func (err *exitCodeError) Value() interface{} {
return err.value
func (err *exitCodeError) IsHaltError() bool {
return err.halt
}

type funcContainsError struct {
Expand Down

0 comments on commit 9a29837

Please sign in to comment.