@@ -54,46 +54,38 @@ private async Task<CommandState> PrintItemAsync(Container container, Cancellatio
5454 try
5555 {
5656 using var response = await container . ReadItemStreamAsync ( this . Id , new PartitionKey ( this . PartitionKey ) , cancellationToken : token ) ;
57+ RequestChargeContext . Record ( response . Headers . RequestCharge ) ;
5758
5859 if ( response . IsSuccessStatusCode )
5960 {
60- commandState . RequestCharge = response . Headers . RequestCharge ;
6161 using var reader = new StreamReader ( response . Content ) ;
6262 var content = await reader . ReadToEndAsync ( ) ;
6363
6464 // Parse the content as JSON for structured output
65- var jsonDocument = System . Text . Json . JsonDocument . Parse ( content ) ;
66- commandState . Result = new ShellJson ( jsonDocument . RootElement ) ;
65+ using var jsonDocument = System . Text . Json . JsonDocument . Parse ( content ) ;
66+ commandState . Result = new ShellJson ( jsonDocument . RootElement . Clone ( ) ) ;
6767 }
6868 else if ( response . StatusCode == System . Net . HttpStatusCode . NotFound )
6969 {
70- return new ErrorCommandState ( new CommandException ( "print" , MessageService . GetString ( "command-print-error-item_not_found" , new Dictionary < string , object >
70+ throw new CommandException ( "print" , MessageService . GetString ( "command-print-error-item_not_found" , new Dictionary < string , object >
7171 {
7272 { "id" , this . Id ?? "(null)" } ,
7373 { "partitionKey" , this . PartitionKey ?? "(null)" } ,
74- } ) ) )
75- {
76- RequestCharge = response . Headers . RequestCharge ,
77- } ;
74+ } ) ) ;
7875 }
7976 else
8077 {
81- return new ErrorCommandState ( new CommandException ( "print" , MessageService . GetString ( "command-print-error-request_failed" , new Dictionary < string , object >
78+ throw new CommandException ( "print" , MessageService . GetString ( "command-print-error-request_failed" , new Dictionary < string , object >
8279 {
8380 { "id" , this . Id ?? "(null)" } ,
8481 { "status" , ( int ) response . StatusCode } ,
85- } ) ) )
86- {
87- RequestCharge = response . Headers . RequestCharge ,
88- } ;
82+ } ) ) ;
8983 }
9084 }
9185 catch ( CosmosException ex )
9286 {
93- return new ErrorCommandState ( new CommandException ( "print" , MessageService . GetArgsString ( "command-print-error-reading_item" , "message" , CommandException . GetDisplayMessage ( ex ) ) , ex ) )
94- {
95- RequestCharge = ex . RequestCharge > 0 ? ex . RequestCharge : null ,
96- } ;
87+ RequestChargeContext . Record ( ex . RequestCharge ) ;
88+ throw new CommandException ( "print" , MessageService . GetArgsString ( "command-print-error-reading_item" , "message" , CommandException . GetDisplayMessage ( ex ) ) , ex ) ;
9789 }
9890
9991 return commandState ;
0 commit comments