Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom mapper should not always return a 503 when the mapping fails #3610

Merged
merged 6 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.hono.adapter.mqtt.MessageMapping;
import org.eclipse.hono.adapter.mqtt.MqttContext;
import org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties;
import org.eclipse.hono.client.ClientErrorException;
import org.eclipse.hono.client.ServerErrorException;
import org.eclipse.hono.client.command.Command;
import org.eclipse.hono.util.MessageHelper;
Expand Down Expand Up @@ -199,7 +200,8 @@ private void mapUpstreamMessageRequest(
command.getDeviceId(),
mapperEndpoint.getHost(), mapperEndpoint.getPort(), mapperEndpoint.getUri(),
httpResponseAsyncResult.cause());
result.fail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, httpResponseAsyncResult.cause()));
final Throwable exception = mapException(httpResponseAsyncResult);
result.fail(exception);
} else {
final HttpResponse<Buffer> httpResponse = httpResponseAsyncResult.result();
if (httpResponse.statusCode() == HttpURLConnection.HTTP_OK) {
Expand All @@ -208,8 +210,9 @@ private void mapUpstreamMessageRequest(
LOG.debug("mapping service [host: {}, port: {}, URI: {}] returned unexpected status code: {}",
mapperEndpoint.getHost(), mapperEndpoint.getPort(), mapperEndpoint.getUri(),
httpResponse.statusCode());
result.fail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE,
"could not invoke configured mapping service"));
final Throwable exception = mapException(httpResponseAsyncResult,
"could not invoke configured mapping service");
result.fail(exception);
}
}
resultHandler.handle(result.future());
Expand Down Expand Up @@ -279,4 +282,22 @@ private void mapDownstreamMessageRequest(
resultHandler.handle(result.future());
});
}

private Throwable mapException(final AsyncResult<HttpResponse<Buffer>> httpResponseAsyncResult) {
final Optional<HttpResponse<Buffer>> httpResponse = Optional.ofNullable(httpResponseAsyncResult.result());
final int statusCode = httpResponse.map(HttpResponse::statusCode).orElse(HttpURLConnection.HTTP_INTERNAL_ERROR);
if (statusCode >= 400 && statusCode < 500) {
return new ClientErrorException(statusCode, httpResponseAsyncResult.cause());
}
return new ServerErrorException(statusCode, httpResponseAsyncResult.cause());
}

private Throwable mapException(final AsyncResult<HttpResponse<Buffer>> httpResponseAsyncResult, final String message) {
final Optional<HttpResponse<Buffer>> httpResponse = Optional.ofNullable(httpResponseAsyncResult.result());
final int statusCode = httpResponse.map(HttpResponse::statusCode).orElse(HttpURLConnection.HTTP_INTERNAL_ERROR);
if (statusCode >= 400 && statusCode < 500) {
return new ClientErrorException(statusCode, message);
}
return new ServerErrorException(statusCode, message);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we reduce the code duplication by e.g. having just one method which accepts an Optional<String> message?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might also want to consider using org.eclipse.hono.client.util.StatusCodeMapper.from() to create a context specific ServiceInvocationException ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, now it uses the StatusCodeMapper.

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.hono.adapter.MapperEndpoint;
import org.eclipse.hono.adapter.mqtt.MqttContext;
import org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties;
import org.eclipse.hono.client.ClientErrorException;
import org.eclipse.hono.client.ServerErrorException;
import org.eclipse.hono.client.command.Command;
import org.eclipse.hono.service.auth.DeviceUser;
Expand Down Expand Up @@ -379,8 +380,8 @@ public void testMappingCommandFailsForWhenPayloadCannotMapped(final VertxTestCon
messageMapping.mapUpstreamMessage(assertion, command)
.onComplete(ctx.failing(t -> {
ctx.verify(() -> {
assertThat(t).isInstanceOf(ServerErrorException.class);
assertThat((((ServerErrorException) t).getErrorCode())).isEqualTo(HttpURLConnection.HTTP_UNAVAILABLE);
assertThat(t).isInstanceOf(ClientErrorException.class);
assertThat((((ClientErrorException) t).getErrorCode())).isEqualTo(HttpURLConnection.HTTP_FORBIDDEN);
sophokles73 marked this conversation as resolved.
Show resolved Hide resolved
});
ctx.completeNow();
}));
Expand Down