mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 04:42:33 +02:00

* feat: idle sleep for proxmox LXCs * refactor: replace deprecated docker api types * chore(api): remove debug task list endpoint * refactor: move servemux to gphttp/servemux; favicon.go to v1/favicon * refactor: introduce Pool interface, move agent_pool to agent module * refactor: simplify api code * feat: introduce debug api * refactor: remove net.URL and net.CIDR types, improved unmarshal handling * chore: update Makefile for debug build tag, update README * chore: add gperr.Unwrap method * feat: relative time and duration formatting * chore: add ROOT_DIR environment variable, refactor * migration: move homepage override and icon cache to $BASE_DIR/data, add migration code * fix: nil dereference on marshalling service health * fix: wait for route deletion * chore: enhance tasks debuggability * feat: stdout access logger and MultiWriter * fix(agent): remove agent properly on verify error * fix(metrics): disk exclusion logic and added corresponding tests * chore: update schema and prettify, fix package.json and Makefile * fix: I/O buffer not being shrunk before putting back to pool * feat: enhanced error handling module * chore: deps upgrade * feat: better value formatting and handling --------- Co-authored-by: yusing <yusing@6uo.me>
114 lines
2.3 KiB
Go
114 lines
2.3 KiB
Go
package gperr
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
|
)
|
|
|
|
//nolint:recvcheck
|
|
type nestedError struct {
|
|
Err error `json:"err"`
|
|
Extras []error `json:"extras"`
|
|
}
|
|
|
|
func (err nestedError) Subject(subject string) Error {
|
|
if err.Err == nil {
|
|
err.Err = PrependSubject(subject, errStr(""))
|
|
} else {
|
|
err.Err = PrependSubject(subject, err.Err)
|
|
}
|
|
return &err
|
|
}
|
|
|
|
func (err *nestedError) Subjectf(format string, args ...any) Error {
|
|
if len(args) > 0 {
|
|
return err.Subject(fmt.Sprintf(format, args...))
|
|
}
|
|
return err.Subject(format)
|
|
}
|
|
|
|
func (err nestedError) With(extra error) Error {
|
|
if extra != nil {
|
|
err.Extras = append(err.Extras, extra)
|
|
}
|
|
return &err
|
|
}
|
|
|
|
func (err nestedError) Withf(format string, args ...any) Error {
|
|
if len(args) > 0 {
|
|
err.Extras = append(err.Extras, fmt.Errorf(format, args...))
|
|
} else {
|
|
err.Extras = append(err.Extras, newError(format))
|
|
}
|
|
return &err
|
|
}
|
|
|
|
func (err *nestedError) Unwrap() []error {
|
|
if err.Err == nil {
|
|
if len(err.Extras) == 0 {
|
|
return nil
|
|
}
|
|
return err.Extras
|
|
}
|
|
return append([]error{err.Err}, err.Extras...)
|
|
}
|
|
|
|
func (err *nestedError) Is(other error) bool {
|
|
if errors.Is(err.Err, other) {
|
|
return true
|
|
}
|
|
for _, e := range err.Extras {
|
|
if errors.Is(e, other) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (err *nestedError) Error() string {
|
|
if err == nil {
|
|
return makeLine("<nil>", 0)
|
|
}
|
|
|
|
if err.Err != nil {
|
|
lines := make([]string, 0, 1+len(err.Extras))
|
|
lines = append(lines, makeLine(err.Err.Error(), 0))
|
|
lines = append(lines, makeLines(err.Extras, 1)...)
|
|
return strutils.JoinLines(lines)
|
|
}
|
|
return strutils.JoinLines(makeLines(err.Extras, 0))
|
|
}
|
|
|
|
//go:inline
|
|
func makeLine(err string, level int) string {
|
|
const bulletPrefix = "• "
|
|
const spaces = " "
|
|
|
|
if level == 0 {
|
|
return err
|
|
}
|
|
return spaces[:2*level] + bulletPrefix + err
|
|
}
|
|
|
|
func makeLines(errs []error, level int) []string {
|
|
if len(errs) == 0 {
|
|
return nil
|
|
}
|
|
lines := make([]string, 0, len(errs))
|
|
for _, err := range errs {
|
|
switch err := wrap(err).(type) {
|
|
case *nestedError:
|
|
if err.Err != nil {
|
|
lines = append(lines, makeLine(err.Err.Error(), level))
|
|
lines = append(lines, makeLines(err.Extras, level+1)...)
|
|
} else {
|
|
lines = append(lines, makeLines(err.Extras, level)...)
|
|
}
|
|
default:
|
|
lines = append(lines, makeLine(err.Error(), level))
|
|
}
|
|
}
|
|
return lines
|
|
}
|