@@ -74,6 +74,31 @@ func (ok uploadAddSuccess) ToIPLD() (ipld.Node, error) {
7474 return nb .Build (), nil
7575}
7676
77+ type uploadAddFailure struct {
78+ name string
79+ message string
80+ }
81+
82+ func (x uploadAddFailure ) Name () string {
83+ return x .name
84+ }
85+
86+ func (x uploadAddFailure ) Error () string {
87+ return x .message
88+ }
89+
90+ func (x uploadAddFailure ) ToIPLD () (ipld.Node , error ) {
91+ np := basicnode .Prototype .Any
92+ nb := np .NewBuilder ()
93+ ma , _ := nb .BeginMap (2 )
94+ ma .AssembleKey ().AssignString ("name" )
95+ ma .AssembleValue ().AssignString (x .name )
96+ ma .AssembleKey ().AssignString ("message" )
97+ ma .AssembleValue ().AssignString (x .message )
98+ ma .Finish ()
99+ return nb .Build (), nil
100+ }
101+
77102var rcptsch = []byte (`
78103 type Result union {
79104 | UploadAddSuccess "ok"
@@ -129,8 +154,8 @@ func TestExecute(t *testing.T) {
129154 fixtures .Service ,
130155 WithServiceMethod (
131156 uploadadd .Can (),
132- Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (uploadAddSuccess , fx.Effects , error ) {
133- return uploadAddSuccess {Root : cap .Nb ().Root , Status : "done" }, nil , nil
157+ Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (result. Result [ uploadAddSuccess , uploadAddFailure ] , fx.Effects , error ) {
158+ return result. Ok [ uploadAddSuccess , uploadAddFailure ]( uploadAddSuccess {Root : cap .Nb ().Root , Status : "done" }) , nil , nil
134159 }),
135160 ),
136161 ))
@@ -152,13 +177,13 @@ func TestExecute(t *testing.T) {
152177 rcpt := helpers .Must (reader .Read (rcptlnk , resp .Blocks ()))
153178
154179 result .MatchResultR0 (rcpt .Out (), func (ok uploadAddSuccess ) {
155- fmt . Printf ("%+v\n " , ok )
180+ t . Logf ("%+v\n " , ok )
156181 require .Equal (t , ok .Root , rt )
157182 require .Equal (t , ok .Status , "done" )
158183 }, func (x ipld.Node ) {
159184 f := asFailure (t , x )
160- fmt . Println (f .Message )
161- fmt . Println (* f .Stack )
185+ t . Log (f .Message )
186+ t . Log (* f .Stack )
162187 require .Nil (t , f )
163188 })
164189 })
@@ -175,8 +200,8 @@ func TestExecute(t *testing.T) {
175200 fixtures .Service ,
176201 WithServiceMethod (
177202 uploadadd .Can (),
178- Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (uploadAddSuccess , fx.Effects , error ) {
179- return uploadAddSuccess {Root : cap .Nb ().Root , Status : "done" }, nil , nil
203+ Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (result. Result [ uploadAddSuccess , uploadAddFailure ] , fx.Effects , error ) {
204+ return result. Ok [ uploadAddSuccess , uploadAddFailure ]( uploadAddSuccess {Root : cap .Nb ().Root , Status : "done" }) , nil , nil
180205 }),
181206 ),
182207 ))
@@ -208,13 +233,13 @@ func TestExecute(t *testing.T) {
208233 rcpt := helpers .Must (reader .Read (rcptlnk , resp .Blocks ()))
209234
210235 result .MatchResultR0 (rcpt .Out (), func (ok uploadAddSuccess ) {
211- fmt . Printf ("%+v\n " , ok )
236+ t . Logf ("%+v\n " , ok )
212237 require .Equal (t , ok .Root , rt )
213238 require .Equal (t , ok .Status , "done" )
214239 }, func (x ipld.Node ) {
215240 f := asFailure (t , x )
216- fmt . Println (f .Message )
217- fmt . Println (* f .Stack )
241+ t . Log (f .Message )
242+ t . Log (* f .Stack )
218243 require .Nil (t , f )
219244 })
220245 })
@@ -242,7 +267,7 @@ func TestExecute(t *testing.T) {
242267 t .Fatalf ("expected error: %s" , invs [0 ].Link ())
243268 }, func (x ipld.Node ) {
244269 f := asFailure (t , x )
245- fmt . Printf ("%s %+v\n " , * f .Name , f )
270+ t . Logf ("%s %+v\n " , * f .Name , f )
246271 require .Equal (t , * f .Name , "HandlerNotFoundError" )
247272 })
248273 })
@@ -259,8 +284,8 @@ func TestExecute(t *testing.T) {
259284 fixtures .Service ,
260285 WithServiceMethod (
261286 uploadadd .Can (),
262- Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (uploadAddSuccess , fx.Effects , error ) {
263- return uploadAddSuccess {} , nil , fmt .Errorf ("test error" )
287+ Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (result. Result [ uploadAddSuccess , uploadAddFailure ] , fx.Effects , error ) {
288+ return nil , nil , fmt .Errorf ("test error" )
264289 }),
265290 ),
266291 ))
@@ -280,11 +305,49 @@ func TestExecute(t *testing.T) {
280305 t .Fatalf ("expected error: %s" , invs [0 ].Link ())
281306 }, func (x ipld.Node ) {
282307 f := asFailure (t , x )
283- fmt . Printf ("%s %+v\n " , * f .Name , f )
308+ t . Logf ("%s %+v\n " , * f .Name , f )
284309 require .Equal (t , * f .Name , "HandlerExecutionError" )
285310 })
286311 })
287312
313+ t .Run ("failure" , func (t * testing.T ) {
314+ uploadadd := validator .NewCapability (
315+ "upload/add" ,
316+ schema .DIDString (),
317+ schema .Struct [uploadAddCaveats ](uploadAddCaveatsType (), nil ),
318+ nil ,
319+ )
320+
321+ server := helpers .Must (NewServer (
322+ fixtures .Service ,
323+ WithServiceMethod (
324+ uploadadd .Can (),
325+ Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (result.Result [uploadAddSuccess , uploadAddFailure ], fx.Effects , error ) {
326+ return result.Error [uploadAddSuccess ](uploadAddFailure {name : "UploadAddError" , message : "boom" }), nil , nil
327+ }),
328+ ),
329+ ))
330+
331+ conn := helpers .Must (client .NewConnection (fixtures .Service , server ))
332+ rt := cidlink.Link {Cid : cid .MustParse ("bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui" )}
333+ cap := uploadadd .New (fixtures .Alice .DID ().String (), uploadAddCaveats {Root : rt })
334+ invs := []invocation.Invocation {helpers .Must (invocation .Invoke (fixtures .Alice , fixtures .Service , cap ))}
335+ resp := helpers .Must (client .Execute (t .Context (), invs , conn ))
336+ rcptlnk , ok := resp .Get (invs [0 ].Link ())
337+ require .True (t , ok , "missing receipt for invocation: %s" , invs [0 ].Link ())
338+
339+ reader := helpers .Must (receipt.NewReceiptReader [uploadAddSuccess , ipld.Node ](rcptsch ))
340+ rcpt := helpers .Must (reader .Read (rcptlnk , resp .Blocks ()))
341+
342+ result .MatchResultR0 (rcpt .Out (), func (uploadAddSuccess ) {
343+ t .Fatalf ("expected error: %s" , invs [0 ].Link ())
344+ }, func (x ipld.Node ) {
345+ f := asFailure (t , x )
346+ t .Logf ("%s %+v\n " , * f .Name , f )
347+ require .Equal (t , * f .Name , "UploadAddError" )
348+ })
349+ })
350+
288351 t .Run ("invalid audience" , func (t * testing.T ) {
289352 uploadadd := validator .NewCapability (
290353 "upload/add" ,
@@ -297,8 +360,8 @@ func TestExecute(t *testing.T) {
297360 fixtures .Service ,
298361 WithServiceMethod (
299362 uploadadd .Can (),
300- Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (uploadAddSuccess , fx.Effects , error ) {
301- return uploadAddSuccess {Root : cap .Nb ().Root , Status : "done" }, nil , nil
363+ Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (result. Result [ uploadAddSuccess , uploadAddFailure ] , fx.Effects , error ) {
364+ return result. Ok [ uploadAddSuccess , uploadAddFailure ]( uploadAddSuccess {Root : cap .Nb ().Root , Status : "done" }) , nil , nil
302365 }),
303366 ),
304367 ))
@@ -339,8 +402,8 @@ func TestExecute(t *testing.T) {
339402 fixtures .Service ,
340403 WithServiceMethod (
341404 uploadadd .Can (),
342- Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (uploadAddSuccess , fx.Effects , error ) {
343- return uploadAddSuccess {Root : cap .Nb ().Root , Status : "done" }, nil , nil
405+ Provide (uploadadd , func (ctx context.Context , cap ucan.Capability [uploadAddCaveats ], inv invocation.Invocation , ictx InvocationContext ) (result. Result [ uploadAddSuccess , uploadAddFailure ] , fx.Effects , error ) {
406+ return result. Ok [ uploadAddSuccess , uploadAddFailure ]( uploadAddSuccess {Root : cap .Nb ().Root , Status : "done" }) , nil , nil
344407 }),
345408 ),
346409 WithAlternativeAudiences (fixtures .Bob ),
0 commit comments