feat: enhanced error handling module

This commit is contained in:
yusing
2025-04-16 12:10:54 +08:00
parent 18ab6c52ec
commit 4615d7dd4e
8 changed files with 112 additions and 30 deletions

View File

@@ -1,11 +1,12 @@
package gperr
import (
"encoding/json"
"errors"
"slices"
"strings"
"github.com/yusing/go-proxy/pkg/json"
"github.com/yusing/go-proxy/internal/utils/strutils/ansi"
)
@@ -93,8 +94,7 @@ func (err *withSubject) Error() string {
return sb.String()
}
// MarshalJSON implements the json.Marshaler interface.
func (err *withSubject) MarshalJSON() ([]byte, error) {
func (err *withSubject) MarshalJSONTo(buf []byte) []byte {
subjects := slices.Clone(err.Subjects)
slices.Reverse(subjects)
@@ -102,5 +102,5 @@ func (err *withSubject) MarshalJSON() ([]byte, error) {
"subjects": subjects,
"err": err.Err,
}
return json.Marshal(reversed)
return json.MarshalTo(reversed, buf)
}