errors_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "errors"
  7. "testing"
  8. "github.com/gin-gonic/gin/internal/json"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestError(t *testing.T) {
  12. baseError := errors.New("test error")
  13. err := &Error{
  14. Err: baseError,
  15. Type: ErrorTypePrivate,
  16. }
  17. assert.Equal(t, err.Error(), baseError.Error())
  18. assert.Equal(t, H{"error": baseError.Error()}, err.JSON())
  19. assert.Equal(t, err.SetType(ErrorTypePublic), err)
  20. assert.Equal(t, ErrorTypePublic, err.Type)
  21. assert.Equal(t, err.SetMeta("some data"), err)
  22. assert.Equal(t, "some data", err.Meta)
  23. assert.Equal(t, H{
  24. "error": baseError.Error(),
  25. "meta": "some data",
  26. }, err.JSON())
  27. jsonBytes, _ := json.Marshal(err)
  28. assert.Equal(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))
  29. err.SetMeta(H{ // nolint: errcheck
  30. "status": "200",
  31. "data": "some data",
  32. })
  33. assert.Equal(t, H{
  34. "error": baseError.Error(),
  35. "status": "200",
  36. "data": "some data",
  37. }, err.JSON())
  38. err.SetMeta(H{ // nolint: errcheck
  39. "error": "custom error",
  40. "status": "200",
  41. "data": "some data",
  42. })
  43. assert.Equal(t, H{
  44. "error": "custom error",
  45. "status": "200",
  46. "data": "some data",
  47. }, err.JSON())
  48. type customError struct {
  49. status string
  50. data string
  51. }
  52. err.SetMeta(customError{status: "200", data: "other data"}) // nolint: errcheck
  53. assert.Equal(t, customError{status: "200", data: "other data"}, err.JSON())
  54. }
  55. func TestErrorSlice(t *testing.T) {
  56. errs := errorMsgs{
  57. {Err: errors.New("first"), Type: ErrorTypePrivate},
  58. {Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},
  59. {Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},
  60. }
  61. assert.Equal(t, errs, errs.ByType(ErrorTypeAny))
  62. assert.Equal(t, "third", errs.Last().Error())
  63. assert.Equal(t, []string{"first", "second", "third"}, errs.Errors())
  64. assert.Equal(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors())
  65. assert.Equal(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors())
  66. assert.Equal(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors())
  67. assert.Empty(t, errs.ByType(ErrorTypeBind))
  68. assert.Empty(t, errs.ByType(ErrorTypeBind).String())
  69. assert.Equal(t, `Error #01: first
  70. Error #02: second
  71. Meta: some data
  72. Error #03: third
  73. Meta: map[status:400]
  74. `, errs.String())
  75. assert.Equal(t, []interface{}{
  76. H{"error": "first"},
  77. H{"error": "second", "meta": "some data"},
  78. H{"error": "third", "status": "400"},
  79. }, errs.JSON())
  80. jsonBytes, _ := json.Marshal(errs)
  81. assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))
  82. errs = errorMsgs{
  83. {Err: errors.New("first"), Type: ErrorTypePrivate},
  84. }
  85. assert.Equal(t, H{"error": "first"}, errs.JSON())
  86. jsonBytes, _ = json.Marshal(errs)
  87. assert.Equal(t, "{\"error\":\"first\"}", string(jsonBytes))
  88. errs = errorMsgs{}
  89. assert.Nil(t, errs.Last())
  90. assert.Nil(t, errs.JSON())
  91. assert.Empty(t, errs.String())
  92. }