mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 12:12:12 +08:00
Merge 50e0aaf86cd8c279bd15e908326182a02e164ff7 into 5dd833f1f26de0eb30eae47b17e05ced2482dc41
This commit is contained in:
commit
980ba14a9b
30
errors.go
30
errors.go
@ -5,6 +5,7 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@ -165,11 +166,32 @@ func (a errorMsgs) String() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
var buffer strings.Builder
|
var buffer strings.Builder
|
||||||
for i, msg := range a {
|
count := 1
|
||||||
fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)
|
for _, msg := range a {
|
||||||
if msg.Meta != nil {
|
for _, err := range unwrapJoinErr(msg.Err) {
|
||||||
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
|
fmt.Fprintf(&buffer, "Error #%02d: %s\n", count, err)
|
||||||
|
if msg.Meta != nil {
|
||||||
|
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
|
||||||
|
}
|
||||||
|
count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return buffer.String()
|
return buffer.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unwrapJoinErr(err error) []error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var result []error
|
||||||
|
if multi, ok := err.(interface{ Unwrap() []error }); ok {
|
||||||
|
for _, e := range multi.Unwrap() {
|
||||||
|
result = append(result, unwrapJoinErr(e)...)
|
||||||
|
}
|
||||||
|
} else if single := errors.Unwrap(err); single != nil {
|
||||||
|
result = append(result, unwrapJoinErr(single)...)
|
||||||
|
} else {
|
||||||
|
result = append(result, err)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
@ -7,6 +7,7 @@ package gin
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin/codec/json"
|
"github.com/gin-gonic/gin/codec/json"
|
||||||
@ -138,3 +139,26 @@ func TestErrorUnwrap(t *testing.T) {
|
|||||||
var testErrNonPointer TestErr
|
var testErrNonPointer TestErr
|
||||||
require.ErrorAs(t, wrappedErr, &testErrNonPointer)
|
require.ErrorAs(t, wrappedErr, &testErrNonPointer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestErrorJoinFormatting(t *testing.T) {
|
||||||
|
// Create a context with errorMsgs slice
|
||||||
|
c, _ := CreateTestContext(nil)
|
||||||
|
|
||||||
|
// Create a joined error
|
||||||
|
err1 := errors.New("service error")
|
||||||
|
err2 := errors.New("store error")
|
||||||
|
joined := errors.Join(err1, err2)
|
||||||
|
|
||||||
|
// Add to context errors
|
||||||
|
if err := c.Error(joined); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call String(), which should now unwrap and format both
|
||||||
|
output := c.Errors.String()
|
||||||
|
|
||||||
|
// Check that both individual errors appear separately
|
||||||
|
if !strings.Contains(output, "Error #01: service error") || !strings.Contains(output, "Error #02: store error") {
|
||||||
|
t.Errorf("expected unwrapped errors in output, got:\n%s", output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user