Java Code Examples for com.nike.backstopper.apierror.ApiError#getErrorCode()

The following examples show how to use com.nike.backstopper.apierror.ApiError#getErrorCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void should_not_contain_same_error_codes_for_different_instances_that_are_not_wrappers() {
    Set<String> allowedDuplicateErrorCodes = allowedDuplicateErrorCodes();
    Map<String, ApiError> codeToErrorMap = new HashMap<>();
    for (ApiError apiError : getProjectApiErrors().getProjectApiErrors()) {
        ApiError errorWithSameCode = codeToErrorMap.get(apiError.getErrorCode());

        if (errorWithSameCode != null && !areWrappersOfEachOther(apiError, errorWithSameCode)
            && !allowedDuplicateErrorCodes.contains(apiError.getErrorCode()))
        {
            throw new AssertionError(
                "There are ApiError instances in the ProjectApiErrors that share duplicate error codes and are not "
                + "wrappers of each other. error_code=" + apiError.getErrorCode() + ", conflicting_api_errors=["
                + apiError.getName() + ", " + errorWithSameCode.getName() + "]"
            );
        }

        codeToErrorMap.put(apiError.getErrorCode(), apiError);
    }
}
 
Example 2
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void allErrorsShouldBeCoreApiErrorsOrCoreApiErrorWrappersOrFallInProjectSpecificErrorRange() {
    ProjectSpecificErrorCodeRange projectSpecificErrorCodeRange = getProjectApiErrors().getProjectSpecificErrorCodeRange();

    for (ApiError error : getProjectApiErrors().getProjectApiErrors()) {
        boolean valid = false;
        if (getProjectApiErrors().getCoreApiErrors().contains(error) || getProjectApiErrors().isWrapperAroundCoreError(error, getProjectApiErrors().getCoreApiErrors()))
            valid = true;
        else if (projectSpecificErrorCodeRange != null && projectSpecificErrorCodeRange.isInRange(error))
            valid = true;

        if (!valid) {
            throw new AssertionError("Found an ApiError in the ProjectApiErrors that is not a core error or wrapper around a core error, and its error code does not fall in the  " +
                                     "range of getProjectApiErrors().getProjectSpecificErrorCodeRange(). getProjectApiErrors().getProjectSpecificErrorCodeRange(): " + projectSpecificErrorCodeRange +
                                     ". Offending error info: name=" + error.getName() + ", errorCode=" + error.getErrorCode() + ", message=\"" + error.getMessage() + "\", httpStatusCode=" +
                                     error.getHttpStatusCode() + ", class=" + error.getClass().getName());
        }
    }
}
 
Example 3
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void should_not_contain_same_error_codes_for_different_instances_that_are_not_wrappers() {
    Set<String> allowedDuplicateErrorCodes = allowedDuplicateErrorCodes();
    Map<String, ApiError> codeToErrorMap = new HashMap<>();
    for (ApiError apiError : getProjectApiErrors().getProjectApiErrors()) {
        ApiError errorWithSameCode = codeToErrorMap.get(apiError.getErrorCode());

        if (errorWithSameCode != null && !areWrappersOfEachOther(apiError, errorWithSameCode)
            && !allowedDuplicateErrorCodes.contains(apiError.getErrorCode())) {
            throw new AssertionError(
                "There are ApiError instances in the ProjectApiErrors that share duplicate error codes and are not "
                + "wrappers of each other. error_code=" + apiError.getErrorCode() + ", conflicting_api_errors=["
                + apiError.getName() + ", " + errorWithSameCode.getName() + "]"
            );
        }

        codeToErrorMap.put(apiError.getErrorCode(), apiError);
    }
}
 
