Skip to content

Commit

Permalink
add New and Newf for HTTPError and remove NewErrorf
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Sep 17, 2020
1 parent 43036f6 commit dfa8032
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
51 changes: 28 additions & 23 deletions herror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ func (e HTTPError) Error() string {
// Unwrap unwraps the inner error.
func (e HTTPError) Unwrap() error { return e.Err }

// NewCT returns a new HTTPError with the new ContentType ct.
func (e HTTPError) NewCT(ct string) HTTPError { e.CT = ct; return e }

// New returns a new HTTPError with the new error.
func (e HTTPError) New(err error) HTTPError { e.Err = err; return e }

// Newf is equal to New(fmt.Errorf(msg, args...)).
func (e HTTPError) Newf(msg string, args ...interface{}) HTTPError {
if len(args) == 0 {
return e.NewError(errors.New(msg))
}
return e.NewError(fmt.Errorf(msg, args...))
}

// GetMsg returns a message.
//
// DEPRECATED!!!
func (e HTTPError) GetMsg() string {
if e.Msg != "" {
return e.Msg
} else if e.Code < 500 && e.Err != nil {
return e.Err.Error()
}
return ""
}

// GetError returns the inner error.
//
// If Err is nil but Msg is not "", return `errors.New(e.Msg)` instead;
Expand All @@ -104,35 +130,14 @@ func (e HTTPError) GetError() error {
return nil
}

// GetMsg returns a message.
// NewError returns a new HTTPError with the new error.
//
// DEPRECATED!!!
func (e HTTPError) GetMsg() string {
if e.Msg != "" {
return e.Msg
} else if e.Code < 500 && e.Err != nil {
return e.Err.Error()
}
return ""
}

// NewCT returns a new HTTPError with the new ContentType ct.
func (e HTTPError) NewCT(ct string) HTTPError { e.CT = ct; return e }

// NewError returns a new HTTPError with the new error.
func (e HTTPError) NewError(err error) HTTPError { e.Err = err; return e }

// NewErrorf is equal to NewError(fmt.Errorf(msg, args...)).
func (e HTTPError) NewErrorf(msg string, args ...interface{}) HTTPError {
if len(args) == 0 {
return e.NewError(errors.New(msg))
}
return e.NewError(fmt.Errorf(msg, args...))
}

// NewMsg returns a new HTTPError with the new msg.
//
// DEPRECATED!!! Please use NewErrorf instead.
// DEPRECATED!!! Please use Newf instead.
func (e HTTPError) NewMsg(msg string, args ...interface{}) HTTPError {
if len(args) == 0 {
e.Msg = msg
Expand Down
2 changes: 1 addition & 1 deletion middleware/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func CSRF(config ...CSRFConfig) Middleware {
return ship.ErrBadRequest.NewError(err)
}
if !validateCSRFToken(token, clientToken) {
return ship.ErrForbidden.NewErrorf("invalid csrf token")
return ship.ErrForbidden.Newf("invalid csrf token")
}
}

Expand Down

0 comments on commit dfa8032

Please sign in to comment.