errors_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "encoding/json"
  7. "errors"
  8. "testing"
  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, err.JSON(), H{"error": baseError.Error()})
  19. assert.Equal(t, err.SetType(ErrorTypePublic), err)
  20. assert.Equal(t, err.Type, ErrorTypePublic)
  21. assert.Equal(t, err.SetMeta("some data"), err)
  22. assert.Equal(t, err.Meta, "some data")
  23. assert.Equal(t, err.JSON(), H{
  24. "error": baseError.Error(),
  25. "meta": "some data",
  26. })
  27. jsonBytes, _ := json.Marshal(err)
  28. assert.Equal(t, string(jsonBytes), "{\"error\":\"test error\",\"meta\":\"some data\"}")
  29. err.SetMeta(H{
  30. "status": "200",
  31. "data": "some data",
  32. })
  33. assert.Equal(t, err.JSON(), H{
  34. "error": baseError.Error(),
  35. "status": "200",
  36. "data": "some data",
  37. })
  38. err.SetMeta(H{
  39. "error": "custom error",
  40. "status": "200",
  41. "data": "some data",
  42. })
  43. assert.Equal(t, err.JSON(), H{
  44. "error": "custom error",
  45. "status": "200",
  46. "data": "some data",
  47. })
  48. }
  49. func TestErrorSlice(t *testing.T) {
  50. errs := errorMsgs{
  51. {Err: errors.New("first"), Type: ErrorTypePrivate},
  52. {Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},
  53. {Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},
  54. }
  55. assert.Equal(t, errs.Last().Error(), "third")
  56. assert.Equal(t, errs.Errors(), []string{"first", "second", "third"})
  57. assert.Equal(t, errs.ByType(ErrorTypePublic).Errors(), []string{"third"})
  58. assert.Equal(t, errs.ByType(ErrorTypePrivate).Errors(), []string{"first", "second"})
  59. assert.Equal(t, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors(), []string{"first", "second", "third"})
  60. assert.Empty(t, errs.ByType(ErrorTypeBind))
  61. assert.Empty(t, errs.ByType(ErrorTypeBind).String())
  62. assert.Equal(t, errs.String(), `Error #01: first
  63. Error #02: second
  64. Meta: some data
  65. Error #03: third
  66. Meta: map[status:400]
  67. `)
  68. assert.Equal(t, errs.JSON(), []interface{}{
  69. H{"error": "first"},
  70. H{"error": "second", "meta": "some data"},
  71. H{"error": "third", "status": "400"},
  72. })
  73. jsonBytes, _ := json.Marshal(errs)
  74. assert.Equal(t, string(jsonBytes), "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]")
  75. errs = errorMsgs{
  76. {Err: errors.New("first"), Type: ErrorTypePrivate},
  77. }
  78. assert.Equal(t, errs.JSON(), H{"error": "first"})
  79. jsonBytes, _ = json.Marshal(errs)
  80. assert.Equal(t, string(jsonBytes), "{\"error\":\"first\"}")
  81. errs = errorMsgs{}
  82. assert.Nil(t, errs.Last())
  83. assert.Nil(t, errs.JSON())
  84. assert.Empty(t, errs.String())
  85. }