Skip to content

Commit

Permalink
[eclipse-ditto#964] adjusted that an "internal server error" 500 was …
Browse files Browse the repository at this point in the history
…returned when top-level policy actions could not be applied

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Mar 2, 2021
1 parent b977e02 commit d6e4db5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected Result<PolicyEvent<?>> doApply(final Context<PolicyId> context,
policyActionCommandStrategyMap.get(actionCommand.getName());

if (strategy == null) {
// builds an internal server error, 500
final PolicyActionFailedException exception = PolicyActionFailedException.newBuilder()
.action(actionCommand.getName())
.dittoHeaders(dittoHeaders)
Expand All @@ -88,7 +89,9 @@ protected Result<PolicyEvent<?>> doApply(final Context<PolicyId> context,
.withCorrelationId(command)
.error(exception, "Strategy not found for top-level action <{}>", actionCommand.getName());
return ResultFactory.newErrorResult(exception, command);
} else if (!entries.isEmpty()) {
} else if (entries.isEmpty()) {
return ResultFactory.newErrorResult(command.getNotApplicableException(dittoHeaders), command);
} else {
final List<PolicyActionCommand<?>> commands = entries.stream()
.map(PolicyEntry::getLabel)
.map(actionCommand::setLabel)
Expand All @@ -103,10 +106,19 @@ protected Result<PolicyEvent<?>> doApply(final Context<PolicyId> context,
final TopLevelPolicyActionCommandResponse response =
TopLevelPolicyActionCommandResponse.of(context.getState(), dittoHeaders);
return ResultFactory.newMutationResult(command, event.get(), response);
} else {
// builds an internal server error, 500
final PolicyActionFailedException exception = PolicyActionFailedException.newBuilder()
.action(actionCommand.getName())
.dittoHeaders(dittoHeaders)
.build();
context.getLog()
.withCorrelationId(command)
.error(exception, "Visitor could not aggregate events for action <{}>", actionCommand.getName());
return ResultFactory.newErrorResult(exception, command);
}
}
}
return ResultFactory.newErrorResult(command.getNotApplicableException(dittoHeaders), command);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ public boolean isApplicable(final PolicyEntry policyEntry, final AuthorizationCo

@Override
public PolicyActionFailedException getNotApplicableException(final DittoHeaders dittoHeaders) {
return PolicyActionFailedException.newBuilder()
.action(getPolicyActionCommand().getName())
return PolicyActionFailedException.newBuilderForTopLevelAction(getPolicyActionCommand().getName())
.dittoHeaders(dittoHeaders)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static Builder newBuilderForDeactivateTokenIntegration() {
* A mutable builder for a {@code PolicyActionFailedException} due to inappropriate authentication method.
*
* @param action the failed action.
* @return the exception builder.
* @return the builder.
*/
public static DittoRuntimeExceptionBuilder<PolicyActionFailedException>
newBuilderForInappropriateAuthenticationMethod(final String action) {
Expand All @@ -103,6 +103,19 @@ public static Builder newBuilderForDeactivateTokenIntegration() {
.description("Policy action is only possible with JWT authentication.");
}

/**
* A mutable builder for a {@code PolicyActionFailedException} for a {@code TopLevelPolicyActionCommand}.
*
* @param action the failed action.
* @return the builder.
*/
public static DittoRuntimeExceptionBuilder<PolicyActionFailedException> newBuilderForTopLevelAction(
final String action) {
return new Builder().action(action)
.status(HttpStatus.NOT_FOUND)
.description("No policy entry found for which the top level action could be applied.");
}

/**
* Constructs a new {@code PolicyActionFailedException} object with the exception content extracted from the
* given JSON object.
Expand Down

0 comments on commit d6e4db5

Please sign in to comment.