Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea
.DS_Store
49 changes: 49 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package fn

type statusCodeError struct {
err error
statusCode int
}

type StatusCodeError interface {
StatusCode() int
}

func (s *statusCodeError) StatusCode() int {
return s.statusCode
}

func (s *statusCodeError) Unwrap() error {
return s.err
}

func (s *statusCodeError) Error() string {
return s.err.Error()
}

func UnwrapErrorStatusCode(err error) (int, bool) {
for err != nil {
if v, ok := err.(StatusCodeError); ok {
return v.StatusCode(), true
}
err = Unwrap(err)
}
return 0, false
}

func ErrorWithStatusCode(err error, statusCode int) error {
return &statusCodeError{err, statusCode}
}
63 changes: 63 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package fn

import (
"errors"
"net/http"
"testing"

. "github.com/pingcap/check"
)

var (
errTest = errors.New("test")
)

type withError struct {
err error
}

func (w *withError) Error() string {
return w.err.Error()
}

func (w *withError) Unwrap() error {
return w.err
}

type errSuite struct{}

func TestError(t *testing.T) {
TestingT(t)
}

var _ = Suite(&errSuite{})

// TestWithError test UnwrapErrorStatusCode method
func (e *errSuite) TestWithError(c *C) {
var err error = &withError{
err: ErrorWithStatusCode(errTest, http.StatusInternalServerError),
}
code, ok := UnwrapErrorStatusCode(err)
c.Assert(ok, IsTrue)
c.Assert(code == http.StatusInternalServerError, IsTrue)
}

// TestOriginError ErrorWithStatusCode Unwrap
func (e *errSuite) TestOriginError(c *C) {
err := ErrorWithStatusCode(errTest, http.StatusInternalServerError)
err = Unwrap(err)
c.Assert(err == errTest, IsTrue)
}
22 changes: 22 additions & 0 deletions go113.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//+build go1.13

// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package fn

import "errors"

func Unwrap(err error) error {
return errors.Unwrap(err)
}
4 changes: 0 additions & 4 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,3 @@ func SetResponseEncoder(c ResponseEncoder) {
func SetMultipartFormMaxMemory(m int64) {
maxMemory = m
}

func ErrorWithStatusCode(err error, statusCode int) error {
return &statusCodeError{err, statusCode}
}
28 changes: 28 additions & 0 deletions pre_go113.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//+build !go1.13

// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package fn

type wrapError interface {
Unwrap() error
}

func Unwrap(err error) error {
u, ok := err.(wrapError)
if !ok {
return nil
}
return u.Unwrap()
}
23 changes: 5 additions & 18 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ package fn

import (
"context"
"reflect"
"encoding/json"
"net/http"
"reflect"
)

type (
Expand All @@ -32,40 +32,27 @@ type (
plugins []PluginFunc
adapter adapter
}

statusCodeError struct {
error
statusCode int
}

StatusCodeError interface {
StatusCode() int
}
)

var (
errorEncoder ErrorEncoder
responseEncoder ResponseEncoder
)

func (s *statusCodeError) StatusCode() int {
return s.statusCode
}

func failure(ctx context.Context, w http.ResponseWriter, err error) {
statusCode := http.StatusBadRequest
if v, ok := err.(StatusCodeError); ok {
statusCode = v.StatusCode()
if v, ok := UnwrapErrorStatusCode(err); ok {
statusCode = v
}
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(errorEncoder(ctx, err))
_ = json.NewEncoder(w).Encode(errorEncoder(ctx, err))
}

func success(ctx context.Context, w http.ResponseWriter, data interface{}) {
if data == nil || (reflect.ValueOf(data).Kind() == reflect.Ptr && reflect.ValueOf(data).IsNil()) {
w.WriteHeader(http.StatusNoContent)
} else {
json.NewEncoder(w).Encode(responseEncoder(ctx, data))
_ = json.NewEncoder(w).Encode(responseEncoder(ctx, data))
}
}

Expand Down
5 changes: 3 additions & 2 deletions wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *fnSuite) TestSetResponseEncoder(c *C) {
c.Assert(err, IsNil)
handler.ServeHTTP(recorder, request)
respMsg := &testResponse{}
json.Unmarshal(recorder.Body.Bytes(), &respMsg)
_ = json.Unmarshal(recorder.Body.Bytes(), &respMsg)
c.Assert(reflect.DeepEqual(respMsg, testResp), IsTrue)
}

Expand All @@ -195,7 +195,7 @@ func (s *fnSuite) TestSetErrorEncoder(c *C) {
handler.ServeHTTP(recorder, request)

respMsg := &testErrorResponse{}
json.Unmarshal(recorder.Body.Bytes(), &respMsg)
_ = json.Unmarshal(recorder.Body.Bytes(), &respMsg)
c.Assert(reflect.DeepEqual(respMsg, testErrorResp), IsTrue)
}

Expand All @@ -207,6 +207,7 @@ func (s *fnSuite) TestGenericAdapter_Invoke(c *C) {

recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
c.Assert(err == nil, IsTrue)
payload := []byte(`{"for":"hello", "bar":10000}`)
request.Body = ioutil.NopCloser(bytes.NewBuffer(payload))
c.Assert(err, IsNil)
Expand Down