Skip to content

Commit

Permalink
Add ON_ERROR_STOP: halt execution when a statement fails
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Waś <[email protected]>
  • Loading branch information
sfc-gh-pbennes and nineinchnick committed Nov 9, 2022
1 parent 7cf5953 commit c8a4edf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
38 changes: 27 additions & 11 deletions env/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func (v Vars) All() map[string]string {

var vars, pvars Vars

// a map of functions to optionally parse parameters when being set
// Takes: value, name string
// Returns: parsedValue string, error
// See ParseBool for an example
var variableParseFunctions = map[string]func(string, string) (string, error){
"ON_ERROR_STOP": ParseBoolEmptyIsOn,
"QUIET": ParseBoolEmptyIsOn,
}

func init() {
// get USQL_* variables
enableHostInformation := "true"
Expand Down Expand Up @@ -69,6 +78,7 @@ func init() {
"SHOW_HOST_INFORMATION": enableHostInformation,
"PAGER": pagerCmd,
"EDITOR": editorCmd,
"ON_ERROR_STOP": "off",
// prompts
"PROMPT1": "%S%N%m%/%R%# ",
// syntax highlighting variables
Expand Down Expand Up @@ -125,21 +135,19 @@ func ValidIdentifier(n string) error {
}

// Set sets a variable.
// Values are optionally parsed by a helper function if one is defined in
// variableParseFunctions
func Set(name, value string) error {
err := ValidIdentifier(name)
if err != nil {
if err := ValidIdentifier(name); err != nil {
return err
}
switch name {
case "QUIET":
if value == "" {
value = "on"
} else {
value, err = ParseBool(value, name)
if err != nil {
return err
}
// parse and validate value if there's a function defined in variableParseFunctions
if parseFunction, ok := variableParseFunctions[name]; ok {
parsedValue, err := parseFunction(value, name)
if err != nil {
return fmt.Errorf(text.FormatFieldInvalid, value, name)
}
value = parsedValue
}
vars.Set(name, value)
return nil
Expand Down Expand Up @@ -212,6 +220,14 @@ func ParseBool(value, name string) (string, error) {
return "", fmt.Errorf(text.FormatFieldInvalidValue, value, name, "Boolean")
}

// ParseBoolEmptyIsOn works like ParseBool but treats empty values as "on"
func ParseBoolEmptyIsOn(value, name string) (string, error) {
if value == "" {
return "on", nil
}
return ParseBool(value, name)
}

func ParseKeywordBool(value, name string, keywords ...string) (string, error) {
v := strings.ToLower(value)
switch v {
Expand Down
13 changes: 12 additions & 1 deletion handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,18 @@ func (h *Handler) Run() error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
if err = h.Execute(ctx, out, opt, h.lastPrefix, h.last, forceBatch); err != nil {
lastErr = WrapErr(h.last, err)
fmt.Fprintln(stderr, "error:", err)
if env.All()["ON_ERROR_STOP"] == "on" {
if iactive {
fmt.Fprintln(stderr, "error:", err)
h.buf.Reset([]rune{}) // empty the buffer so no other statements are run
continue
} else {
stop()
return err
}
} else {
fmt.Fprintln(stderr, "error:", err)
}
}
stop()
}
Expand Down

0 comments on commit c8a4edf

Please sign in to comment.