|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/slack-go/slack" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +// TestSlackErrorStringUnmarshal reproduces the bug where Slack API returns |
| 12 | +// an "errors" field as an array of strings instead of objects. |
| 13 | +// Error: json: cannot unmarshal string into Go struct field |
| 14 | +// chatResponseFull.SlackResponse.errors of type map[string]interface{} |
| 15 | +func TestSlackErrorStringUnmarshal(t *testing.T) { |
| 16 | + // This is what Slack sometimes returns - errors as array of strings |
| 17 | + jsonWithStringErrors := `{ |
| 18 | + "ok": false, |
| 19 | + "error": "invalid_blocks", |
| 20 | + "errors": ["block_validation_error"] |
| 21 | + }` |
| 22 | + |
| 23 | + // This response embeds SlackResponse just like chatResponseFull does |
| 24 | + type testResponse struct { |
| 25 | + Channel string `json:"channel"` |
| 26 | + slack.SlackResponse |
| 27 | + } |
| 28 | + |
| 29 | + var resp testResponse |
| 30 | + err := json.Unmarshal([]byte(jsonWithStringErrors), &resp) |
| 31 | + |
| 32 | + // This currently FAILS with: |
| 33 | + // json: cannot unmarshal string into Go struct field |
| 34 | + // testResponse.SlackResponse.errors of type map[string]interface{} |
| 35 | + assert.NoError(t, err, "Should handle string errors from Slack API") |
| 36 | + assert.False(t, resp.Ok) |
| 37 | + assert.Equal(t, "invalid_blocks", resp.Error) |
| 38 | +} |
| 39 | + |
| 40 | +// TestSlackErrorObjectUnmarshal verifies that object errors work correctly |
| 41 | +func TestSlackErrorObjectUnmarshal(t *testing.T) { |
| 42 | + // This is the normal case where errors are objects |
| 43 | + jsonWithObjectErrors := `{ |
| 44 | + "ok": false, |
| 45 | + "error": "invalid_blocks", |
| 46 | + "errors": [{"pointer": "/blocks/0", "message": "Invalid block"}] |
| 47 | + }` |
| 48 | + |
| 49 | + type testResponse struct { |
| 50 | + Channel string `json:"channel"` |
| 51 | + slack.SlackResponse |
| 52 | + } |
| 53 | + |
| 54 | + var resp testResponse |
| 55 | + err := json.Unmarshal([]byte(jsonWithObjectErrors), &resp) |
| 56 | + |
| 57 | + assert.NoError(t, err, "Should handle object errors from Slack API") |
| 58 | + assert.False(t, resp.Ok) |
| 59 | + assert.Equal(t, "invalid_blocks", resp.Error) |
| 60 | +} |
0 commit comments