Skip to content

Commit

Permalink
make logger private, create ResponseError
Browse files Browse the repository at this point in the history
  • Loading branch information
mauserzjeh committed Mar 15, 2024
1 parent 1f8ca52 commit 8b841ee
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
54 changes: 41 additions & 13 deletions pingo.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ import (

type (

// Logger is the internal logger used by the package
Logger struct {
// logger is the internal logger used by the package
logger struct {
l *log.Logger // underlying [log.Logger]
flag atomic.Int32 // logging flags
timeFormat atomic.Pointer[string] // format of the time part when [Ftime] flag is provided
Expand All @@ -63,7 +63,7 @@ type (
headers http.Header // headers for the client
queryParams url.Values // query parameters for the client
timeout time.Duration // timeout for the client
logger *Logger // logger used by the client
logger *logger // logger used by the client
isLogEnabled bool // whether logging is enabled or disabled in this client
}

Expand Down Expand Up @@ -106,6 +106,12 @@ type (
body []byte // response body
}

// ResponseError holds data of response that is considered to be an error
ResponseError struct {
responseHeader // response header info
body []byte // response body
}

// ResponseUnmarshaler is a function that can be used to unmarshal a response
ResponseUnmarshaler func(r *Response) error

Expand Down Expand Up @@ -142,7 +148,7 @@ var (
)

const (
version = "v2.0.0"
version = "v2.1.0"
pingo = "pingo"
defaultTimeFormat = "2006-01-02 15:04:05"

Expand All @@ -166,8 +172,8 @@ const (
// ---------------------------------------------- //

// newDefaultLogger creates a new default logger
func newDefaultLogger() *Logger {
l := &Logger{
func newDefaultLogger() *logger {
l := &logger{
l: log.New(os.Stdout, "", 0),
}

Expand All @@ -178,32 +184,32 @@ func newDefaultLogger() *Logger {
}

// setFlags sets the flag value
func (l *Logger) setFlags(flag int) {
func (l *logger) setFlags(flag int) {
l.flag.Store(int32(flag))
}

// flags returns the flag value
func (l *Logger) flags() int {
func (l *logger) flags() int {
return int(l.flag.Load())
}

// setTimeFormat sets the time format
func (l *Logger) setTimeFormat(format string) {
func (l *logger) setTimeFormat(format string) {
l.timeFormat.Store(&format)
}

// timeFmt returns the time format
func (l *Logger) timeFmt() string {
func (l *logger) timeFmt() string {
return *(l.timeFormat.Load())
}

// setOutput sets the output
func (l *Logger) setOutput(w io.Writer) {
func (l *logger) setOutput(w io.Writer) {
l.l.SetOutput(w)
}

// log writes the log message
func (l *Logger) log(format string, args ...any) {
func (l *logger) log(format string, args ...any) {
t := time.Now()
flag := l.flags()
sb := strings.Builder{}
Expand Down Expand Up @@ -808,7 +814,10 @@ func (r *Response) BodyString() string {
// The error's text will be the response body
func (r *Response) IsError() error {
if r.statusCode < 200 || r.statusCode >= 400 {
return fmt.Errorf("%s", r.body)
return &ResponseError{
responseHeader: r.responseHeader,
body: r.body,
}
}

return nil
Expand All @@ -820,6 +829,25 @@ func (r *Response) Unmarshal(u ResponseUnmarshaler) error {
return u(r)
}

// ---------------------------------------------- //
// ResponseError //
// ---------------------------------------------- //

// Error implements the error interface
func (r ResponseError) Error() string {
return fmt.Sprintf("[%v] %s", r.status, r.body)
}

// BodyRaw returns the response body as a byte slice
func (r *ResponseError) BodyRaw() []byte {
return r.body
}

// BodyString returns the response body as string
func (r *ResponseError) BodyString() string {
return string(r.body)
}

// ---------------------------------------------- //
// ResponseStream //
// ---------------------------------------------- //
Expand Down
9 changes: 8 additions & 1 deletion pingo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,14 @@ func TestError(t *testing.T) {
if respErr == nil {
t.Fatal("respErr is nil")
}
assertEqual(t, respErr.Error(), "error")

var e *ResponseError
assertEqual(t, errors.As(respErr, &e), true)
assertEqual(t, e.BodyString(), "error")
assertEqual(t, bytes.Equal(e.BodyRaw(), []byte("error")), true)
assertEqual(t, e.StatusCode(), http.StatusInternalServerError)
assertEqual(t, e.Error(), "[500 Internal Server Error] error")

}

type sUnmarshal struct {
Expand Down

0 comments on commit 8b841ee

Please sign in to comment.