Skip to content

Commit 3de5da2

Browse files
authored
Merge pull request eclipse-ditto#1738 from eclipse-ditto/bugfix/implicit-policy-creation-error-message
fix wrong exception message when policy could not be implicitly created when creating thing
2 parents b7315c6 + 8b0453e commit 3de5da2

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

things/model/src/main/java/org/eclipse/ditto/things/model/signals/commands/exceptions/ThingNotCreatableException.java

+21
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public final class ThingNotCreatableException extends DittoRuntimeException impl
4949
"The Thing with ID ''{0}'' could not be created because creation of its " +
5050
"implicit Policy ID ''{1}'' failed.";
5151

52+
static final String MESSAGE_TEMPLATE_CUSTOM_REASON = "The Thing with ID ''{0}'' could not be created as the " +
53+
"Policy with ID ''{1}'' could not be created due to: ''{2}''";
54+
5255
static final String DEFAULT_DESCRIPTION_NOT_EXISTING =
5356
"Check if the ID of the Policy you created the Thing with is correct and that the Policy is existing.";
5457

@@ -101,6 +104,19 @@ public static Builder newBuilderForPolicyExisting(final ThingId thingId, final P
101104
return new Builder(thingId, policyId, false);
102105
}
103106

107+
/**
108+
* A mutable builder for a {@code ThingNotCreatableException} thrown if a Thing could not be created because
109+
* the creation of its implicit Policy failed due to a reason passed as {@code reason}.
110+
*
111+
* @param thingId the ID of the Thing.
112+
* @param policyId the ID of the Policy which was used when creating the Thing.
113+
* @param reason the reason why the implicit policy creation failed.
114+
* @return the builder.
115+
*/
116+
public static Builder newBuilderForOtherReason(final ThingId thingId, final PolicyId policyId, final String reason) {
117+
return new Builder(thingId, policyId, reason);
118+
}
119+
104120
/**
105121
* Returns a new instance of {@code ThingNotCreatableException} that is caused by using the unsupported "live"
106122
* channel for sending a {@code CreateThing} command.
@@ -221,6 +237,11 @@ private Builder(final ThingId thingId, final PolicyId policyId, final boolean po
221237
}
222238
}
223239

240+
private Builder(final ThingId thingId, final PolicyId policyId, final String reason) {
241+
this();
242+
message(MessageFormat.format(MESSAGE_TEMPLATE_CUSTOM_REASON, String.valueOf(thingId), policyId, reason));
243+
}
244+
224245
private Builder httpStatus(final HttpStatus httpStatus) {
225246
this.httpStatus = httpStatus;
226247
return this;

things/service/src/main/java/org/eclipse/ditto/things/service/enforcement/ThingEnforcerActor.java

+30-25
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.eclipse.ditto.policies.model.SubjectId;
5252
import org.eclipse.ditto.policies.model.signals.commands.PolicyErrorResponse;
5353
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyConflictException;
54-
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyNotAccessibleException;
5554
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyUnavailableException;
5655
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicy;
5756
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicyResponse;
@@ -383,19 +382,17 @@ private Policy handleCreatePolicyResponse(final CreatePolicy createPolicy, final
383382
.ifPresent(policy -> getContext().getParent().tell(new ThingPolicyCreated(createThing.getEntityId(),
384383
createPolicyResponse.getEntityId(), createPolicy.getDittoHeaders()), getSelf()));
385384
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);
386391
} 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());
399396
}
400397
}
401398

@@ -410,22 +407,30 @@ private static boolean isAskTimeoutException(final Object response, @Nullable fi
410407
return error instanceof AskTimeoutException || response instanceof AskTimeoutException;
411408
}
412409

413-
private static boolean shouldReportInitialPolicyCreationFailure(final Object policyResponse) {
414-
return policyResponse instanceof PolicyConflictException ||
415-
policyResponse instanceof PolicyNotAccessibleException ||
416-
policyResponse instanceof NamespaceBlockedException;
417-
}
418-
419410
private ThingNotCreatableException reportInitialPolicyCreationFailure(final PolicyId policyId,
420-
final CreateThing command) {
411+
final CreateThing command, final DittoRuntimeException policyException) {
421412

422413
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: <{}: {}>." +
424415
" 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+
}
429434
}
430435

431436
@Override

0 commit comments

Comments
 (0)