Example 4
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void allErrorsShouldBeCoreApiErrorsOrCoreApiErrorWrappersOrFallInProjectSpecificErrorRange() {
    ProjectSpecificErrorCodeRange projectSpecificErrorCodeRange =
        getProjectApiErrors().getProjectSpecificErrorCodeRange();

    for (ApiError error : getProjectApiErrors().getProjectApiErrors()) {
        boolean valid = false;
        if (getProjectApiErrors().getCoreApiErrors().contains(error) || getProjectApiErrors()
            .isWrapperAroundCoreError(error, getProjectApiErrors().getCoreApiErrors()))
            valid = true;
        else if (projectSpecificErrorCodeRange != null && projectSpecificErrorCodeRange.isInRange(error))
            valid = true;

        if (!valid) {
            throw new AssertionError(
                "Found an ApiError in the ProjectApiErrors that is not a core error or wrapper around a core error, and its error code does not fall in the  "
                +
                "range of getProjectApiErrors().getProjectSpecificErrorCodeRange(). getProjectApiErrors().getProjectSpecificErrorCodeRange(): "
                + projectSpecificErrorCodeRange +
                ". Offending error info: name=" + error.getName() + ", errorCode=" + error.getErrorCode()
                + ", message=\"" + error.getMessage() + "\", httpStatusCode=" +
                error.getHttpStatusCode() + ", class=" + error.getClass().getName());
        }
    }
}
 
Example 5
Source File: VerifyMiscellaneousFunctionalityComponentTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void verify_proxy_router_response_modification_works_as_expected() throws IOException, InterruptedException {

    ExtractableResponse response =
        given()
            .baseUri("http://127.0.0.1")
            .port(serverConfig.endpointsPort())
            .basePath(ProxyRouterResponseModificationEndpoint.MATCHING_PATH)
            .log().all()
        .when()
            .get()
        .then()
            .log().headers()
            .extract();

    ApiError unmodifiedError = EmptyMetadataErrorThrower.ERROR_NO_METADATA;
    assertThat(response.statusCode()).isEqualTo(ProxyRouterResponseModificationEndpoint.MODIFIED_HTTP_STATUS_RESPONSE_CODE);
    assertThat(response.header(ProxyRouterResponseModificationEndpoint.ORIG_HTTP_STATUS_CODE_RESPONSE_HEADER_KEY))
        .isEqualTo(String.valueOf(unmodifiedError.getHttpStatusCode()));
    ApiError expectedModifiedError = new ApiErrorBase(unmodifiedError.getName(), unmodifiedError.getErrorCode(), unmodifiedError.getMessage(),
                                                      ProxyRouterResponseModificationEndpoint.MODIFIED_HTTP_STATUS_RESPONSE_CODE);
    verifyErrorReceived(response, expectedModifiedError);
    assertThat(response.asString()).doesNotContain("metadata");
}
 
Example 6
Source File: ProjectApiErrors.java    From backstopper with Apache License 2.0 5 votes vote down vote up
/**
 * Compares the list of project-specific errors against {@link #getProjectSpecificErrorCodeRange()} to verify that
 * all errors fall within this project's valid range. If there is an error that falls outside the valid range then
 * an {@link java.lang.IllegalStateException} will be thrown.
 *
 * <p>The given list of core errors is used to determine if any of the project-specific errors are "wrappers" around
 * a core error, which is the only allowed case where a project-specific error can have an error code outside the
 * allowed range.
 *
 * <p>An {@link java.lang.IllegalStateException} will also be thrown if {@link #getProjectSpecificErrorCodeRange()}
 * is null but you have any project-specific errors that are not core error wrappers (the
 * {@link #getProjectSpecificErrorCodeRange()} is allowed to be null only if you have 100% core errors or core error
 * wrappers).
 */
protected void verifyErrorsAreInRange(List<ApiError> projectSpecificErrors, List<ApiError> coreErrors) {
    ProjectSpecificErrorCodeRange validRange = getProjectSpecificErrorCodeRange();

    for (ApiError projectError : projectSpecificErrors) {
        // Ignore wrappers around core errors
        boolean isCoreError = isWrapperAroundCoreError(projectError, coreErrors);

        if (!isCoreError) {
            // It's not a wrapper around a core error.

            // If validRange is null at this point then that constitutes an error since
            //      getProjectSpecificErrorCodeRange() is only allowed to be null if the project is
            //      100% core errors.
            if (validRange == null) {
                throw new IllegalStateException(
                    "The ProjectSpecificErrorCodeRange for this project is null, but there is an ApiError that is "
                    + "not a core error. This project must have a ProjectSpecificErrorCodeRange that covers all "
                    + "non-core errors. Offending ApiError: " + projectError.getName()
                );
            }

            // Check to make sure the project-specific error falls into the project's error range.
            if (!validRange.isInRange(projectError)) {
                throw new IllegalStateException(
                    "Found ApiError for this project with an error code that does not fall within the valid range "
                    + "specified by " + validRange.getName() + ". ApiError: " + projectError.getName()
                    + ". Error code value: " + projectError.getErrorCode()
                );
            }
        }
    }
}
 
