Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unit test and improve recovery middleware #103

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion framework/rest/bizerror.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest

import (
"fmt"
"net/http"
)

Expand Down Expand Up @@ -50,12 +51,15 @@ func NewBizError(err error, opts ...BizErrorOption) BizError {

// String function is used for printing string representation of a BizError instance
func (b BizError) String() string {
if b.ErrCode > 0 {
return fmt.Sprintf("%d %s", b.ErrCode, b.ErrMsg)
}
return b.ErrMsg
}

// Error is used for implementing error interface
func (b BizError) Error() string {
return b.String()
return b.ErrMsg
}

func HandleBadRequestErr(err error) {
Expand Down
6 changes: 1 addition & 5 deletions framework/rest/bizerror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestWithStatusCode(t *testing.T) {
So(bizError.StatusCode, ShouldEqual, 401)

Convey("Should output Unauthorised", func() {
So(bizError.String(), ShouldEqual, "Unauthorised")
So(bizError.Error(), ShouldEqual, "Unauthorised")
})
})
}
Expand All @@ -28,9 +28,5 @@ func TestWithErrCode(t *testing.T) {
Convey("Should output 100401 Unauthorised", func() {
So(bizError.String(), ShouldEqual, "100401 Unauthorised")
})

Convey("Should have the same output with String()", func() {
So(bizError.Error(), ShouldEqual, bizError.String())
})
})
}
36 changes: 25 additions & 11 deletions framework/rest/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rest
import (
"context"
"crypto/subtle"
"encoding/json"
"fmt"
"github.com/apolloconfig/agollo/v4/storage"
"github.com/ascarter/requestid"
Expand Down Expand Up @@ -232,23 +233,36 @@ func recovery(inner http.Handler) http.Handler {
defer func() {
if e := recover(); e != nil {
statusCode := http.StatusInternalServerError
respErr := fmt.Sprintf("%v", e)
errCode := 1 // 1 indicates there is an error
message := fmt.Sprintf("%v", e)
if err, ok := e.(error); ok {
if errors.Is(err, context.Canceled) {
switch {
case errors.Is(err, context.Canceled):
statusCode = http.StatusBadRequest
} else {
var bizErr BizError
if errors.As(err, &bizErr) {
statusCode = bizErr.StatusCode
if bizErr.Cause != nil {
e = bizErr.Cause
}
respErr = bizErr.Error()
default:
var bizError BizError
if errors.As(err, &bizError) {
statusCode = bizError.StatusCode
errCode = bizError.ErrCode
message = bizError.Error()
}
}
}
w.WriteHeader(statusCode)
if stringutils.IsEmpty(message) {
message = http.StatusText(statusCode)
}
logger.Error().Msgf("panic: %+v\n\nstacktrace from panic: %s\n", e, string(debug.Stack()))
http.Error(w, respErr, statusCode)
if _err := json.NewEncoder(w).Encode(struct {
Code int `json:"code"`
Message string `json:"message"`
}{
Code: errCode,
Message: message,
}); _err != nil {
http.Error(w, _err.Error(), http.StatusInternalServerError)
return
}
}
}()
inner.ServeHTTP(w, r)
Expand Down