Skip to content

Commit

Permalink
[#926] fix deserialization of PolicyActionFailedException; fix status…
Browse files Browse the repository at this point in the history
… code when executing an action on a nonexistent policy entry.

Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Jan 2, 2021
1 parent e517083 commit edc5200
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.concurrent.NotThreadSafe;

import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.model.base.common.HttpStatusCode;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeExceptionBuilder;
Expand Down Expand Up @@ -121,7 +122,9 @@ public static Builder newBuilderForDeactivateTokenIntegration() {
* format.
*/
public static PolicyActionFailedException fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return DittoRuntimeException.fromJson(jsonObject, dittoHeaders, new Builder());
final HttpStatusCode status = HttpStatusCode.forInt(jsonObject.getValueOrThrow(JsonFields.STATUS))
.orElseThrow(() -> new JsonParseException("Unsupported status"));
return DittoRuntimeException.fromJson(jsonObject, dittoHeaders, new Builder().status(status));
}

@Override
Expand Down
Expand Up @@ -24,7 +24,6 @@
import org.eclipse.ditto.model.base.headers.entitytag.EntityTag;
import org.eclipse.ditto.model.policies.Label;
import org.eclipse.ditto.model.policies.Policy;
import org.eclipse.ditto.model.policies.PolicyActionFailedException;
import org.eclipse.ditto.model.policies.PolicyEntry;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.model.policies.Subject;
Expand All @@ -34,6 +33,7 @@
import org.eclipse.ditto.services.policies.common.config.PolicyConfig;
import org.eclipse.ditto.services.utils.persistentactors.results.Result;
import org.eclipse.ditto.services.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyEntryNotAccessibleException;
import org.eclipse.ditto.signals.commands.policies.modify.ActivateSubject;
import org.eclipse.ditto.signals.commands.policies.modify.ActivateSubjectResponse;
import org.eclipse.ditto.signals.events.policies.PolicyEvent;
Expand Down Expand Up @@ -92,9 +92,10 @@ protected Result<PolicyEvent<?>> doApply(final Context<PolicyId> context,
command);
}
} else {
// Command is constructed incorrectly. This is a bug.
// Policy is configured incorrectly
return ResultFactory.newErrorResult(
PolicyActionFailedException.newBuilderForActivateTokenIntegration().build(), command);
PolicyEntryNotAccessibleException.newBuilder(policyId, label).dittoHeaders(dittoHeaders).build(),
command);
}
}

Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.ditto.services.policies.common.config.PolicyConfig;
import org.eclipse.ditto.services.utils.persistentactors.results.Result;
import org.eclipse.ditto.services.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyEntryNotAccessibleException;
import org.eclipse.ditto.signals.commands.policies.modify.DeactivateSubject;
import org.eclipse.ditto.signals.commands.policies.modify.DeactivateSubjectResponse;
import org.eclipse.ditto.signals.events.policies.PolicyEvent;
Expand Down Expand Up @@ -88,9 +89,10 @@ protected Result<PolicyEvent<?>> doApply(final Context<PolicyId> context,
return ResultFactory.newMutationResult(adjustedCommand, event, rawResponse);
}
} else {
// Command is constructed incorrectly. This is a bug.
// Policy is configured incorrectly
return ResultFactory.newErrorResult(
PolicyActionFailedException.newBuilderForDeactivateTokenIntegration().build(), command);
PolicyEntryNotAccessibleException.newBuilder(policyId, label).dittoHeaders(dittoHeaders).build(),
command);
}
}

Expand Down

0 comments on commit edc5200

Please sign in to comment.