Skip to content

Commit

Permalink
Handle whole request rejection scenarios (#1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
schernysh authored May 13, 2021
1 parent 24c9176 commit b88b688
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/main/java/org/prebid/server/handler/openrtb2/AmpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@
import org.prebid.server.analytics.AnalyticsReporterDelegator;
import org.prebid.server.analytics.model.AmpEvent;
import org.prebid.server.analytics.model.HttpContext;
import org.prebid.server.auction.requestfactory.AmpRequestFactory;
import org.prebid.server.auction.AmpResponsePostProcessor;
import org.prebid.server.auction.ExchangeService;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.model.Tuple2;
import org.prebid.server.auction.model.Tuple3;
import org.prebid.server.auction.requestfactory.AmpRequestFactory;
import org.prebid.server.bidder.BidderCatalog;
import org.prebid.server.cookie.UidsCookie;
import org.prebid.server.exception.BlacklistedAccountException;
import org.prebid.server.exception.BlacklistedAppException;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.exception.RejectedRequestException;
import org.prebid.server.exception.UnauthorizedAccountException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.log.ConditionalLogger;
Expand Down Expand Up @@ -337,6 +338,15 @@ private void handleResult(AsyncResult<Tuple3<BidResponse, AuctionContext, AmpRes
errorMessages = Collections.singletonList(message);
status = HttpResponseStatus.FORBIDDEN.code();
body = message;
} else if (exception instanceof RejectedRequestException) {
metricRequestStatus = MetricName.ok;
errorMessages = Collections.emptyList();

status = HttpResponseStatus.OK.code();
routingContext.response()
.headers()
.add(HttpUtil.CONTENT_TYPE_HEADER, HttpHeaderValues.APPLICATION_JSON);
body = mapper.encode(rejectedResponse());
} else {
final String message = exception.getMessage();

Expand Down Expand Up @@ -393,4 +403,8 @@ private void handleResponseException(Throwable exception) {
logger.warn("Failed to send amp response: {0}", exception.getMessage());
metrics.updateRequestTypeMetric(REQUEST_TYPE_METRIC, MetricName.networkerr);
}

private static AmpResponse rejectedResponse() {
return AmpResponse.of(Collections.emptyMap(), null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
import org.prebid.server.analytics.AnalyticsReporterDelegator;
import org.prebid.server.analytics.model.AuctionEvent;
import org.prebid.server.analytics.model.HttpContext;
import org.prebid.server.auction.requestfactory.AuctionRequestFactory;
import org.prebid.server.auction.ExchangeService;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.model.Tuple2;
import org.prebid.server.auction.requestfactory.AuctionRequestFactory;
import org.prebid.server.cookie.UidsCookie;
import org.prebid.server.exception.BlacklistedAccountException;
import org.prebid.server.exception.BlacklistedAppException;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.RejectedRequestException;
import org.prebid.server.exception.UnauthorizedAccountException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.log.ConditionalLogger;
Expand Down Expand Up @@ -167,6 +168,15 @@ private void handleResult(AsyncResult<Tuple2<BidResponse, AuctionContext>> respo
errorMessages = Collections.singletonList(message);
status = HttpResponseStatus.FORBIDDEN.code();
body = message;
} else if (exception instanceof RejectedRequestException) {
metricRequestStatus = MetricName.ok;
errorMessages = Collections.emptyList();

status = HttpResponseStatus.OK.code();
routingContext.response()
.headers()
.add(HttpUtil.CONTENT_TYPE_HEADER, HttpHeaderValues.APPLICATION_JSON);
body = mapper.encode(rejectedResponse());
} else {
metricRequestStatus = MetricName.err;
logger.error("Critical error while running the auction", exception);
Expand Down Expand Up @@ -210,4 +220,10 @@ private void handleResponseException(Throwable throwable, MetricName requestType
logger.warn("Failed to send auction response: {0}", throwable.getMessage());
metrics.updateRequestTypeMetric(requestType, MetricName.networkerr);
}

private static BidResponse rejectedResponse() {
return BidResponse.builder()
.seatbid(Collections.emptyList())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import org.prebid.server.analytics.model.HttpContext;
import org.prebid.server.analytics.model.VideoEvent;
import org.prebid.server.auction.ExchangeService;
import org.prebid.server.auction.requestfactory.VideoRequestFactory;
import org.prebid.server.auction.VideoResponseFactory;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.model.Tuple2;
import org.prebid.server.auction.requestfactory.VideoRequestFactory;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.RejectedRequestException;
import org.prebid.server.exception.UnauthorizedAccountException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.metric.MetricName;
Expand Down Expand Up @@ -124,6 +125,15 @@ private void handleResult(AsyncResult<VideoResponse> responseResult, VideoEvent.

status = HttpResponseStatus.UNAUTHORIZED.code();
body = String.format("Unauthorised: %s", errorMessage);
} else if (exception instanceof RejectedRequestException) {
metricRequestStatus = MetricName.ok;
errorMessages = Collections.emptyList();

status = HttpResponseStatus.OK.code();
context.response()
.headers()
.add(HttpUtil.CONTENT_TYPE_HEADER, HttpHeaderValues.APPLICATION_JSON);
body = mapper.encode(rejectedResponse());
} else {
metricRequestStatus = MetricName.err;
logger.error("Critical error while running the auction", exception);
Expand Down Expand Up @@ -164,4 +174,8 @@ private void handleResponseException(Throwable throwable) {
logger.warn("Failed to send video response: {0}", throwable.getMessage());
metrics.updateRequestTypeMetric(REQUEST_TYPE_METRIC, MetricName.networkerr);
}

private static VideoResponse rejectedResponse() {
return VideoResponse.of(Collections.emptyList(), null, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
import org.prebid.server.analytics.AnalyticsReporterDelegator;
import org.prebid.server.analytics.model.AmpEvent;
import org.prebid.server.analytics.model.HttpContext;
import org.prebid.server.auction.requestfactory.AmpRequestFactory;
import org.prebid.server.auction.AmpResponsePostProcessor;
import org.prebid.server.auction.ExchangeService;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.requestfactory.AmpRequestFactory;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.BidderCatalog;
import org.prebid.server.cookie.UidsCookie;
import org.prebid.server.exception.BlacklistedAccountException;
import org.prebid.server.exception.BlacklistedAppException;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.RejectedRequestException;
import org.prebid.server.exception.UnauthorizedAccountException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.TimeoutFactory;
Expand Down Expand Up @@ -265,6 +266,21 @@ public void shouldRespondWithUnauthorizedIfAccountIdIsInvalid() {
verify(httpResponse).end(eq("Account id is not provided"));
}

@Test
public void shouldRespondWithEmptyResponseIfRequestIsRejected() {
// given
given(ampRequestFactory.fromRequest(any(), anyLong()))
.willReturn(Future.failedFuture(new RejectedRequestException(null)));

// when
ampHandler.handle(routingContext);

// then
verifyZeroInteractions(exchangeService);
verify(httpResponse).setStatusCode(eq(200));
verify(httpResponse).end(eq("{\"targeting\":{}}"));
}

@Test
public void shouldRespondWithInternalServerErrorIfAuctionFails() {
// given
Expand Down Expand Up @@ -546,6 +562,19 @@ public void shouldIncrementBadinputAmpRequestMetrics() {
verify(metrics).updateRequestTypeMetric(eq(MetricName.amp), eq(MetricName.badinput));
}

@Test
public void shouldIncrementOkAmpRequestMetricIfRequestIsRejected() {
// given
given(ampRequestFactory.fromRequest(any(), anyLong()))
.willReturn(Future.failedFuture(new RejectedRequestException(null)));

// when
ampHandler.handle(routingContext);

// then
verify(metrics).updateRequestTypeMetric(eq(MetricName.amp), eq(MetricName.ok));
}

@Test
public void shouldIncrementErrAmpRequestMetrics() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import org.prebid.server.analytics.AnalyticsReporterDelegator;
import org.prebid.server.analytics.model.AuctionEvent;
import org.prebid.server.analytics.model.HttpContext;
import org.prebid.server.auction.requestfactory.AuctionRequestFactory;
import org.prebid.server.auction.ExchangeService;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.requestfactory.AuctionRequestFactory;
import org.prebid.server.cookie.UidsCookie;
import org.prebid.server.exception.BlacklistedAccountException;
import org.prebid.server.exception.BlacklistedAppException;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.RejectedRequestException;
import org.prebid.server.exception.UnauthorizedAccountException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.TimeoutFactory;
Expand Down Expand Up @@ -238,6 +239,21 @@ public void shouldRespondWithUnauthorizedIfAccountIdIsInvalid() {
verify(httpResponse).end(eq("Account id is not provided"));
}

@Test
public void shouldRespondWithEmptyResponseIfRequestIsRejected() {
// given
given(auctionRequestFactory.fromRequest(any(), anyLong()))
.willReturn(Future.failedFuture(new RejectedRequestException(null)));

// when
auctionHandler.handle(routingContext);

// then
verifyZeroInteractions(exchangeService);
verify(httpResponse).setStatusCode(eq(200));
verify(httpResponse).end(eq("{\"seatbid\":[]}"));
}

@Test
public void shouldRespondWithInternalServerErrorIfAuctionFails() {
// given
Expand Down Expand Up @@ -564,6 +580,19 @@ public void shouldIncrementRejectedMetricsIfUnknownUser() {
verify(metrics).updateAccountRequestRejectedMetrics(eq("1"));
}

@Test
public void shouldIncrementOkOpenrtb2WebRequestMetricIfRequestIsRejected() {
// given
given(auctionRequestFactory.fromRequest(any(), anyLong()))
.willReturn(Future.failedFuture(new RejectedRequestException(null)));

// when
auctionHandler.handle(routingContext);

// then
verify(metrics).updateRequestTypeMetric(eq(MetricName.openrtb2web), eq(MetricName.ok));
}

@Test
public void shouldPassBadRequestEventToAnalyticsReporterIfBidRequestIsInvalid() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import org.prebid.server.VertxTest;
import org.prebid.server.analytics.AnalyticsReporterDelegator;
import org.prebid.server.auction.ExchangeService;
import org.prebid.server.auction.requestfactory.VideoRequestFactory;
import org.prebid.server.auction.VideoResponseFactory;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.model.WithPodErrors;
import org.prebid.server.auction.requestfactory.VideoRequestFactory;
import org.prebid.server.cookie.UidsCookie;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.RejectedRequestException;
import org.prebid.server.exception.UnauthorizedAccountException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.TimeoutFactory;
Expand Down Expand Up @@ -166,6 +167,21 @@ public void shouldRespondWithUnauthorizedIfAccountIdIsInvalid() {
verify(httpResponse).end(eq("Unauthorised: Account id is not provided"));
}

@Test
public void shouldRespondWithEmptyResponseIfRequestIsRejected() {
// given
given(videoRequestFactory.fromRequest(any(), anyLong()))
.willReturn(Future.failedFuture(new RejectedRequestException(null)));

// when
videoHandler.handle(routingContext);

// then
verifyZeroInteractions(exchangeService);
verify(httpResponse).setStatusCode(eq(200));
verify(httpResponse).end(eq("{\"adPods\":[]}"));
}

@Test
public void shouldRespondWithInternalServerErrorIfAuctionFails() {
// given
Expand Down

0 comments on commit b88b688

Please sign in to comment.