src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/errs/errs.go (about)

     1  // Package errs declares error types used as exception causes.
     2  package errs
     3  
     4  import (
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"src.elv.sh/pkg/parse"
     9  )
    10  
    11  // OutOfRange encodes an error where a value is out of its valid range.
    12  type OutOfRange struct {
    13  	What      string
    14  	ValidLow  string
    15  	ValidHigh string
    16  	Actual    string
    17  }
    18  
    19  // Error implements the error interface.
    20  func (e OutOfRange) Error() string {
    21  	return fmt.Sprintf(
    22  		"out of range: %s must be from %s to %s, but is %s",
    23  		e.What, e.ValidLow, e.ValidHigh, e.Actual)
    24  }
    25  
    26  // BadValue encodes an error where the value does not meet a requirement. For
    27  // out-of-range errors, use OutOfRange.
    28  type BadValue struct {
    29  	What   string
    30  	Valid  string
    31  	Actual string
    32  }
    33  
    34  // Error implements the error interface.
    35  func (e BadValue) Error() string {
    36  	return fmt.Sprintf(
    37  		"bad value: %v must be %v, but is %v", e.What, e.Valid, e.Actual)
    38  }
    39  
    40  // ArityMismatch encodes an error where the expected number of values is out of
    41  // the valid range.
    42  type ArityMismatch struct {
    43  	What      string
    44  	ValidLow  int
    45  	ValidHigh int
    46  	Actual    int
    47  }
    48  
    49  func (e ArityMismatch) Error() string {
    50  	switch {
    51  	case e.ValidHigh == e.ValidLow:
    52  		return fmt.Sprintf("arity mismatch: %v must be %v, but is %v",
    53  			e.What, nValues(e.ValidLow), nValues(e.Actual))
    54  	case e.ValidHigh == -1:
    55  		return fmt.Sprintf("arity mismatch: %v must be %v or more values, but is %v",
    56  			e.What, e.ValidLow, nValues(e.Actual))
    57  	default:
    58  		return fmt.Sprintf("arity mismatch: %v must be %v to %v values, but is %v",
    59  			e.What, e.ValidLow, e.ValidHigh, nValues(e.Actual))
    60  	}
    61  }
    62  
    63  func nValues(n int) string {
    64  	if n == 1 {
    65  		return "1 value"
    66  	}
    67  	return strconv.Itoa(n) + " values"
    68  }
    69  
    70  // SetReadOnlyVar is returned by the Set method of a read-only variable.
    71  type SetReadOnlyVar struct {
    72  	// Name of the read-only variable. This field is initially empty, and
    73  	// populated later when context information is available.
    74  	VarName string
    75  }
    76  
    77  // Error implements the error interface.
    78  func (e SetReadOnlyVar) Error() string {
    79  	return fmt.Sprintf(
    80  		"cannot set read-only variable $%s", parse.QuoteVariableName(e.VarName))
    81  }
    82  
    83  // ReaderGone is raised by the writer in a pipeline when the reader end has
    84  // terminated. It could be raised directly by builtin commands, or when an
    85  // external command gets terminated by SIGPIPE after Elvish detects the read end
    86  // of the pipe has exited earlier.
    87  type ReaderGone struct {
    88  }
    89  
    90  // Error implements the error interface.
    91  func (e ReaderGone) Error() string {
    92  	return "reader gone"
    93  }