38 lines
1.2 KiB
Go
38 lines
1.2 KiB
Go
package paymentsvc
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestSafeErrorCategory(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
err error
|
|
want string
|
|
}{
|
|
{"missing result", nil, "unresolved_result"},
|
|
{"timeout", fmt.Errorf("private URL: %w", context.DeadlineExceeded), "timeout"},
|
|
{"cancel", fmt.Errorf("private token: %w", context.Canceled), "cancelled"},
|
|
{"eof", fmt.Errorf("raw response: %w", io.EOF), "response_lost"},
|
|
{"truncated", io.ErrUnexpectedEOF, "response_lost"},
|
|
{"connection", &url.Error{Op: "Post", URL: "https://user:secret@synthetic.invalid", Err: &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("private credential")}}, "connection_failure"},
|
|
{"json syntax", &json.SyntaxError{Offset: 1}, "invalid_response"},
|
|
{"json type", &json.UnmarshalTypeError{Value: "private card", Field: "secret"}, "invalid_response"},
|
|
{"other", errors.New("raw provider response with sensitive content"), "provider_error"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := SafeErrorCategory(tc.err); got != tc.want {
|
|
t.Errorf("SafeErrorCategory(%s)=%q, want %q", tc.name, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|