Files
micro-client-http/status/status.go
pugnack 6209a03044
Some checks failed
sync / sync (push) Failing after 1h5m56s
test / test (push) Failing after 1h9m11s
coverage / build (push) Failing after 1h9m22s
[v4] support error handling flow like gRPC (#158)
* add status package with tests and integrate into response parsing
* improve unit-tests
* improve readme
2025-09-30 10:28:39 +03:00

55 lines
982 B
Go

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}
}