♻️ Use standard getter/setter names
This commit is contained in:
parent
7fd3192f51
commit
f27c669aeb
5 changed files with 41 additions and 37 deletions
12
errorf.go
12
errorf.go
|
|
@ -14,7 +14,7 @@ import (
|
||||||
//
|
//
|
||||||
// If you want a different user message, use Msg, such as:
|
// If you want a different user message, use Msg, such as:
|
||||||
//
|
//
|
||||||
// err := errors.Errorf(http.StatusNotFound, "%w", sqlError).Msg("user not found %d", userId)
|
// err := errors.Errorf(http.StatusNotFound, "%w", sqlError).SetMsg("user not found %d", userId)
|
||||||
func Errorf(status int, format string, parts ...any) SettableError {
|
func Errorf(status int, format string, parts ...any) SettableError {
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
return &erf{status, format, ""}
|
return &erf{status, format, ""}
|
||||||
|
|
@ -41,7 +41,7 @@ type erf struct {
|
||||||
msg string
|
msg string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *erf) GetStatus() int {
|
func (e *erf) Status() int {
|
||||||
if e.stat < http.StatusContinue || e.stat >= 600 {
|
if e.stat < http.StatusContinue || e.stat >= 600 {
|
||||||
e.stat = http.StatusInternalServerError
|
e.stat = http.StatusInternalServerError
|
||||||
}
|
}
|
||||||
|
|
@ -52,15 +52,15 @@ func (e *erf) Error() string {
|
||||||
return e.err
|
return e.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *erf) GetMsg() string {
|
func (e *erf) Msg() string {
|
||||||
if e.msg == "" {
|
if e.msg == "" {
|
||||||
return e.err
|
return e.err
|
||||||
}
|
}
|
||||||
return e.msg
|
return e.msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *erf) Status(status int) SettableError {
|
func (e *erf) SetStatus(status int) SettableError {
|
||||||
// GetStatus already handles invalid values, so we'll just ignore this.
|
// Status already handles invalid values, so we'll just ignore this.
|
||||||
if status < http.StatusContinue || status >= 600 {
|
if status < http.StatusContinue || status >= 600 {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
@ -68,7 +68,7 @@ func (e *erf) Status(status int) SettableError {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *erf) Msg(msg string, parts ...any) SettableError {
|
func (e *erf) SetMsg(msg string, parts ...any) SettableError {
|
||||||
e.msg = msg
|
e.msg = msg
|
||||||
if len(parts) > 0 {
|
if len(parts) > 0 {
|
||||||
e.msg = fmt.Sprintf(msg, parts...)
|
e.msg = fmt.Sprintf(msg, parts...)
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,8 @@ func (s *ErrorfTestSuite) TestNoWrap() {
|
||||||
|
|
||||||
exp := "42 is more than 13"
|
exp := "42 is more than 13"
|
||||||
s.Assert().Equal(exp, err.Error())
|
s.Assert().Equal(exp, err.Error())
|
||||||
s.Assert().Equal(exp, err.GetMsg())
|
s.Assert().Equal(exp, err.Msg())
|
||||||
s.Assert().Equal(http.StatusTeapot, err.GetStatus())
|
s.Assert().Equal(http.StatusTeapot, err.Status())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ErrorfTestSuite) TestWrapOne() {
|
func (s *ErrorfTestSuite) TestWrapOne() {
|
||||||
|
|
@ -45,7 +45,7 @@ func (s *ErrorfTestSuite) TestWrapOne() {
|
||||||
|
|
||||||
exp := "I'm a little teapot. Here is my handle."
|
exp := "I'm a little teapot. Here is my handle."
|
||||||
s.Assert().Equal(exp, we.Error())
|
s.Assert().Equal(exp, we.Error())
|
||||||
s.Assert().Equal(exp, we.GetMsg())
|
s.Assert().Equal(exp, we.Msg())
|
||||||
s.Assert().Same(wrapped, we.Unwrap())
|
s.Assert().Same(wrapped, we.Unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,7 +63,7 @@ func (s *ErrorfTestSuite) TestWrapTwo() {
|
||||||
|
|
||||||
exp := "I'm a little teapot: short and stout. Here is my handle."
|
exp := "I'm a little teapot: short and stout. Here is my handle."
|
||||||
s.Assert().Equal(exp, we.Error())
|
s.Assert().Equal(exp, we.Error())
|
||||||
s.Assert().Equal(exp, we.GetMsg())
|
s.Assert().Equal(exp, we.Msg())
|
||||||
unwrapped := we.Unwrap()
|
unwrapped := we.Unwrap()
|
||||||
s.Assert().Len(unwrapped, 2)
|
s.Assert().Len(unwrapped, 2)
|
||||||
s.Assert().Same(wrappedOne, unwrapped[0])
|
s.Assert().Same(wrappedOne, unwrapped[0])
|
||||||
|
|
@ -74,31 +74,31 @@ func (s *ErrorfTestSuite) TestSet() {
|
||||||
var err SettableError = Errorf(http.StatusTeapot, "Unable to BREW")
|
var err SettableError = Errorf(http.StatusTeapot, "Unable to BREW")
|
||||||
s.Assert().NotNil(err)
|
s.Assert().NotNil(err)
|
||||||
|
|
||||||
err.Status(http.StatusTooEarly).Msg("It's only %dAM", 2)
|
err.SetStatus(http.StatusTooEarly).SetMsg("It's only %dAM", 2)
|
||||||
s.Assert().Equal(http.StatusTooEarly, err.GetStatus())
|
s.Assert().Equal(http.StatusTooEarly, err.Status())
|
||||||
s.Assert().Equal("It's only 2AM", err.GetMsg())
|
s.Assert().Equal("It's only 2AM", err.Msg())
|
||||||
s.Assert().Equal("Unable to BREW", err.Error())
|
s.Assert().Equal("Unable to BREW", err.Error())
|
||||||
|
|
||||||
err.Msg("I am so great")
|
err.SetMsg("I am so great")
|
||||||
s.Assert().Equal("I am so great", err.GetMsg())
|
s.Assert().Equal("I am so great", err.Msg())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ErrorfTestSuite) TestGetStatusOutsideRange() {
|
func (s *ErrorfTestSuite) TestGetStatusOutsideRange() {
|
||||||
var err ResponsableError = Errorf(5, "Hello")
|
var err ResponsableError = Errorf(5, "Hello")
|
||||||
s.Assert().NotNil(err)
|
s.Assert().NotNil(err)
|
||||||
s.Assert().Equal(http.StatusInternalServerError, err.GetStatus())
|
s.Assert().Equal(http.StatusInternalServerError, err.Status())
|
||||||
|
|
||||||
err = Errorf(5, "Hello")
|
err = Errorf(5, "Hello")
|
||||||
s.Assert().NotNil(err)
|
s.Assert().NotNil(err)
|
||||||
s.Assert().Equal(http.StatusInternalServerError, err.GetStatus())
|
s.Assert().Equal(http.StatusInternalServerError, err.Status())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ErrorfTestSuite) TestSetStatusOutsideRange() {
|
func (s *ErrorfTestSuite) TestSetStatusOutsideRange() {
|
||||||
var err SettableError = Errorf(http.StatusPaymentRequired, "Hello")
|
var err SettableError = Errorf(http.StatusPaymentRequired, "Hello")
|
||||||
s.Assert().NotNil(err)
|
s.Assert().NotNil(err)
|
||||||
|
|
||||||
err.Status(10)
|
err.SetStatus(10)
|
||||||
s.Assert().Equal(http.StatusPaymentRequired, err.GetStatus())
|
s.Assert().Equal(http.StatusPaymentRequired, err.Status())
|
||||||
err.Status(600)
|
err.SetStatus(600)
|
||||||
s.Assert().Equal(http.StatusPaymentRequired, err.GetStatus())
|
s.Assert().Equal(http.StatusPaymentRequired, err.Status())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
interface.go
20
interface.go
|
|
@ -3,25 +3,29 @@ package errors
|
||||||
// ResponsableError is an error that has information useful in an HTTP response.
|
// ResponsableError is an error that has information useful in an HTTP response.
|
||||||
// The string returned by Error should be suitable for logging useful information
|
// The string returned by Error should be suitable for logging useful information
|
||||||
// to assist debugging the error.
|
// to assist debugging the error.
|
||||||
// GetStatus should return an appropriate HTTP status.
|
//
|
||||||
// GetMsg should return a message suitable to display to the end user. If the message
|
// Status should return an appropriate HTTP status. It must not return < 100 || >= 600.
|
||||||
|
//
|
||||||
|
// Msg should return a message suitable to display to the end user. If the message
|
||||||
// returned by Error is safe for the end user, it may simply call that.
|
// returned by Error is safe for the end user, it may simply call that.
|
||||||
// GetStatus should not return a value outside of the 100 - 599 range.
|
|
||||||
type ResponsableError interface {
|
type ResponsableError interface {
|
||||||
Error() string
|
Error() string
|
||||||
GetStatus() int
|
Status() int
|
||||||
GetMsg() string
|
Msg() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// SettableError is a ResponsableError which can be modified after initial creation.
|
// SettableError is a ResponsableError which can be modified after initial creation.
|
||||||
// The Status method can set the status, while the Msg method can set user message.
|
//
|
||||||
|
// The SetStatus method can set the status, while the SetMsg method can set user message.
|
||||||
// If any values are passed after the message, it should be passed to fmt.Sprintf for formatting.
|
// If any values are passed after the message, it should be passed to fmt.Sprintf for formatting.
|
||||||
|
//
|
||||||
// Methods are chainable.
|
// Methods are chainable.
|
||||||
|
//
|
||||||
// SetStatus should not use a value outside of 100-599. It may either ignore such values, or panic.
|
// SetStatus should not use a value outside of 100-599. It may either ignore such values, or panic.
|
||||||
type SettableError interface {
|
type SettableError interface {
|
||||||
ResponsableError
|
ResponsableError
|
||||||
Status(int) SettableError
|
SetStatus(int) SettableError
|
||||||
Msg(string, ...any) SettableError
|
SetMsg(string, ...any) SettableError
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnwrappableError allows a ResponsableError to wrap another error.
|
// UnwrappableError allows a ResponsableError to wrap another error.
|
||||||
|
|
|
||||||
2
new.go
2
new.go
|
|
@ -24,5 +24,5 @@ func NewBadRequest(format string, parts ...any) SettableError {
|
||||||
func NewInternalError(format string, parts ...any) SettableError {
|
func NewInternalError(format string, parts ...any) SettableError {
|
||||||
status := http.StatusInternalServerError
|
status := http.StatusInternalServerError
|
||||||
msg := "Unknown Error"
|
msg := "Unknown Error"
|
||||||
return Errorf(status, format, parts...).Msg(msg)
|
return Errorf(status, format, parts...).SetMsg(msg)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
new_test.go
14
new_test.go
|
|
@ -19,8 +19,8 @@ func (s *NewTestSuite) TestNotFound() {
|
||||||
msg := "I can't see you"
|
msg := "I can't see you"
|
||||||
var err ResponsableError = NewNotFound(msg)
|
var err ResponsableError = NewNotFound(msg)
|
||||||
s.Assert().NotNil(err)
|
s.Assert().NotNil(err)
|
||||||
s.Assert().Equal(http.StatusNotFound, err.GetStatus())
|
s.Assert().Equal(http.StatusNotFound, err.Status())
|
||||||
s.Assert().Equal(msg, err.GetMsg())
|
s.Assert().Equal(msg, err.Msg())
|
||||||
s.Assert().Equal(msg, err.Error())
|
s.Assert().Equal(msg, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -28,7 +28,7 @@ func (s *NewTestSuite) TestNotFoundDefaultMsg() {
|
||||||
msg := "Not Found"
|
msg := "Not Found"
|
||||||
var err ResponsableError = NewNotFound("")
|
var err ResponsableError = NewNotFound("")
|
||||||
s.Assert().NotNil(err)
|
s.Assert().NotNil(err)
|
||||||
s.Assert().Equal(msg, err.GetMsg())
|
s.Assert().Equal(msg, err.Msg())
|
||||||
s.Assert().Equal(msg, err.Error())
|
s.Assert().Equal(msg, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,8 +36,8 @@ func (s *NewTestSuite) TestBadRequest() {
|
||||||
msg := "I can't see you"
|
msg := "I can't see you"
|
||||||
var err ResponsableError = NewBadRequest(msg)
|
var err ResponsableError = NewBadRequest(msg)
|
||||||
s.Assert().NotNil(err)
|
s.Assert().NotNil(err)
|
||||||
s.Assert().Equal(http.StatusBadRequest, err.GetStatus())
|
s.Assert().Equal(http.StatusBadRequest, err.Status())
|
||||||
s.Assert().Equal(msg, err.GetMsg())
|
s.Assert().Equal(msg, err.Msg())
|
||||||
s.Assert().Equal(msg, err.Error())
|
s.Assert().Equal(msg, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,7 +45,7 @@ func (s *NewTestSuite) TestBadRequestDefaultMsg() {
|
||||||
msg := "Bad Request"
|
msg := "Bad Request"
|
||||||
var err ResponsableError = NewBadRequest("")
|
var err ResponsableError = NewBadRequest("")
|
||||||
s.Assert().NotNil(err)
|
s.Assert().NotNil(err)
|
||||||
s.Assert().Equal(msg, err.GetMsg())
|
s.Assert().Equal(msg, err.Msg())
|
||||||
s.Assert().Equal(msg, err.Error())
|
s.Assert().Equal(msg, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,5 +53,5 @@ func (s *NewTestSuite) TestInternal() {
|
||||||
var err ResponsableError = NewInternalError("%d > %d", 42, 13)
|
var err ResponsableError = NewInternalError("%d > %d", 42, 13)
|
||||||
s.Assert().NotNil(err)
|
s.Assert().NotNil(err)
|
||||||
s.Assert().Equal("42 > 13", err.Error())
|
s.Assert().Equal("42 > 13", err.Error())
|
||||||
s.Assert().Equal("Unknown Error", err.GetMsg())
|
s.Assert().Equal("Unknown Error", err.Msg())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue