github.com/haraldrudell/parl@v0.4.176/perrors/errorglue/chain-string-format.go (about)

     1  /*
     2  © 2020–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package errorglue
     7  
     8  import "strconv"
     9  
    10  const (
    11  	// DefaultFormat is like printf %v, printf %s and error.Error() “message”
    12  	//	- data, stack traces and associated errors are not printed
    13  	//	- code location is not printed
    14  	DefaultFormat CSFormat = iota + 1
    15  	// CodeLocation DefaultFormat with location added “message at runtime/panic.go:914”
    16  	//	- location is:
    17  	//	- — oldest panic code-line in any stack trace of err or its error chain
    18  	//	- — code-line creating the oldest error in err and its chain that has
    19  	//		a stack trace, when none of the errors hold a panic
    20  	CodeLocation
    21  	// ShortFormat has one-line location similar to printf %-v
    22  	//	- data, and stack traces are not printed
    23  	//	- associated errors are printed
    24  	//	- if the error or its chain contains a stack trace, a code location is output
    25  	//	- if any stack trace contains a panic, that oldest panic location is printed
    26  	//	- “error-message at error116.(*csTypeName).FuncName-chainstring_test.go:26”
    27  	ShortFormat
    28  	// LongFormat is similar to printf %+v.
    29  	//	- prints data, stack traces and associated errors
    30  	//	- if any stack trace contains a panic, that oldest panic location is printed after message
    31  	//
    32  	// output:
    33  	//	error-message
    34  	//	  github.com/haraldrudell/parl/error116.(*csTypeName).FuncName
    35  	//	    /opt/sw/privates/parl/error116/chainstring_test.go:26
    36  	//	  runtime.goexit
    37  	//	    /opt/homebrew/Cellar/go/1.17.8/libexec/src/runtime/asm_arm64.s:1133
    38  	LongFormat
    39  	// ShortSuffix is code-location “runtime/panic.go:914”
    40  	//	- no leading “ at ”
    41  	//	- if no stack is present, empty string
    42  	ShortSuffix
    43  	// LongSuffix full stack trace without message
    44  	LongSuffix
    45  )
    46  
    47  // CSFormat describes string conversion of an error chain
    48  //   - DefaultFormat ShortFormat LongFormat ShortSuffix LongSuffix
    49  type CSFormat uint8
    50  
    51  func (csFormat CSFormat) String() (s string) {
    52  	var ok bool
    53  	if s, ok = csFormatMap[csFormat]; !ok {
    54  		s = "?" + strconv.Itoa(int(csFormat))
    55  	}
    56  	return
    57  }
    58  
    59  // map for quick printable-string lookup
    60  var csFormatMap = map[CSFormat]string{
    61  	DefaultFormat: "DefaultFormat",
    62  	CodeLocation:  "CodeLocation",
    63  	ShortFormat:   "ShortFormat",
    64  	LongFormat:    "LongFormat",
    65  	ShortSuffix:   "ShortSuffix",
    66  	LongSuffix:    "LongSuffix",
    67  }
    68  
    69  // DefaultFormat CodeLocation ShortFormat LongFormat ShortSuffix LongSuffix,
    70  var csFormatList = []CSFormat{
    71  	DefaultFormat, CodeLocation, ShortFormat, LongFormat, ShortSuffix, LongSuffix,
    72  }