Skip to content

Provide flexible error formatting patterns? #9

@lordvidex

Description

@lordvidex

Currently errors are formatted in a fixed way i.e.

err := B().Code(codes.Internal).Msg("some internal error").Err()
fmt.Println(err) // "internal: some internal error"

Users might want to provide a different formatting for the error messages. More importantly, wrapped errors are separated by a newline token \n and this can be restricting.

Temporary proposal

Currently, my idea is to provide some singleton config that should be set ONLY once, preferrably at the start of a program to specify the user's error formatting preferences.

For Example:

package errs
var prefs struct {
// separates code, op, msg
msgSeparator string // ": "
// separates wrapped errors
errSeparator string  // "\n"
}

func init() {
 prefs = defaultPrefs
}

func SetPreferences(o ...PrefOpts) {
...
}

... and can be used like:

package main 

func main() {
 errs.SetPreferences(
  WithMsgSeparator(", "),
  WithErrSeparator(func(depth int) { 
    "\n" + strings.Repeat("\t", depth)
   }),
   )
   
   // start work ...
}

Sidenote

No one has requested this feature yet, and this error format is the best for my usecases. Till I find a dynamic error API that won't hurt performance so much, I'll leave this issue open.

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions