Skip to content

Commit 7b3f73d

Browse files
committed
Create custom error for fulfilling a pending message without losing track of failed events
1 parent abdf7e9 commit 7b3f73d

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

errors.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package appsync
22

3-
import "github.com/exanubes/appsync/internal/app"
3+
import (
4+
"github.com/exanubes/appsync/internal/app"
5+
"github.com/exanubes/appsync/internal/app/protocol"
6+
)
47

58
var (
69
ErrEmptyUrl = app.ErrEmptyUrl
@@ -11,4 +14,5 @@ var (
1114
ErrSubscriptionNotFound = app.ErrSubscriptionNotFound
1215
ErrHeartbeatTimeout = app.ErrHeartbeatTimeout
1316
ErrConnectionClosed = app.ErrConnectionClosed
17+
ErrBatchPublishFailed = protocol.BatchPublishError{}
1418
)

internal/app/protocol/protocol.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package protocol
22

3-
import "github.com/exanubes/appsync/internal/app"
3+
import (
4+
"fmt"
5+
6+
"github.com/exanubes/appsync/internal/app"
7+
)
48

59
type KeepAliveMessage struct{}
610

@@ -46,3 +50,16 @@ type FailedEvent struct {
4650
Index int
4751
RawMessage []byte
4852
}
53+
54+
type BatchPublishError struct {
55+
Failures []FailedEvent
56+
}
57+
58+
func (err BatchPublishError) Error() string {
59+
return fmt.Sprintf("batch publish failed: %d event(s) rejected", len(err.Failures))
60+
}
61+
62+
func (err BatchPublishError) Is(target error) bool {
63+
_, ok := target.(BatchPublishError)
64+
return ok
65+
}

internal/app/router/router.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ func (router *MessageHandler) Handle(ctx context.Context, msg app.Message) error
3535
return router.pending.Fulfill(ctx, msg.ID, nil)
3636

3737
case protocol.PublishResultMessage:
38-
// TODO: When publishing a batch of events, need to be able to return an error per failed event or nil
39-
return router.pending.Fulfill(ctx, msg.ID, nil)
40-
38+
if len(msg.Failures) == 0 {
39+
return router.pending.Fulfill(ctx, msg.ID, nil)
40+
}
41+
return router.pending.Fulfill(ctx, msg.ID, protocol.BatchPublishError{Failures: msg.Failures})
4142
case protocol.DataMessage:
4243
return router.receive_use_case.Execute(ctx, msg)
4344
}

0 commit comments

Comments
 (0)