(t *testing.T)
| 66 | } |
| 67 | |
| 68 | func TestErrorSlice(t *testing.T) { |
| 69 | errs := errorMsgs{ |
| 70 | {Err: errors.New("first"), Type: ErrorTypePrivate}, |
| 71 | {Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"}, |
| 72 | {Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}}, |
| 73 | } |
| 74 | |
| 75 | assert.Equal(t, errs, errs.ByType(ErrorTypeAny)) |
| 76 | assert.Equal(t, "third", errs.Last().Error()) |
| 77 | assert.Equal(t, []string{"first", "second", "third"}, errs.Errors()) |
| 78 | assert.Equal(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors()) |
| 79 | assert.Equal(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors()) |
| 80 | assert.Equal(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors()) |
| 81 | assert.Empty(t, errs.ByType(ErrorTypeBind)) |
| 82 | assert.Empty(t, errs.ByType(ErrorTypeBind).String()) |
| 83 | |
| 84 | assert.Equal(t, `Error #01: first |
| 85 | Error #02: second |
| 86 | Meta: some data |
| 87 | Error #03: third |
| 88 | Meta: map[status:400] |
| 89 | `, errs.String()) |
| 90 | assert.Equal(t, []any{ |
| 91 | H{"error": "first"}, |
| 92 | H{"error": "second", "meta": "some data"}, |
| 93 | H{"error": "third", "status": "400"}, |
| 94 | }, errs.JSON()) |
| 95 | jsonBytes, _ := json.API.Marshal(errs) |
| 96 | assert.JSONEq(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes)) |
| 97 | errs = errorMsgs{ |
| 98 | {Err: errors.New("first"), Type: ErrorTypePrivate}, |
| 99 | } |
| 100 | assert.Equal(t, H{"error": "first"}, errs.JSON()) |
| 101 | jsonBytes, _ = json.API.Marshal(errs) |
| 102 | assert.JSONEq(t, "{\"error\":\"first\"}", string(jsonBytes)) |
| 103 | |
| 104 | errs = errorMsgs{} |
| 105 | assert.Nil(t, errs.Last()) |
| 106 | assert.Nil(t, errs.JSON()) |
| 107 | assert.Empty(t, errs.String()) |
| 108 | } |
| 109 | |
| 110 | type TestErr string |
| 111 |
nothing calls this directly
no test coverage detected
searching dependent graphs…