Skip to content

Commit e9275f3

Browse files
committed
Use iter.Seq2 to allow errors to be accessed by calling code in downstream provider implementation
1 parent 66d2254 commit e9275f3

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

tfprotov6/state_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type ReadStateBytesStream struct {
5757
}
5858

5959
type WriteStateBytesStream struct {
60-
Chunks iter.Seq[WriteStateByteChunk]
60+
Chunks iter.Seq2[WriteStateByteChunk, error]
6161
}
6262

6363
type WriteStateBytesResponse struct {

tfprotov6/tf6server/server.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,30 +1644,19 @@ func (s *server) WriteStateBytes(srv grpc.ClientStreamingServer[tfplugin6.WriteS
16441644
return err
16451645
}
16461646

1647-
iterator := func(yield func(tfprotov6.WriteStateByteChunk) bool) {
1647+
// Trying an aproach using iter.Seq2
1648+
iterator := func(yield func(tfprotov6.WriteStateByteChunk, error) bool) {
16481649
for {
16491650
chunk, err := srv.Recv()
16501651
if err == io.EOF {
16511652
break
16521653
}
16531654
if err != nil {
1654-
// attempt to send the error back to client
1655-
msgErr := srv.SendMsg(&tfplugin6.WriteStateBytes_Response{
1656-
Diagnostics: toproto.Diagnostics([]*tfprotov6.Diagnostic{
1657-
{
1658-
Severity: tfprotov6.DiagnosticSeverityError,
1659-
Summary: "Writing state chunk failed",
1660-
Detail: fmt.Sprintf("Attempt to write a byte chunk of state %q to %q failed: %s",
1661-
chunk.StateId, chunk.TypeName, err),
1662-
},
1663-
}),
1664-
})
1665-
if msgErr != nil {
1666-
err := status.Error(codes.Unimplemented, "ProviderServer does not implement WriteStateBytes")
1667-
logging.ProtocolError(ctx, err.Error())
1668-
return
1669-
}
1670-
return
1655+
logging.ProtocolError(ctx, fmt.Sprintf(
1656+
"WriteStateBytes experienced an error when receiving state data from Terraform: %s",
1657+
err,
1658+
),
1659+
)
16711660
}
16721661

16731662
ok := yield(tfprotov6.WriteStateByteChunk{
@@ -1677,11 +1666,10 @@ func (s *server) WriteStateBytes(srv grpc.ClientStreamingServer[tfplugin6.WriteS
16771666
Start: chunk.Range.Start,
16781667
End: chunk.Range.End,
16791668
},
1680-
})
1669+
}, err)
16811670
if !ok {
16821671
return
16831672
}
1684-
16851673
}
16861674
}
16871675

0 commit comments

Comments
 (0)