@@ -54,7 +54,7 @@ func (r *Transport) RoundTrip(incomingRequest *http.Request) (*http.Response, er
5454
5555 outHeaders := types .NewFields ()
5656 if err := HTTPtoWASIHeader (incomingRequest .Header , outHeaders ); err != nil {
57- return nil , err
57+ return nil , fmt . Errorf ( "failed to convert outgoing headers: %w" , err )
5858 }
5959
6060 outRequest := types .NewOutgoingRequest (outHeaders )
@@ -77,77 +77,76 @@ func (r *Transport) RoundTrip(incomingRequest *http.Request) (*http.Response, er
7777 outRequest .SetScheme (cm .Some (types .SchemeOther (incomingRequest .URL .Scheme )))
7878 }
7979
80- bodyRes := outRequest .Body ()
81- if bodyRes . IsErr () {
82- return nil , fmt .Errorf ("failed to acquire resource handle to request body: %s" , bodyRes . Err () )
80+ body , bodyErr , isErr := outRequest .Body (). Result ()
81+ if isErr {
82+ return nil , fmt .Errorf ("failed to acquire resource handle to request body: %s" , bodyErr )
8383 }
84- body := bodyRes .OK ()
8584
86- handleResp := outgoinghandler .Handle (outRequest , cm .Some (r .requestOptions ()))
87- if handleResp . Err () != nil {
88- return nil , fmt .Errorf ("%v " , handleResp . Err () )
85+ futureResponse , handlerErr , isErr := outgoinghandler .Handle (outRequest , cm .Some (r .requestOptions ())). Result ( )
86+ if isErr {
87+ return nil , fmt .Errorf ("failed to acquire handle to outbound request: %s " , handlerErr )
8988 }
9089
9190 maybeTrailers := cm .None [types.Fields ]()
9291 if len (incomingRequest .Trailer ) > 0 {
9392 outTrailers := types .NewFields ()
9493 if err := HTTPtoWASIHeader (incomingRequest .Trailer , outTrailers ); err != nil {
95- return nil , err
94+ return nil , fmt . Errorf ( "failed to convert outgoing trailers: %w" , err )
9695 }
9796 maybeTrailers = cm .Some (outTrailers )
9897 }
9998
10099 // NOTE(lxf): If request includes a body, copy it to the adapted wasi body
101100 if incomingRequest .Body != nil {
102- adaptedBody , err := NewOutgoingBody (body )
101+ // For client requests, the Transport is responsible for calling Close on request's body.
102+ defer incomingRequest .Body .Close ()
103+ adaptedBody , err := NewOutgoingBody (& body )
103104 if err != nil {
104- return nil , fmt .Errorf ("failed to adapt body: %s " , err )
105+ return nil , fmt .Errorf ("failed to adapt body: %w " , err )
105106 }
106107 if _ , err := io .Copy (adaptedBody , incomingRequest .Body ); err != nil {
107- return nil , fmt .Errorf ("failed to copy body: %v " , err )
108+ return nil , fmt .Errorf ("failed to copy body: %w " , err )
108109 }
109-
110110 if err := adaptedBody .Close (); err != nil {
111- return nil , fmt .Errorf ("failed to close body: %v " , err )
111+ return nil , fmt .Errorf ("failed to close body: %w " , err )
112112 }
113113 }
114114
115115 // From `outgoing-body` documentation:
116116 // Finalize an outgoing body, optionally providing trailers. This must be
117117 // called to signal that the response is complete.
118- outFinish := types .OutgoingBodyFinish (* body , maybeTrailers )
118+ outFinish := types .OutgoingBodyFinish (body , maybeTrailers )
119119 if outFinish .IsErr () {
120- return nil , fmt .Errorf ("failed to finish body: %v " , outFinish .Err ())
120+ return nil , fmt .Errorf ("failed to finish body: %s " , outFinish .Err ())
121121 }
122122
123- // NOTE(lxf): Request is fully sent. Processing response.
124- futureResponse := handleResp .OK ()
125-
126123 // wait until resp is returned
127124 futurePollable := futureResponse .Subscribe ()
128125 for ! futurePollable .Ready () {
129126 runtime .Gosched ()
130127 }
131128
132- pollableOption := futureResponse .Get ()
133- if pollableOption .None () {
134- return nil , fmt .Errorf ("incoming resp is None" )
129+ incomingResponseOuterOption := futureResponse .Get ()
130+ if incomingResponseOuterOption .None () {
131+ // NOTE: This should never happen since we subscribe to response readiness above
132+ return nil , fmt .Errorf ("failed to wait for future-incoming-response readiness" )
135133 }
136134
137- pollableResult := pollableOption .Some ()
138- if pollableResult .IsErr () {
139- return nil , fmt .Errorf ("error is %v" , pollableResult .Err ())
135+ // Unwrap the outer Option and the outer Result within it
136+ innerResult , outerResultErr , isErr := incomingResponseOuterOption .Some ().Result ()
137+ if isErr {
138+ return nil , fmt .Errorf ("failed to unwrap the outer result for incoming-response: %s" , outerResultErr )
140139 }
141140
142- resultOption := pollableResult .OK ()
143- if resultOption .IsErr () {
144- return nil , fmt .Errorf ("%v" , resultOption .Err ())
141+ // Unwrap the inner Result
142+ incomingResponse , innerResultErr , isErr := innerResult .Result ()
143+ if isErr {
144+ return nil , fmt .Errorf ("failed to unwrap the inner result for incoming-response: %s" , innerResultErr )
145145 }
146146
147- incomingResponse := resultOption .OK ()
148147 incomingBody , incomingTrailers , err := NewIncomingBodyTrailer (incomingResponse )
149148 if err != nil {
150- return nil , fmt .Errorf ("failed to consume incoming request %s " , err )
149+ return nil , fmt .Errorf ("failed to parse incoming-response: %w " , err )
151150 }
152151
153152 incomingHeaders := http.Header {}
0 commit comments