Skip to content

Commit 0143265

Browse files
authored
Merge pull request #134 from ljagiello/fix/slack-go-unmarshal-error
fix: update slack-go/slack to v0.17.3 to fix JSON unmarshal error
2 parents c840478 + 17e55a9 commit 0143265

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/rusq/slackauth v0.6.1
1414
github.com/rusq/slackdump/v3 v3.1.6
1515
github.com/rusq/tagops v0.1.1
16-
github.com/slack-go/slack v0.17.1
16+
github.com/slack-go/slack v0.17.3
1717
github.com/stretchr/testify v1.10.0
1818
github.com/takara2314/slack-go-util v0.2.0
1919
go.uber.org/zap v1.27.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ github.com/rusq/slackdump/v3 v3.1.6 h1:t6hi49jSDWpiXqyna8OlEd2I2zkLBgi9XZGr+xDl5
148148
github.com/rusq/slackdump/v3 v3.1.6/go.mod h1:c9AiEEkmLWIbQJuxDIK+K9H5g6kdfc06Eqk6DmLWWps=
149149
github.com/rusq/tagops v0.1.1 h1:R5MHPR822lSg3LFr0RS3DFS0CapRiqtuHVD5NlOMOvY=
150150
github.com/rusq/tagops v0.1.1/go.mod h1:mUJ5WoHxrSv9wreCrHQkAeMevt5aXFadlOdLM6UsoHc=
151-
github.com/slack-go/slack v0.17.1 h1:x0Mnc6biHBea5vfxLR+x4JFl/Rm3eIo0iS3xDZenX+o=
152-
github.com/slack-go/slack v0.17.1/go.mod h1:X+UqOufi3LYQHDnMG1vxf0J8asC6+WllXrVrhl8/Prk=
151+
github.com/slack-go/slack v0.17.3 h1:zV5qO3Q+WJAQ/XwbGfNFrRMaJ5T/naqaonyPV/1TP4g=
152+
github.com/slack-go/slack v0.17.3/go.mod h1:X+UqOufi3LYQHDnMG1vxf0J8asC6+WllXrVrhl8/Prk=
153153
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
154154
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
155155
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

pkg/handler/slack_error_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)