Skip to content

Commit

Permalink
feat: adjust http error
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Dec 23, 2018
1 parent 9e5fa12 commit db5737e
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 78 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@
[[constraint]]
name = "github.com/json-iterator/go"
version = "1.1.5"

[[constraint]]
name = "github.com/vicanso/errors"
version = "0.0.2"
4 changes: 2 additions & 2 deletions cod.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cod

import (
"errors"
"net/http"
"sync"

"github.com/julienschmidt/httprouter"
"github.com/vicanso/errors"
)

var (
Expand Down Expand Up @@ -230,7 +230,7 @@ func (d *Cod) Error(c *Context, err error) {
return
}
resp := c.Response
he, ok := err.(*HTTPError)
he, ok := err.(*errors.HTTPError)
if ok {
resp.WriteHeader(he.StatusCode)
resp.Write([]byte(he.Error()))
Expand Down
21 changes: 20 additions & 1 deletion df.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cod

import "net/http"
import (
"net/http"

"github.com/vicanso/errors"
)

var (
methods = []string{
Expand All @@ -13,9 +17,24 @@ var (
http.MethodOptions,
http.MethodTrace,
}

// ErrInvalidRedirect invalid redirect
ErrInvalidRedirect = &errors.HTTPError{
StatusCode: 400,
Message: "invalid redirect",
Category: ErrCategoryCod,
}
// ErrInvalidResponse invalid response(body an status is nil)
ErrInvalidResponse = &errors.HTTPError{
StatusCode: 500,
Message: "invalid response",
Category: ErrCategoryCod,
}
)

const (
// ErrCategoryCod cod category
ErrCategoryCod = "cod"
// HeaderXForwardedFor x-forwarded-for
HeaderXForwardedFor = "X-Forwarded-For"
// HeaderXRealIp x-real-ip
Expand Down
47 changes: 0 additions & 47 deletions error.go

This file was deleted.

16 changes: 0 additions & 16 deletions error_test.go

This file was deleted.

5 changes: 3 additions & 2 deletions middleware/basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/vicanso/cod"
"github.com/vicanso/errors"
)

const (
Expand All @@ -28,8 +29,8 @@ var (
errUnauthorized = getBasicAuthError("unAuthorized", http.StatusUnauthorized)
)

func getBasicAuthError(message string, statusCode int) *cod.HTTPError {
return &cod.HTTPError{
func getBasicAuthError(message string, statusCode int) *errors.HTTPError {
return &errors.HTTPError{
StatusCode: statusCode,
Message: message,
Category: errBasicAuthCategory,
Expand Down
6 changes: 3 additions & 3 deletions middleware/basic_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestBasicAuth(t *testing.T) {
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
if resp.Code != http.StatusBadRequest ||
resp.Body.String() != "category=cod-basic-auth, status=400, message=illegal base64 data at input byte 0" {
resp.Body.String() != "category=cod-basic-auth, message=illegal base64 data at input byte 0" {
t.Fatalf("base64 decode fail error is invalid")
}
})
Expand All @@ -64,14 +64,14 @@ func TestBasicAuth(t *testing.T) {
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
if resp.Code != http.StatusUnauthorized ||
resp.Body.String() != "category=cod-basic-auth, status=401, message=unAuthorized" {
resp.Body.String() != "category=cod-basic-auth, message=unAuthorized" {
t.Fatalf("validate fail error is invalid")
}
req.Header.Set(cod.HeaderAuthorization, "basic bjph")
resp = httptest.NewRecorder()
d.ServeHTTP(resp, req)
if resp.Code != http.StatusBadRequest ||
resp.Body.String() != "category=cod-basic-auth, status=400, message=account is invalid" {
resp.Body.String() != "category=cod-basic-auth, message=account is invalid" {
t.Fatalf("validate return error is fail")
}
})
Expand Down
5 changes: 3 additions & 2 deletions middleware/body_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/vicanso/cod"
"github.com/vicanso/errors"
)

const (
Expand Down Expand Up @@ -75,15 +76,15 @@ func NewBodyParser(config BodyParserConfig) cod.Handler {

body, e := ioutil.ReadAll(c.Request.Body)
if e != nil {
err = &cod.HTTPError{
err = &errors.HTTPError{
StatusCode: http.StatusBadRequest,
Message: e.Error(),
Category: errBodyParserCategory,
}
return
}
if limit > 0 && len(body) > limit {
err = &cod.HTTPError{
err = &errors.HTTPError{
StatusCode: http.StatusBadRequest,
Message: fmt.Sprintf("requst body is %d bytes, it should be <= %d", len(body), limit),
Category: errBodyParserCategory,
Expand Down
7 changes: 4 additions & 3 deletions middleware/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

jsoniter "github.com/json-iterator/go"
"github.com/vicanso/cod"
"github.com/vicanso/errors"
)

var (
Expand All @@ -21,12 +22,12 @@ type (
func NewResponder(config ResponderConfig) cod.Handler {
return func(c *cod.Context) error {
e := c.Next()
var err *cod.HTTPError
var err *errors.HTTPError
if e != nil {
// 如果出错,尝试转换为HTTPError
he, ok := e.(*cod.HTTPError)
he, ok := e.(*errors.HTTPError)
if !ok {
he = &cod.HTTPError{
he = &errors.HTTPError{
StatusCode: http.StatusInternalServerError,
Message: e.Error(),
}
Expand Down
4 changes: 2 additions & 2 deletions middleware/responder_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package middleware

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

"github.com/vicanso/cod"
"github.com/vicanso/errors"
)

func checkResponse(t *testing.T, resp *httptest.ResponseRecorder, code int, data string) {
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestResponder(t *testing.T) {
d := cod.New()
d.Use(m)
d.GET("/", func(c *cod.Context) error {
return &cod.HTTPError{
return &errors.HTTPError{
StatusCode: 400,
Message: "abc",
Category: "custom",
Expand Down

0 comments on commit db5737e

Please sign in to comment.