33
44package com .example .fleetwise .scenario ;
55
6+ import org .slf4j .Logger ;
7+ import org .slf4j .LoggerFactory ;
68import software .amazon .awssdk .core .client .config .ClientOverrideConfiguration ;
79import software .amazon .awssdk .core .retry .RetryMode ;
810import software .amazon .awssdk .http .async .SdkAsyncHttpClient ;
2729
2830// snippet-start:[iotfleetwise.java2.scenario.actions.main]
2931public class FleetwiseActions {
32+ private static final Logger logger = LoggerFactory .getLogger (FleetwiseActions .class );
3033 private static IoTFleetWiseAsyncClient ioTFleetWiseAsyncClient ;
3134
3235 private static IoTFleetWiseAsyncClient getAsyncClient () {
@@ -102,7 +105,11 @@ public CompletableFuture<String> createSignalCatalogAsync(String signalCatalogNa
102105 return getAsyncClient ().createSignalCatalog (request )
103106 .whenComplete ((response , exception ) -> {
104107 if (exception != null ) {
105- throw new CompletionException ("Failed to create signal catalog: " + exception .getMessage (), exception );
108+ Throwable cause = exception .getCause ();
109+ if (cause instanceof software .amazon .awssdk .services .iot .model .ValidationException ) {
110+ throw new CompletionException ("A validation error occurred: " + cause .getMessage (), cause );
111+ }
112+ throw new CompletionException ("Error performing place search" , exception );
106113 }
107114 })
108115 .thenApply (CreateSignalCatalogResponse ::arn );
@@ -243,14 +250,18 @@ public CompletableFuture<Void> deleteDecoderManifestAsync(String name) {
243250 .build ();
244251
245252 return getAsyncClient ().deleteDecoderManifest (request )
246- .whenComplete ((response , exception ) -> {
253+ .handle ((response , exception ) -> {
247254 if (exception != null ) {
248- throw new RuntimeException ("Failed to delete decoder manifest: " + name , exception );
255+ Throwable cause = exception .getCause ();
256+ if (cause instanceof ResourceNotFoundException ) {
257+ throw new CompletionException ("❌ Failed to locate the decoder manifest: " + name , cause );
258+ }
259+ throw new CompletionException ("❌ Failed to delete decoder manifest: " + name , cause );
249260 } else {
250- System .out .println ("✅ " + name + " was successfully deleted" );
261+ logger .info ("✅ {} was successfully deleted" , name );
262+ return null ;
251263 }
252- })
253- .thenApply (response -> null ); // Return CompletableFuture<Void>
264+ });
254265 }
255266 // snippet-end:[iotfleetwise.java2.delete.decoder.main]
256267
@@ -268,14 +279,18 @@ public CompletableFuture<Void> deleteVehicleAsync(String vecName) {
268279 .build ();
269280
270281 return getAsyncClient ().deleteVehicle (request )
271- .whenComplete ((response , exception ) -> {
282+ .handle ((response , exception ) -> {
272283 if (exception != null ) {
273- throw new RuntimeException ("Failed to delete vehicle: " + vecName , exception );
284+ Throwable cause = exception .getCause ();
285+ if (cause instanceof ResourceNotFoundException ) {
286+ throw new CompletionException ("❌ Failed to locate the vehicle: " + vecName , cause );
287+ }
288+ throw new CompletionException ("❌ Failed to delete vehicle: " + vecName , cause );
274289 } else {
275- System .out .println ("✅ " + vecName + " was successfully deleted" );
290+ logger .info ("✅ {} was successfully deleted" , vecName );
291+ return null ;
276292 }
277- })
278- .thenApply (response -> null ); // Return CompletableFuture<Void>
293+ });
279294 }
280295 // snippet-end:[iotfleetwise.java2.delete.vehicle.main]
281296
@@ -345,9 +360,13 @@ public CompletableFuture<Void> createVehicleAsync(String vecName, String manifes
345360 return getAsyncClient ().createVehicle (request )
346361 .whenComplete ((response , exception ) -> {
347362 if (exception != null ) {
348- throw new CompletionException ("Failed to create vehicle: " + exception .getMessage (), exception );
363+ Throwable cause = exception .getCause ();
364+ if (cause instanceof ResourceNotFoundException ) {
365+ throw new CompletionException ("The required resource was not located: " + exception .getMessage (), exception );
366+ }
367+ throw new CompletionException ("Failed to delete signal catalog: " + exception .getMessage (), exception );
349368 } else {
350- System . out . println ("✅ Vehicle '" + vecName + "' created successfully." );
369+ logger . info ("✅ Vehicle '" + vecName + "' created successfully." );
351370 }
352371 })
353372 .thenApply (response -> null ); // Void return type
@@ -508,7 +527,7 @@ public CompletableFuture<Void> getVehicleDetailsAsync(String vehicleName) {
508527 details .put ("lastModificationTime" , response .lastModificationTime ().toString ());
509528
510529 // Print details in a readable format
511- System . out . println ("🚗 Vehicle Details:" );
530+ logger . info ("🚗 Vehicle Details:" );
512531 details .forEach ((key , value ) -> {
513532 System .out .printf ("• %-20s : %s%n" , key , value );
514533 });
@@ -537,12 +556,12 @@ public CompletableFuture<Void> createThingIfNotExistsAsync(String thingName) {
537556 .whenComplete ((response , exception ) -> {
538557 if (exception != null ) {
539558 if (exception instanceof ResourceAlreadyExistsException ) {
540- System . out . println ("ℹ️ IoT Thing already exists: " + thingName );
559+ logger . info ("ℹ️ IoT Thing already exists: " + thingName );
541560 } else {
542561 throw new CompletionException ("Failed to create IoT Thing: " + thingName , exception );
543562 }
544563 } else {
545- System . out . println ("✅ IoT Thing created: " + response .thingName ());
564+ logger . info ("✅ IoT Thing created: " + response .thingName ());
546565 }
547566 })
548567 .thenApply (response -> null );
@@ -562,15 +581,20 @@ public CompletableFuture<Void> deleteModelManifestAsync(String name) {
562581 .build ();
563582
564583 return getAsyncClient ().deleteModelManifest (request )
565- .whenComplete ((response , exception ) -> {
584+ .handle ((response , exception ) -> {
566585 if (exception != null ) {
567- throw new CompletionException ("Failed to delete model manifest: " + exception .getMessage (), exception );
586+ Throwable cause = exception .getCause ();
587+ if (cause instanceof ResourceNotFoundException ) {
588+ throw new CompletionException ("❌ Failed to locate the model manifest: " + name , cause );
589+ }
590+ throw new CompletionException ("❌ Failed to delete model manifest: " + name , cause );
568591 } else {
569- System .out .println ("✅ " + name + " was successfully deleted" );
592+ logger .info ("✅ {} was successfully deleted" , name );
593+ return null ;
570594 }
571- })
572- .thenApply (response -> null ); // return type is CompletableFuture<Void>
595+ });
573596 }
597+
574598 // snippet-end:[iotfleetwise.java2.delete.model.main]
575599
576600 // snippet-start:[iotfleetwise.java2.delete.catalog.main]
@@ -587,15 +611,20 @@ public CompletableFuture<Void> deleteSignalCatalogAsync(String name) {
587611 .build ();
588612
589613 return getAsyncClient ().deleteSignalCatalog (request )
590- .whenComplete ((response , exception ) -> {
614+ .handle ((response , exception ) -> {
591615 if (exception != null ) {
592- throw new CompletionException ("Failed to delete signal catalog: " + exception .getMessage (), exception );
616+ Throwable cause = exception .getCause ();
617+ if (cause instanceof ResourceNotFoundException ) {
618+ throw new CompletionException ("❌ Failed to locate the signal catalog: " + name , cause );
619+ }
620+ throw new CompletionException ("❌ Failed to delete signal catalog: " + name , cause );
593621 } else {
594- System .out .println ("✅ " +name + " was successfully deleted" );
622+ logger .info ("✅ {} was successfully deleted" , name );
623+ return null ;
595624 }
596- })
597- .thenApply (response -> null ); // return type is CompletableFuture<Void>
625+ });
598626 }
627+
599628 // snippet-end:[iotfleetwise.java2.delete.catalog.main]
600629
601630 // snippet-start:[iotfleetwise.java2.list.catalogs.main]
@@ -680,14 +709,18 @@ public CompletableFuture<Void> deleteFleetAsync(String fleetId) {
680709 .build ();
681710
682711 return getAsyncClient ().deleteFleet (request )
683- .whenComplete ((response , exception ) -> {
712+ .handle ((response , exception ) -> {
684713 if (exception != null ) {
685- throw new CompletionException ("Failed to delete fleet: " + exception .getMessage (), exception );
714+ Throwable cause = exception .getCause ();
715+ if (cause instanceof ResourceNotFoundException ) {
716+ throw new CompletionException ("❌ Failed to locate the fleet: " + fleetId , cause );
717+ }
718+ throw new CompletionException ("❌ Failed to delete fleet: " + fleetId , cause );
686719 } else {
687- System .out .println ("✅ " + fleetId + " was successfully deleted" );
720+ logger .info ("✅ {} was successfully deleted" , fleetId );
721+ return null ;
688722 }
689- })
690- .thenApply (response -> null ); // Returning Void
723+ });
691724 }
692725 // snippet-end:[iotfleetwise.java2.delete.fleet.main]
693726
@@ -711,7 +744,11 @@ public CompletableFuture<String> createFleetAsync(String catARN, String fleetId)
711744 return getAsyncClient ().createFleet (fleetRequest )
712745 .whenComplete ((response , exception ) -> {
713746 if (exception != null ) {
714- throw new RuntimeException ("Failed to create fleet: " + fleetId , exception );
747+ Throwable cause = exception .getCause ();
748+ if (cause instanceof ResourceNotFoundException ) {
749+ throw new CompletionException ("The required resource was not found: " + cause .getMessage (), cause );
750+ }
751+ throw new CompletionException ("An unexpected error occurred" , exception );
715752 }
716753 })
717754 .thenApply (CreateFleetResponse ::id ); // Extract fleet ID on success
0 commit comments