GoDoxy/internal/gperr
Yuzerion 80bc018a7f
feat: custom json marshaling implementation, replace json and yaml library (#89)
* chore: replace gopkg.in/yaml.v3 vs goccy/go-yaml; replace encoding/json with bytedance/sonic

* fix: yaml unmarshal panic

* feat: custom json marshaler implementation

* chore: fix import and err marshal handling

---------

Co-authored-by: yusing <yusing@6uo.me>
2025-04-16 15:02:11 +08:00
..
base.go feat: custom json marshaling implementation, replace json and yaml library (#89) 2025-04-16 15:02:11 +08:00
builder.go feat: proxmox idlewatcher (#88) 2025-04-16 14:52:33 +08:00
builder_test.go refactor: rename module 'err' to 'gperr' 2025-03-28 05:57:43 +08:00
error.go refactor: rename module 'err' to 'gperr' 2025-03-28 05:57:43 +08:00
error_test.go fix(tests): fix tests for gperr module by stripping ANSI color codes from error messages 2025-04-05 13:31:53 +08:00
log.go feat: proxmox idlewatcher (#88) 2025-04-16 14:52:33 +08:00
multiline.go feat: proxmox idlewatcher (#88) 2025-04-16 14:52:33 +08:00
multiline_test.go feat: proxmox idlewatcher (#88) 2025-04-16 14:52:33 +08:00
nested_error.go feat: proxmox idlewatcher (#88) 2025-04-16 14:52:33 +08:00
README.md refactor: rename module 'err' to 'gperr' 2025-03-28 05:57:43 +08:00
subject.go feat: custom json marshaling implementation, replace json and yaml library (#89) 2025-04-16 15:02:11 +08:00
utils.go feat: custom json marshaling implementation, replace json and yaml library (#89) 2025-04-16 15:02:11 +08:00
utils_test.go refactor: rename module 'err' to 'gperr' 2025-03-28 05:57:43 +08:00

gperr

gperr is an error interface that supports nested structure and subject highlighting.

Usage

gperr.Error

The error interface.

gperr.New

Like errors.New, but returns a gperr.Error.

gperr.Wrap

Like fmt.Errorf("%s: %w", message, err), but returns a gperr.Error.

gperr.Error.Subject

Returns a new error with the subject prepended to the error message. The main subject is highlighted.

err := gperr.New("error message")
err = err.Subject("bar")
err = err.Subject("foo")

Output:

foo > bar: error message

gperr.Error.Subjectf

Like gperr.Error.Subject, but formats the subject with fmt.Sprintf.

gperr.PrependSubject

Prepends the subject to the error message like gperr.Error.Subject.

err := gperr.New("error message")
err = gperr.PrependSubject(err, "foo")
err = gperr.PrependSubject(err, "bar")

Output:

bar > foo: error message

gperr.Error.With

Adds a new error to the error chain.

err := gperr.New("error message")
err = err.With(gperr.New("inner error"))
err = err.With(gperr.New("inner error 2").With(gperr.New("inner inner error")))

Output:

error message:
  • inner error
  • inner error 2
    • inner inner error

gperr.Error.Withf

Like gperr.Error.With, but formats the error with fmt.Errorf.

gperr.Error.Is

Returns true if the error is equal to the given error.

gperr.Builder

A builder for gperr.Error.

builder := gperr.NewBuilder("foo")
builder.Add(gperr.New("error message"))
builder.Addf("error message: %s", "foo")
builder.AddRange(gperr.New("error message 1"), gperr.New("error message 2"))

Output:

foo:
  • error message
  • error message: foo
  • error message 1
  • error message 2

gperr.Builder.Build

Builds a gperr.Error from the builder.

When to return gperr.Error

  • When you want to return multiple errors
  • When the error has a subject