Example 7
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyErrorsAreInRangeShouldNotThrowExceptionIfListIncludesCoreApiErrorWrapper() {
    ApiError coreApiError = BarebonesCoreApiErrorForTesting.GENERIC_SERVICE_ERROR;
    final ApiError coreApiErrorWrapper = new ApiErrorBase("blah", coreApiError.getErrorCode(), coreApiError.getMessage(), coreApiError.getHttpStatusCode());
    ProjectApiErrors pae = ProjectApiErrorsForTesting.withProjectSpecificData(Collections.singletonList(coreApiErrorWrapper), null);

    assertThat(pae, notNullValue());
    assertThat(pae.getProjectApiErrors().contains(coreApiErrorWrapper), is(true));
}
 
Example 8
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyErrorsAreInRangeShouldNotThrowExceptionIfListIncludesCoreApiErrorWrapper() {
    ApiError coreApiError = BarebonesCoreApiErrorForTesting.GENERIC_SERVICE_ERROR;
    final ApiError coreApiErrorWrapper =
        new ApiErrorBase("blah", coreApiError.getErrorCode(), coreApiError.getMessage(),
                         coreApiError.getHttpStatusCode());
    ProjectApiErrors pae =
        ProjectApiErrorsForTesting.withProjectSpecificData(Collections.singletonList(coreApiErrorWrapper), null);

    assertThat(pae, notNullValue());
    assertThat(pae.getProjectApiErrors().contains(coreApiErrorWrapper), is(true));
}
 
Example 9
Source File: BackstopperRiposteFrameworkErrorHandlerListener.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Inject
public BackstopperRiposteFrameworkErrorHandlerListener(@NotNull ProjectApiErrors projectApiErrors) {
    //noinspection ConstantConditions
    if (projectApiErrors == null)
        throw new IllegalArgumentException("ProjectApiErrors cannot be null");

    this.projectApiErrors = projectApiErrors;

    CIRCUIT_BREAKER_GENERIC_API_ERROR =
        new ApiErrorBase(projectApiErrors.getTemporaryServiceProblemApiError(), "CIRCUIT_BREAKER");
    CIRCUIT_BREAKER_OPEN_API_ERROR =
        new ApiErrorBase(projectApiErrors.getTemporaryServiceProblemApiError(), "CIRCUIT_BREAKER_OPEN");
    CIRCUIT_BREAKER_TIMEOUT_API_ERROR =
        new ApiErrorBase(projectApiErrors.getTemporaryServiceProblemApiError(), "CIRCUIT_BREAKER_TIMEOUT");

    ApiError malformedReqError = projectApiErrors.getMalformedRequestApiError();
    // Too long line can keep generic malformed request ApiError error code, message, and HTTP status code (400),
    //      but we'll give it a new name for the logs and some cause metadata so the caller knows what went wrong.
    TOO_LONG_FRAME_LINE_API_ERROR_BASE = new ApiErrorWithMetadata(
        new ApiErrorBase(malformedReqError, "TOO_LONG_HTTP_LINE"),
        Pair.of("cause", TOO_LONG_FRAME_LINE_METADATA_MESSAGE)
    );
    // Too long headers should keep the error code and message of malformed request ApiError, but use 431 HTTP
    //      status code (see https://tools.ietf.org/html/rfc6585#page-4), and we'll give it a new name for the logs
    //      and some cause metadata so the caller knows what went wrong.
    TOO_LONG_FRAME_HEADER_API_ERROR_BASE = new ApiErrorWithMetadata(
        new ApiErrorBase(
            "TOO_LONG_HEADERS", malformedReqError.getErrorCode(), malformedReqError.getMessage(),
            431, malformedReqError.getMetadata()
        ),
        Pair.of("cause", TOO_LONG_FRAME_HEADER_METADATA_MESSAGE)
    );
}
 
Example 10
Source File: DefaultErrorDTO.java    From backstopper with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new instance with code, message, and metadata pulled from the given {@link ApiError}.
 */
public DefaultErrorDTO(ApiError error) {
    this(error.getErrorCode(), error.getMessage(), error.getMetadata());
}