bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Revisiting Error handling (errexit)


From: Yair Lenga
Subject: Re: Revisiting Error handling (errexit)
Date: Tue, 5 Jul 2022 14:01:16 +0300

Hi,

Below is the patch for the new 'errfail' option.

Please note that this is MINIMAL implementation. It will cover the cases
below. Possible to think about this as replacing every ';' (or new line
line that terminate statements) with '&&' - forcing execution to break.
.
Patch is minimal (less than 15 lines of code changes), and is low-risk -
all changes are conditional on the (global) flag setting. No complex logic,
No write of functions.

Feedback/comments are welcomed.

set -o errfail
function foo { echo A ; false ; echo B ; }
function bar { echo C ; foo ; echo D ; }

 # Will print A, return non-zero status
foo

# Return from function on first error
# Will print A, CATCH
foo || { echo CATCH  ; }

# Failures will propagate through function calls, unless explicitly "caught"
# Print C A CATCH
bar || echo CATCH

# Fancier: "throw"
function throw { echo "${0##*/}: $@" >& 2 ; false ; }

function foo {
    if [ ! -f required-file.txt ] ; then
        throw "Missing required file"
    fi
    echo "YES"
}

Small Letters:
* The errfail does NOT cover "top-level" errors. Only "connected"
statements. Either create a 'main' function, or create a top level block:

On Tue, Jul 5, 2022 at 12:00 AM Lawrence Velázquez <vq@larryv.me> wrote:

> On Mon, Jul 4, 2022, at 3:55 PM, Yair Lenga wrote:
> > I'm sorry - I misunderstood your original comments. I'll prepare the
> > patched version (at least, I would like to add comments before
> > publishing...) , and share it.
> > Where/how can I post it ?
>
> Send it to this list as an attachment [1] with a .txt suffix [2].
>
> [1] Gmail will mangle the patch if you send it inline.
> [2] Alleviates issues with clients on the receiving end.
>
> > I did not see anyone else dropping source
> > code/patches into the group ?
>
> Code contributions are not as common as you might think, given
> bash's prominence.
>
> --
> vq
>

Attachment: errfail-patch.txt
Description: Text document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]