Search in sources :

Example 1 with HttpResponseStatus

use of com.hotels.styx.api.HttpResponseStatus in project styx by ExpediaGroup.

the class ExceptionStatusMapperTest method exceptionMayNotBeMappedToMultipleExceptions.

@Test
public void exceptionMayNotBeMappedToMultipleExceptions() {
    ExceptionStatusMapper mapper = new ExceptionStatusMapper.Builder().add(BAD_GATEWAY, Exception1.class).add(GATEWAY_TIMEOUT, DoubleMappedException.class).build();
    LoggingTestSupport support = new LoggingTestSupport(ExceptionStatusMapper.class);
    Optional<HttpResponseStatus> status;
    try {
        status = mapper.statusFor(new DoubleMappedException());
    } finally {
        assertThat(support.lastMessage(), is(loggingEvent(ERROR, "Multiple matching statuses for throwable=" + quote(DoubleMappedException.class.getName()) + " statuses=\\[502 Bad Gateway, 504 Gateway Timeout\\]")));
    }
    assertThat(status, isAbsent());
}
Also used : HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) LoggingTestSupport(com.hotels.styx.support.matchers.LoggingTestSupport) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with HttpResponseStatus

use of com.hotels.styx.api.HttpResponseStatus in project styx by ExpediaGroup.

the class HttpPipelineHandler method exceptionToResponse.

private LiveHttpResponse exceptionToResponse(Throwable cause, LiveHttpRequest request, CharSequence originsHeaderName) {
    HttpResponseStatus status = status(cause instanceof PluginException ? cause.getCause() : cause);
    String message = status.code() >= 500 ? "Site temporarily unavailable." : status.description();
    LiveHttpResponse.Transformer builder = responseEnhancer.enhance(response(status).body(new ByteStream(Flux.just(new Buffer(message, UTF_8)))).build().newBuilder(), request).header(CONTENT_LENGTH, message.getBytes(UTF_8).length).header(CONNECTION, "close");
    if (originsHeaderName != null && originFromException(cause) != null) {
        return builder.header(originsHeaderName, originFromException(cause)).build();
    } else {
        return builder.build();
    }
}
Also used : Buffer(com.hotels.styx.api.Buffer) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) PluginException(com.hotels.styx.api.plugins.spi.PluginException) ByteStream(com.hotels.styx.api.ByteStream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse)

Aggregations

HttpResponseStatus (com.hotels.styx.api.HttpResponseStatus)2 Buffer (com.hotels.styx.api.Buffer)1 ByteStream (com.hotels.styx.api.ByteStream)1 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)1 PluginException (com.hotels.styx.api.plugins.spi.PluginException)1 LoggingTestSupport (com.hotels.styx.support.matchers.LoggingTestSupport)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1