~andyc/oils

2a29ebeb62b919185812f5cc63a76573fcf20843 — Andy C 2 months ago cc90726 dev-hoover-17
[ysh breaking] Rename _error.status -> _error.code
4 files changed, 19 insertions(+), 16 deletions(-)

M builtin/error_ysh.py
M core/error.py
M doc/ref/chap-builtin-cmd.md
M spec/ysh-builtin-error.test.sh
M builtin/error_ysh.py => builtin/error_ysh.py +5 -3
@@ 58,8 58,10 @@ class Try(vm._Builtin):
    - Set _error_location
    - These could be used by a 'raise' builtin?  Or 'reraise'

    try foo
    if (_status != 0) {
    try {
      foo
    }
    if (_status !== 0) {
      echo 'hello'
      raise  # reads _status, _error_str, and _error_location ?
    }


@@ 129,7 131,7 @@ class Error(vm._Builtin):
        # use status 3 for expressions and 4 for encode/decode, and 10 "leaves
        # room" for others.
        # The user is of course free to choose status 1.
        status = mops.BigTruncate(rd.NamedInt('status', 10))
        status = mops.BigTruncate(rd.NamedInt('code', 10))

        # attach rest of named args to _error Dict
        properties = rd.RestNamed()

M core/error.py => core/error.py +2 -1
@@ 181,7 181,8 @@ class Structured(FatalRuntime):
        # before these required fields.  But we always want the required fields
        # to take precedence, so it makes sense.

        self.properties['status'] = num.ToBig(self.ExitStatus())
        # _error.code is better than _error.status
        self.properties['code'] = num.ToBig(self.ExitStatus())
        self.properties['message'] = value.Str(self.msg)

        return value.Dict(self.properties)

M doc/ref/chap-builtin-cmd.md => doc/ref/chap-builtin-cmd.md +2 -2
@@ 117,7 117,7 @@ The `error` builtin interrupts the shell program.

Override the default status of `10` with a named argument:

    error 'Missing /tmp' (status=99) 
    error 'Missing /tmp' (code=99) 

In YSH, it's customary to use `error` instead of `return 1`, since it provides
more information:


@@ 144,7 144,7 @@ The integer `_status` is always set, and the Dict `_error` is set for all
Special properties of `_error`:

- `_error.message` - the positional string arg
- `_error.status` - the named `status` arg, or the default 10
- `_error.code` - the named `code` arg, or the default 10

You can attach other, arbitrary properties to the error:


M spec/ysh-builtin-error.test.sh => spec/ysh-builtin-error.test.sh +10 -10
@@ 15,7 15,7 @@ echo status=$?
#### User errors behave like builtin errors
func divide(a, b) {
  if (b === 0) {
    error 'divide by zero' (status=3)
    error 'divide by zero' (code=3)
  }

  return (a / b)


@@ 45,7 45,7 @@ Dict

func divide(a, b) {
  if (b === 0) {
    error "divide by zero: $a / $b" (status=3)
    error "divide by zero: $a / $b" (code=3)
  }
  return (a / b)
}


@@ 76,25 76,25 @@ message=divide by zero: 5 / 0
#### error builtin adds named args as properties on _error Dict

try {
  error 'bad' (status=99)
  error 'bad' (code=99)
}
pp line (_error)

# Note: myData co
try {
  error 'bad' (status=99, myData={spam:'eggs'})
  error 'bad' (code=99, myData={spam:'eggs'})
}
pp line (_error)

try {
  error 'bad' (status=99, message='cannot override')
  error 'bad' (code=99, message='cannot override')
}
pp line (_error)

## STDOUT:
(Dict)   {"status":99,"message":"bad"}
(Dict)   {"myData":{"spam":"eggs"},"status":99,"message":"bad"}
(Dict)   {"message":"bad","status":99}
(Dict)   {"code":99,"message":"bad"}
(Dict)   {"myData":{"spam":"eggs"},"code":99,"message":"bad"}
(Dict)   {"message":"bad","code":99}
## END

#### Errors within multiple functions


@@ 140,8 140,8 @@ error 'some error'
## STDOUT:
## END

#### Error expects an int status
error 'error' (status='a string?')
#### error expects an integer code
error 'error' (code='a string?')
## status: 3
## STDOUT:
## END