51
51
import org .eclipse .ditto .policies .model .SubjectId ;
52
52
import org .eclipse .ditto .policies .model .signals .commands .PolicyErrorResponse ;
53
53
import org .eclipse .ditto .policies .model .signals .commands .exceptions .PolicyConflictException ;
54
- import org .eclipse .ditto .policies .model .signals .commands .exceptions .PolicyNotAccessibleException ;
55
54
import org .eclipse .ditto .policies .model .signals .commands .exceptions .PolicyUnavailableException ;
56
55
import org .eclipse .ditto .policies .model .signals .commands .modify .CreatePolicy ;
57
56
import org .eclipse .ditto .policies .model .signals .commands .modify .CreatePolicyResponse ;
@@ -383,19 +382,17 @@ private Policy handleCreatePolicyResponse(final CreatePolicy createPolicy, final
383
382
.ifPresent (policy -> getContext ().getParent ().tell (new ThingPolicyCreated (createThing .getEntityId (),
384
383
createPolicyResponse .getEntityId (), createPolicy .getDittoHeaders ()), getSelf ()));
385
384
return createPolicyResponse .getPolicyCreated ().orElseThrow ();
385
+ } else if (isAskTimeoutException (policyResponse , null )) {
386
+ throw PolicyUnavailableException .newBuilder (createPolicy .getEntityId ())
387
+ .dittoHeaders (createThing .getDittoHeaders ())
388
+ .build ();
389
+ } else if (policyResponse instanceof DittoRuntimeException policyException ) {
390
+ throw reportInitialPolicyCreationFailure (createPolicy .getEntityId (), createThing , policyException );
386
391
} else {
387
- if (shouldReportInitialPolicyCreationFailure (policyResponse )) {
388
- throw reportInitialPolicyCreationFailure (createPolicy .getEntityId (), createThing );
389
- } else if (isAskTimeoutException (policyResponse , null )) {
390
- throw PolicyUnavailableException .newBuilder (createPolicy .getEntityId ())
391
- .dittoHeaders (createThing .getDittoHeaders ())
392
- .build ();
393
- } else {
394
- final var hint = String .format ("creating initial policy during creation of Thing <%s>" ,
395
- createThing .getEntityId ());
396
- throw AbstractEnforcementReloaded .reportErrorOrResponse (hint , policyResponse , null ,
397
- createThing .getDittoHeaders ());
398
- }
392
+ final var hint = String .format ("creating initial policy during creation of Thing <%s>" ,
393
+ createThing .getEntityId ());
394
+ throw AbstractEnforcementReloaded .reportErrorOrResponse (hint , policyResponse , null ,
395
+ createThing .getDittoHeaders ());
399
396
}
400
397
}
401
398
@@ -410,22 +407,30 @@ private static boolean isAskTimeoutException(final Object response, @Nullable fi
410
407
return error instanceof AskTimeoutException || response instanceof AskTimeoutException ;
411
408
}
412
409
413
- private static boolean shouldReportInitialPolicyCreationFailure (final Object policyResponse ) {
414
- return policyResponse instanceof PolicyConflictException ||
415
- policyResponse instanceof PolicyNotAccessibleException ||
416
- policyResponse instanceof NamespaceBlockedException ;
417
- }
418
-
419
410
private ThingNotCreatableException reportInitialPolicyCreationFailure (final PolicyId policyId ,
420
- final CreateThing command ) {
411
+ final CreateThing command , final DittoRuntimeException policyException ) {
421
412
422
413
log .withCorrelationId (command )
423
- .info ("Failed to create Policy with ID <{}> because it already exists ." +
414
+ .info ("Failed to create Policy with ID <{}> due to: <{}: {}> ." +
424
415
" The CreateThing command which would have created a Policy for the Thing with ID <{}>" +
425
- " is therefore not handled." , policyId , command .getEntityId ());
426
- return ThingNotCreatableException .newBuilderForPolicyExisting (command .getEntityId (), policyId )
427
- .dittoHeaders (command .getDittoHeaders ())
428
- .build ();
416
+ " is therefore not handled." , policyId ,
417
+ policyException .getClass ().getSimpleName (), policyException .getMessage (),
418
+ command .getEntityId ()
419
+ );
420
+ if (policyException instanceof PolicyConflictException ) {
421
+ return ThingNotCreatableException .newBuilderForPolicyExisting (command .getEntityId (), policyId )
422
+ .dittoHeaders (command .getDittoHeaders ())
423
+ .build ();
424
+ } else if (policyException instanceof NamespaceBlockedException ) {
425
+ return ThingNotCreatableException .newBuilderForPolicyMissing (command .getEntityId (), policyId )
426
+ .dittoHeaders (command .getDittoHeaders ())
427
+ .build ();
428
+ } else {
429
+ return ThingNotCreatableException .newBuilderForOtherReason (command .getEntityId (), policyId ,
430
+ policyException .getMessage ())
431
+ .dittoHeaders (command .getDittoHeaders ())
432
+ .build ();
433
+ }
429
434
}
430
435
431
436
@ Override
0 commit comments