Class CustomHttpErrors

java.lang.Object
org.apache.beam.sdk.extensions.gcp.util.CustomHttpErrors

public class CustomHttpErrors extends Object
An optional component to use with the RetryHttpRequestInitializer in order to provide custom errors for failing http calls. This class allows you to specify custom error messages which match specific error codes and containing strings in the URL. The first matcher to match the request and response will be used to provide the custom error.

The intended use case here is to examine one of the logs emitted by a failing call made by the RetryHttpRequestInitializer, and then adding a custom error message which matches the URL and code for it.

Usage: See more in CustomHttpErrorsTest.


 CustomHttpErrors.Builder builder = new CustomHttpErrors.Builder();
 builder.addErrorForCodeAndUrlContains(403,"/tables?", "Custom Error Msg");
 CustomHttpErrors customErrors = builder.build();


 RetryHttpRequestInitializer initializer = ...
 initializer.setCustomErrors(customErrors);
 

Suggestions for future enhancements to anyone upgrading this file:

  • This class is left open for extension, to allow different functions for HttpCallMatcher and HttpCallCustomError to match and log errors. For example, new functionality may include matching an error based on the HttpResponse body. Additionally, extracting and logging strings from the HttpResponse body may make useful functionality.
  • Add a methods to add custom errors based on inspecting the contents of the HttpRequest and HttpResponse
  • Be sure to update the HttpRequestWrapper and HttpResponseWrapper with any new getters that you may use. The wrappers were introduced to add a layer of indirection which could be mocked mocked out in tests. This was unfortunately needed because mockito cannot mock final classes and its non trivial to just construct HttpRequest and HttpResponse objects.
  • Making matchers composable with an AND operator may simplify enhancing this code, if several different matchers are used.

  • Method Details

    • getCustomError

      public String getCustomError(org.apache.beam.sdk.extensions.gcp.util.HttpRequestWrapper req, org.apache.beam.sdk.extensions.gcp.util.HttpResponseWrapper res)
      Returns:
      The first custom error for the failing request and response to match, or null.