Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds custom error codes #236

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Conversation

nkakouros
Copy link

@nkakouros nkakouros commented Jan 17, 2018

cc: @mbland

This PR is an approach to introducing custom exit codes. This is in relation to #234.

Some notes

  • I used the following to pinpoint the lines I needed to edit:
grep -R -E "^[ ]*(return ?[0-9])|(return$)|(exit$)|([^a-zA-Z0-9_-]exit ?[0-9]?$)" . | grep -v './.git'
  • There are return x commands that are used by functions to signal either an error or one of two or more possible outcomes. There are also exit x commands to stop script execution. I rewrite both of these.
  • Simple return, exit, return 0 or exit 0 commands are rewritten as return "$_GO_EC_OK", etc, where the return code will not be but 0. Although adding nothing in functionality, this is done to be consistent.
  • When a function may return with more than one possible return codes or when it is not obvious by the name of the function what its single return code would be, then other functions that call it catch its return code and return with that. Eg, @go() may return with $_GO_EC_CONFIG or $_GO_EC_USAGE. If @go() were to be called by another function, eg @go.for_lack_of_better_example() then it would be called like this:
@go "$@" && ec=0 || ec="$?"
if [[ "$ec" -ne 0 ]]; then
  return "$ec"
fi
  • In cases like this:
@go.example_func() {
  ...
   @go.another_func arg1 arg2
  return
}

return will return with the return code of @go.another_func. Assuming that, as a go function, @go.another_func will return using the custom codes, no change is performed in the code.

  • When builtins or external commands are called and there is a reasonable chance of sth going wrong, then I catch the return code and return an appropriate custom one. Eg
printf "%s" "$var"
return

is rewritten to

printf "%s" "$var" && ec=0 || ec="$?"
if [[ "$ec" -ne 0 ]]; then
  return $((_GO_EC_GENERR+ec))
fi

Here, printf may exit with 2 if there are wrong arguments used and 1 for other errors. _GO_EC_GENERR+1 corresponds to _GO_EC_EXT1 and _GO_EC_GENERR+2 to _GO_EC_EXT2.
But, for instance:

echo "$var"
return

is left as is, since echo will fail only when a write error occurs, which would mean bigger problems anyway.

  • Bats stuff is left untouched. This means that even calls to bats_restore_shell_options are left as is. Should these be changed as well?

@coveralls
Copy link

Coverage Status

Coverage decreased (-22.2%) to 72.844% when pulling 470239b on tterranigma:return_codes into b8d7140 on mbland:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-22.2%) to 72.862% when pulling 74c09b1 on tterranigma:return_codes into b8d7140 on mbland:master.

@nkakouros nkakouros changed the title Adds custom error codes to go-core.bash Adds custom error codes Jan 19, 2018
@coveralls
Copy link

Coverage Status

Coverage decreased (-22.3%) to 72.779% when pulling fbfc8be on tterranigma:return_codes into b8d7140 on mbland:master.

@coveralls
Copy link

coveralls commented Jan 20, 2018

Coverage Status

Coverage decreased (-22.05%) to 72.999% when pulling 769ee6b on tterranigma:return_codes into b8d7140 on mbland:master.

@nkakouros
Copy link
Author

I have now finished work on this. Waiting for feedback.

@nkakouros
Copy link
Author

Is there interest in this PR? Would it be worth spending more time on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants