✨ Add a few common responses
This commit is contained in:
parent
c0dc44e28e
commit
4ad19029d2
2 changed files with 85 additions and 0 deletions
28
new.go
Normal file
28
new.go
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
package errors
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
// Represents a 404 error.
|
||||||
|
func NewNotFound(format string, parts ...any) SettableError {
|
||||||
|
if format == "" {
|
||||||
|
format = "Not Found"
|
||||||
|
}
|
||||||
|
|
||||||
|
return Errorf(http.StatusNotFound, format, parts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Represents a 400 error.
|
||||||
|
func NewBadRequest(format string, parts ...any) SettableError {
|
||||||
|
if format == "" {
|
||||||
|
format = "Bad Request"
|
||||||
|
}
|
||||||
|
|
||||||
|
return Errorf(http.StatusBadRequest, format, parts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Represents a 500 error. For this error, the user error is preset to "Unknown Error".
|
||||||
|
func NewInternalError(format string, parts ...any) SettableError {
|
||||||
|
status := http.StatusInternalServerError
|
||||||
|
msg := "Unknown Error"
|
||||||
|
return Errorf(status, format, parts...).Msg(msg)
|
||||||
|
}
|
||||||
57
new_test.go
Normal file
57
new_test.go
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNew(t *testing.T) {
|
||||||
|
suite.Run(t, new(NewTestSuite))
|
||||||
|
}
|
||||||
|
|
||||||
|
type NewTestSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NewTestSuite) TestNotFound() {
|
||||||
|
msg := "I can't see you"
|
||||||
|
var err ResponsableError = NewNotFound(msg)
|
||||||
|
s.Assert().NotNil(err)
|
||||||
|
s.Assert().Equal(http.StatusNotFound, err.GetStatus())
|
||||||
|
s.Assert().Equal(msg, err.GetMsg())
|
||||||
|
s.Assert().Equal(msg, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NewTestSuite) TestNotFoundDefaultMsg() {
|
||||||
|
msg := "Not Found"
|
||||||
|
var err ResponsableError = NewNotFound("")
|
||||||
|
s.Assert().NotNil(err)
|
||||||
|
s.Assert().Equal(msg, err.GetMsg())
|
||||||
|
s.Assert().Equal(msg, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NewTestSuite) TestBadRequest() {
|
||||||
|
msg := "I can't see you"
|
||||||
|
var err ResponsableError = NewBadRequest(msg)
|
||||||
|
s.Assert().NotNil(err)
|
||||||
|
s.Assert().Equal(http.StatusBadRequest, err.GetStatus())
|
||||||
|
s.Assert().Equal(msg, err.GetMsg())
|
||||||
|
s.Assert().Equal(msg, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NewTestSuite) TestBadRequestDefaultMsg() {
|
||||||
|
msg := "Bad Request"
|
||||||
|
var err ResponsableError = NewBadRequest("")
|
||||||
|
s.Assert().NotNil(err)
|
||||||
|
s.Assert().Equal(msg, err.GetMsg())
|
||||||
|
s.Assert().Equal(msg, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NewTestSuite) TestInternal() {
|
||||||
|
var err ResponsableError = NewInternalError("%d > %d", 42, 13)
|
||||||
|
s.Assert().NotNil(err)
|
||||||
|
s.Assert().Equal("42 > 13", err.Error())
|
||||||
|
s.Assert().Equal("Unknown Error", err.GetMsg())
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue