Skip to content

Commit

Permalink
refactor halt error implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Nov 26, 2023
1 parent 47f5df9 commit 4066b7a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
27 changes: 20 additions & 7 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ func (err *func2WrapError) Error() string {
type exitCodeError struct {
value any
code int
halt bool
}

func (err *exitCodeError) Error() string {
Expand All @@ -173,10 +172,6 @@ func (err *exitCodeError) Error() string {
return "error: " + jsonMarshal(err.value)
}

func (err *exitCodeError) IsEmptyError() bool {
return err.value == nil && err.halt
}

func (err *exitCodeError) Value() any {
return err.value
}
Expand All @@ -185,8 +180,26 @@ func (err *exitCodeError) ExitCode() int {
return err.code
}

func (err *exitCodeError) IsHaltError() bool {
return err.halt
type haltError exitCodeError

func (err *haltError) Error() string {
return (*exitCodeError)(err).Error()
}

func (err *haltError) IsEmptyError() bool {
return err.value == nil
}

func (err *haltError) Value() any {
return (*exitCodeError)(err).Value()
}

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

func (err *haltError) IsHaltError() bool {
return true
}

type flattenDepthError struct {
Expand Down
11 changes: 4 additions & 7 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,15 @@ loop:
if err == nil {
break loop
}
switch er := err.(type) {
switch e := err.(type) {
case *tryEndError:
err = er.err
err = e.err
break loop
case *breakError:
case *breakError, *haltError:
break loop
case ValueError:
if er, ok := er.(*exitCodeError); ok && er.halt {
break loop
}
env.pop()
env.push(er.Value())
env.push(e.Value())
default:
env.pop()
env.push(err.Error())
Expand Down
6 changes: 3 additions & 3 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -2076,11 +2076,11 @@ func funcError(v any, args []any) any {
if len(args) > 0 {
v = args[0]
}
return &exitCodeError{v, 5, false}
return &exitCodeError{v, 5}
}

func funcHalt(any) any {
return &exitCodeError{nil, 0, true}
return &haltError{nil, 0}
}

func funcHaltError(v any, args []any) any {
Expand All @@ -2091,7 +2091,7 @@ func funcHaltError(v any, args []any) any {
return &func0TypeError{"halt_error", args[0]}
}
}
return &exitCodeError{v, code, true}
return &haltError{v, code}
}

func toInt(x any) (int, bool) {
Expand Down

0 comments on commit 4066b7a

Please sign in to comment.