integrate request builder into HTTP client for googleapis support (#159)
All checks were successful
coverage / build (push) Successful in 2m19s
test / test (push) Successful in 2m27s

This commit is contained in:
2025-10-03 10:39:44 +05:00
committed by GitHub
parent b2b24e0a9a
commit c757127453
38 changed files with 9820 additions and 1894 deletions

14
status/error.go Normal file
View File

@@ -0,0 +1,14 @@
package status
// Error is a thin wrapper around Status that implements the error interface.
type Error struct {
s *Status
}
func (e *Error) Error() string {
return e.s.String()
}
func (e *Error) HTTPStatus() *Status {
return e.s
}

54
status/status.go Normal file
View File

@@ -0,0 +1,54 @@
package status
import (
"errors"
"fmt"
"net/http"
)
// Status represents the outcome of an HTTP request in a style similar to gRPC status.
type Status struct {
code int // HTTP status code
message string // HTTP status text
details any // parsed error object
}
func New(statusCode int) *Status {
return &Status{code: statusCode, message: http.StatusText(statusCode)}
}
func FromError(err error) (*Status, bool) {
if err == nil {
return nil, false
}
var e *Error
if errors.As(err, &e) {
return e.HTTPStatus(), true
}
return nil, false
}
func (s *Status) Code() int {
return s.code
}
func (s *Status) Message() string {
return s.message
}
func (s *Status) Details() any {
return s.details
}
func (s *Status) WithDetails(details any) *Status {
s.details = details
return s
}
func (s *Status) String() string {
return fmt.Sprintf("http error: code = %d desc = %s", s.Code(), s.Message())
}
func (s *Status) Err() error {
return &Error{s: s}
}

97
status/status_test.go Normal file
View File

@@ -0,0 +1,97 @@
package status_test
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/require"
"go.unistack.org/micro-client-http/v3/status"
)
type fakeError struct{ s *status.Status }
func (fe *fakeError) Error() string {
return fe.s.String()
}
func TestNew(t *testing.T) {
s := status.New(http.StatusNotFound)
require.Equal(t, http.StatusNotFound, s.Code())
require.Equal(t, "Not Found", s.Message())
}
func TestFromError(t *testing.T) {
tests := []struct {
name string
input error
wantStatus *status.Status
wantOK bool
}{
{
name: "nil error",
input: nil,
wantStatus: nil,
wantOK: false,
},
{
name: "simple error",
input: errors.New("some error"),
wantStatus: nil,
wantOK: false,
},
{
name: "unexpected type of error",
input: func() error {
return &fakeError{s: status.New(http.StatusNotFound)}
}(),
wantStatus: nil,
wantOK: false,
},
{
name: "expected type of error",
input: status.New(http.StatusNotFound).Err(),
wantStatus: status.New(http.StatusNotFound),
wantOK: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, ok := status.FromError(tt.input)
require.Equal(t, tt.wantStatus, result)
require.Equal(t, tt.wantOK, ok)
})
}
}
func TestStatus_Code(t *testing.T) {
s := status.New(http.StatusNotFound)
require.Equal(t, http.StatusNotFound, s.Code())
}
func TestStatus_Message(t *testing.T) {
s := status.New(http.StatusNotFound)
require.Equal(t, "Not Found", s.Message())
}
func TestStatus_WithDetails(t *testing.T) {
s := status.New(http.StatusNotFound).WithDetails(errors.New("some error"))
require.Equal(t, errors.New("some error"), s.Details())
}
func TestStatus_String(t *testing.T) {
s := status.New(http.StatusInternalServerError)
expected := fmt.Sprintf("http error: code = %d desc = %s", 500, "Internal Server Error")
require.Equal(t, expected, s.String())
}
func TestStatus_Err(t *testing.T) {
var e *status.Error
s := status.New(http.StatusForbidden)
require.Error(t, s.Err())
require.ErrorAs(t, s.Err(), &e)
require.Equal(t, status.New(http.StatusForbidden), e.HTTPStatus())
}