@@ -11,6 +11,7 @@ use concordium_rust_sdk::base::{
11
11
} ,
12
12
smart_contracts:: ReceiveName ,
13
13
} ;
14
+ use serde:: Serialize ;
14
15
15
16
#[ derive( Enum , Copy , Clone , PartialEq , Eq , serde:: Serialize , serde:: Deserialize ) ]
16
17
pub enum ContractVersion {
@@ -118,7 +119,7 @@ impl ContractInitialized {
118
119
opt_event_schema. as_ref ( ) ,
119
120
log,
120
121
SmartContractSchemaNames :: Event ,
121
- ) ;
122
+ ) ? ;
122
123
123
124
connection. edges . push ( connection:: Edge :: new ( index. to_string ( ) , decoded_log) ) ;
124
125
}
@@ -190,7 +191,7 @@ impl ContractUpdated {
190
191
opt_receive_param_schema. as_ref ( ) ,
191
192
& self . input_parameter ,
192
193
SmartContractSchemaNames :: InputParameterReceiveFunction ,
193
- ) ;
194
+ ) ? ;
194
195
195
196
Ok ( Some ( decoded_input_parameter) )
196
197
}
@@ -250,7 +251,7 @@ impl ContractUpdated {
250
251
opt_event_schema. as_ref ( ) ,
251
252
log,
252
253
SmartContractSchemaNames :: Event ,
253
- ) ;
254
+ ) ? ;
254
255
255
256
connection. edges . push ( connection:: Edge :: new ( index. to_string ( ) , decoded_log) ) ;
256
257
}
@@ -331,7 +332,7 @@ impl ContractInterrupted {
331
332
opt_event_schema. as_ref ( ) ,
332
333
log,
333
334
SmartContractSchemaNames :: Event ,
334
- ) ;
335
+ ) ? ;
335
336
336
337
connection. edges . push ( connection:: Edge :: new ( index. to_string ( ) , decoded_log) ) ;
337
338
}
@@ -394,38 +395,55 @@ impl SmartContractSchemaNames {
394
395
}
395
396
}
396
397
398
+ /// Schema decoding error reported to the front-end.
399
+ #[ derive( Serialize ) ]
400
+ struct SchemaDecodingError {
401
+ error : String ,
402
+ }
403
+
397
404
fn decode_value_with_schema (
398
405
opt_schema : Option < & Type > ,
399
406
value : & [ u8 ] ,
400
407
schema_name : SmartContractSchemaNames ,
401
- ) -> String {
408
+ ) -> Result < String , ApiError > {
402
409
let Some ( schema) = opt_schema else {
403
410
// Note: There could be something better displayed than this string if no schema
404
411
// is available for decoding at the frontend long-term.
405
- return format ! (
406
- "No embedded {} schema in smart contract available for decoding" ,
407
- schema_name. kind( )
408
- ) ;
412
+ return serde_json:: to_string ( & SchemaDecodingError {
413
+ error : format ! (
414
+ "No embedded {} schema in smart contract available for decoding" ,
415
+ schema_name. kind( )
416
+ ) ,
417
+ } ) . map_err ( |_| ApiError :: InternalError ( "Should be valid error string" . to_string ( ) ) ) ;
409
418
} ;
410
419
411
420
let mut cursor = Cursor :: new ( & value) ;
421
+
412
422
match schema. to_json ( & mut cursor) {
413
423
Ok ( v) => {
414
- serde_json:: to_string ( & v) . unwrap_or_else ( |e| {
415
- // We don't return an error here since the query is correctly formed and
416
- // the CCDScan backend is working as expected.
417
- // A wrong/missing schema is a mistake by the smart contract
418
- // developer which in general cannot be fixed after the deployment of
419
- // the contract. We display the error message (instead of the decoded
420
- // value) in the block explorer to make the info visible to the smart
421
- // contract developer for debugging purposes here.
422
- format ! (
423
- "Failed to deserialize {} with {} schema into string: {:?}" ,
424
- schema_name. value( ) ,
425
- schema_name. kind( ) ,
426
- e
427
- )
428
- } )
424
+ match serde_json:: to_string ( & v) {
425
+ Ok ( v) => Ok ( v) ,
426
+ Err ( error) => {
427
+ // We don't return an error here since the query is correctly formed and
428
+ // the CCDScan backend is working as expected.
429
+ // A wrong/missing schema is a mistake by the smart contract
430
+ // developer which in general cannot be fixed after the deployment of
431
+ // the contract. We display the error message (instead of the decoded
432
+ // value) in the block explorer to make the info visible to the smart
433
+ // contract developer for debugging purposes here.
434
+ Ok ( serde_json:: to_string ( & SchemaDecodingError {
435
+ error : format ! (
436
+ "Failed to deserialize {} with {} schema into string: {:?}" ,
437
+ schema_name. value( ) ,
438
+ schema_name. kind( ) ,
439
+ error
440
+ ) ,
441
+ } )
442
+ . map_err ( |_| {
443
+ ApiError :: InternalError ( "Should be valid error string" . to_string ( ) )
444
+ } ) ?)
445
+ }
446
+ }
429
447
}
430
448
Err ( e) => {
431
449
// We don't return an error here since the query is correctly formed and
@@ -435,12 +453,15 @@ fn decode_value_with_schema(
435
453
// the contract. We display the error message (instead of the decoded
436
454
// value) in the block explorer to make the info visible to the smart
437
455
// contract developer for debugging purposes here.
438
- format ! (
439
- "Failed to deserialize {} with {} schema: {:?}" ,
440
- schema_name. value( ) ,
441
- schema_name. kind( ) ,
442
- e. display( true )
443
- )
456
+ Ok ( serde_json:: to_string ( & SchemaDecodingError {
457
+ error : format ! (
458
+ "Failed to deserialize {} with {} schema: {:?}" ,
459
+ schema_name. value( ) ,
460
+ schema_name. kind( ) ,
461
+ e. display( true )
462
+ ) ,
463
+ } )
464
+ . map_err ( |_| ApiError :: InternalError ( "Should be valid error string" . to_string ( ) ) ) ?)
444
465
}
445
466
}
446
467
}
0 commit comments