responsable-errors/errorf_test.go

127 lines
3.3 KiB
Go
Raw Permalink Normal View History

2024-01-17 10:58:39 -06:00
package errors
import (
"errors"
2024-01-21 13:50:13 -06:00
"encoding/json"
2024-01-17 10:58:39 -06:00
"net/http"
"testing"
"github.com/stretchr/testify/suite"
)
func TestErrorf(t *testing.T) {
suite.Run(t, new(ErrorfTestSuite))
}
type ErrorfTestSuite struct {
suite.Suite
}
func (s *ErrorfTestSuite) TestNoWrap() {
var err ResponsableError = Errorf(http.StatusTeapot, "%d is more than %d", 42, 13)
s.Assert().NotNil(err)
_, ok := err.(UnwrappableError)
s.Assert().False(ok)
_, ok = err.(UnwrappableErrors)
s.Assert().False(ok)
exp := "42 is more than 13"
s.Assert().Equal(exp, err.Error())
s.Assert().Equal(exp, err.Msg())
s.Assert().Equal(http.StatusTeapot, err.Status())
2024-01-17 10:58:39 -06:00
}
func (s *ErrorfTestSuite) TestWrapOne() {
var wrapped error = errors.New("Here is my handle.")
var err ResponsableError = Errorf(http.StatusTeapot, "I'm a little teapot. %w", wrapped)
s.Assert().NotNil(err)
we, ok := err.(UnwrappableError)
s.Assert().True(ok)
_, ok = err.(UnwrappableErrors)
s.Assert().False(ok)
exp := "I'm a little teapot. Here is my handle."
s.Assert().Equal(exp, we.Error())
s.Assert().Equal(exp, we.Msg())
2024-01-17 10:58:39 -06:00
s.Assert().Same(wrapped, we.Unwrap())
}
func (s *ErrorfTestSuite) TestWrapTwo() {
var wrappedOne error = errors.New("short and stout")
var wrappedTwo error = errors.New("Here is my handle.")
var err ResponsableError = Errorf(http.StatusTeapot, "I'm a little teapot: %w. %w", wrappedOne, wrappedTwo)
s.Assert().NotNil(err)
_, ok := err.(UnwrappableError)
s.Assert().False(ok)
we, ok := err.(UnwrappableErrors)
s.Assert().True(ok)
exp := "I'm a little teapot: short and stout. Here is my handle."
s.Assert().Equal(exp, we.Error())
s.Assert().Equal(exp, we.Msg())
2024-01-17 10:58:39 -06:00
unwrapped := we.Unwrap()
s.Assert().Len(unwrapped, 2)
s.Assert().Same(wrappedOne, unwrapped[0])
s.Assert().Same(wrappedTwo, unwrapped[1])
}
2024-01-17 15:51:28 -06:00
func (s *ErrorfTestSuite) TestSet() {
var err SettableError = Errorf(http.StatusTeapot, "Unable to BREW")
s.Assert().NotNil(err)
err.SetStatus(http.StatusTooEarly).SetMsg("It's only %dAM", 2)
s.Assert().Equal(http.StatusTooEarly, err.Status())
s.Assert().Equal("It's only 2AM", err.Msg())
2024-01-17 15:51:28 -06:00
s.Assert().Equal("Unable to BREW", err.Error())
err.SetMsg("I am so great")
s.Assert().Equal("I am so great", err.Msg())
2024-01-17 15:51:28 -06:00
}
func (s *ErrorfTestSuite) TestGetStatusOutsideRange() {
var err ResponsableError = Errorf(5, "Hello")
s.Assert().NotNil(err)
s.Assert().Equal(http.StatusInternalServerError, err.Status())
2024-01-17 15:51:28 -06:00
err = Errorf(5, "Hello")
s.Assert().NotNil(err)
s.Assert().Equal(http.StatusInternalServerError, err.Status())
2024-01-17 15:51:28 -06:00
}
func (s *ErrorfTestSuite) TestSetStatusOutsideRange() {
var err SettableError = Errorf(http.StatusPaymentRequired, "Hello")
s.Assert().NotNil(err)
err.SetStatus(10)
s.Assert().Equal(http.StatusPaymentRequired, err.Status())
err.SetStatus(600)
s.Assert().Equal(http.StatusPaymentRequired, err.Status())
2024-01-17 15:51:28 -06:00
}
2024-01-21 13:50:13 -06:00
func (s *ErrorfTestSuite) TestJSON() {
var err SettableError = Errorf(http.StatusPaymentRequired, "Hello")
s.Assert().NotNil(err)
m := err.JSON()
ma, _ := m.(map[string]any)
s.Assert().Equal("Hello", ma["error"])
s.Assert().Nil(ma["number"])
j, _ := json.Marshal(err)
s.Assert().Equal(`{"error":"Hello"}`, string(j))
err.SetField("number",42)
m = err.JSON()
ma, _ = m.(map[string]any)
s.Assert().Equal(42, ma["number"])
j, _ = json.Marshal(err)
s.Assert().Equal(`{"error":"Hello","number":42}`, string(j))
}