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>
106 lines
2.1 KiB
Go
106 lines
2.1 KiB
Go
package gperr
|
|
|
|
import (
|
|
"errors"
|
|
"slices"
|
|
"strings"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/yusing/go-proxy/internal/utils/strutils/ansi"
|
|
)
|
|
|
|
//nolint:errname
|
|
type withSubject struct {
|
|
Subjects []string
|
|
Err error
|
|
|
|
pendingSubject string
|
|
}
|
|
|
|
const subjectSep = " > "
|
|
|
|
func highlight(subject string) string {
|
|
return ansi.HighlightRed + subject + ansi.Reset
|
|
}
|
|
|
|
func PrependSubject(subject string, err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
|
|
if subject == "" {
|
|
return err
|
|
}
|
|
|
|
//nolint:errorlint
|
|
switch err := err.(type) {
|
|
case *withSubject:
|
|
return err.Prepend(subject)
|
|
case Error:
|
|
return err.Subject(subject)
|
|
}
|
|
return &withSubject{[]string{subject}, err, ""}
|
|
}
|
|
|
|
func (err *withSubject) Prepend(subject string) *withSubject {
|
|
if subject == "" {
|
|
return err
|
|
}
|
|
|
|
clone := *err
|
|
switch subject[0] {
|
|
case '[', '(', '{':
|
|
// since prepend is called in depth-first order,
|
|
// the subject of the index is not yet seen
|
|
// add it when the next subject is seen
|
|
clone.pendingSubject += subject
|
|
default:
|
|
clone.Subjects = append(clone.Subjects, subject)
|
|
if clone.pendingSubject != "" {
|
|
clone.Subjects[len(clone.Subjects)-1] = subject + clone.pendingSubject
|
|
clone.pendingSubject = ""
|
|
}
|
|
}
|
|
return &clone
|
|
}
|
|
|
|
func (err *withSubject) Is(other error) bool {
|
|
return errors.Is(other, err.Err)
|
|
}
|
|
|
|
func (err *withSubject) Unwrap() error {
|
|
return err.Err
|
|
}
|
|
|
|
func (err *withSubject) Error() string {
|
|
// subject is in reversed order
|
|
n := len(err.Subjects)
|
|
size := 0
|
|
errStr := err.Err.Error()
|
|
var sb strings.Builder
|
|
for _, s := range err.Subjects {
|
|
size += len(s)
|
|
}
|
|
sb.Grow(size + 2 + n*len(subjectSep) + len(errStr) + len(highlight("")))
|
|
|
|
for i := n - 1; i > 0; i-- {
|
|
sb.WriteString(err.Subjects[i])
|
|
sb.WriteString(subjectSep)
|
|
}
|
|
sb.WriteString(highlight(err.Subjects[0]))
|
|
sb.WriteString(": ")
|
|
sb.WriteString(errStr)
|
|
return sb.String()
|
|
}
|
|
|
|
func (err *withSubject) MarshalJSON() ([]byte, error) {
|
|
subjects := slices.Clone(err.Subjects)
|
|
slices.Reverse(subjects)
|
|
|
|
reversed := map[string]any{
|
|
"subjects": subjects,
|
|
"err": err.Err,
|
|
}
|
|
return json.Marshal(reversed)
|
|
}
|