-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from theothertomelliott/theothertomelliott/iss…
…ue31 Add errors to actions to allow for custom handling
- Loading branch information
Showing
18 changed files
with
266 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ package spanner | |
import "context" | ||
|
||
type Action interface { | ||
HasError | ||
|
||
Type() string | ||
Data() interface{} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package spanner | ||
|
||
import "context" | ||
|
||
type HasError interface { | ||
ErrorFunc(ErrorFunc) | ||
} | ||
|
||
type ErrorFunc func(ctx context.Context, ev ErrorEvent) | ||
|
||
type ErrorEvent interface { | ||
SendMessage(channelID string) ErrorMessage | ||
ReceiveError() error | ||
} | ||
|
||
type ErrorMessage interface { | ||
NonInteractiveMessage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package slack | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/slack-go/slack/slackevents" | ||
"github.com/theothertomelliott/spanner" | ||
) | ||
|
||
// TestErrorHandling verifies that error handlers are called appropriately | ||
func TestErrorHandling(t *testing.T) { | ||
client := newTestClient([]string{"ABC123"}) | ||
testApp := client.CreateApp() | ||
|
||
go func() { | ||
err := testApp.Run(handlerTestErrors) | ||
if err != nil { | ||
t.Errorf("error running app: %v", err) | ||
} | ||
}() | ||
|
||
// Send hello message | ||
client.SendEventToApp(messageEvent( | ||
slackevents.MessageEvent{ | ||
Text: "hello", | ||
Channel: "ABC123", | ||
User: "DEF456", | ||
}, | ||
)) | ||
|
||
// Expect a single message and clear the message list | ||
if len(client.messagesSent) != 2 { | ||
t.Errorf("expected two messages to be sent, got %d", len(client.messagesSent)) | ||
} | ||
|
||
firstBlocks, _ := json.MarshalIndent(client.messagesSent[0].blocks, "", " ") | ||
if !strings.Contains(string(firstBlocks), `This message should succeed`) { | ||
t.Errorf("first message content was not as expected, got: %v", string(firstBlocks)) | ||
} | ||
|
||
secondBlocks, _ := json.MarshalIndent(client.messagesSent[1].blocks, "", " ") | ||
if !strings.Contains(string(secondBlocks), `There was an error sending a message`) { | ||
t.Errorf("first message content was not as expected, got: %v", string(secondBlocks)) | ||
} | ||
} | ||
|
||
func handlerTestErrors(ctx context.Context, ev spanner.Event) error { | ||
if msg := ev.ReceiveMessage(); msg != nil && msg.Text() == "hello" { | ||
replyGood := ev.SendMessage(msg.Channel().ID()) | ||
replyGood.PlainText("This message should succeed") | ||
replyGood.ErrorFunc(func(ctx context.Context, ev spanner.ErrorEvent) { | ||
panic("did not expect this message to fail") | ||
}) | ||
|
||
replyBad := ev.SendMessage("invalid_channel") | ||
replyBad.PlainText("This message will always fail to post") | ||
replyBad.ErrorFunc(func(ctx context.Context, ev spanner.ErrorEvent) { | ||
errorNotice := ev.SendMessage(msg.Channel().ID()) | ||
errorNotice.PlainText(fmt.Sprintf("There was an error sending a message: %v", ev.ReceiveError())) | ||
}) | ||
|
||
replySkipped := ev.SendMessage(msg.Channel().ID()) | ||
replySkipped.PlainText("This message should be skipped because of the previous error") | ||
replySkipped.ErrorFunc(func(ctx context.Context, ev spanner.ErrorEvent) { | ||
panic("did not expect this message to fail") | ||
}) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.