Search in sources :

Example 1 with INTERNAL_SERVER_ERROR

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

the class UrlPatternRouter method handle.

@Override
public Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context) {
    for (RouteDescriptor route : alternatives) {
        if (request.method().equals(route.method())) {
            Matcher match = route.uriPattern().matcher(request.path());
            LOGGER.debug("Request path '{}' matching against route pattern '{}' matches: {}", new Object[] { request.path(), route.uriPattern(), match.matches() });
            if (match.matches()) {
                Map<String, String> placeholders = route.placeholderNames().stream().collect(toMap(name -> name, match::group));
                context.add(PLACEHOLDERS_KEY, placeholders);
                try {
                    return route.handler().handle(request, context);
                } catch (Exception cause) {
                    LOGGER.error("ERROR: {} {}", new Object[] { request.method(), request.path(), cause });
                    return Eventual.of(response(INTERNAL_SERVER_ERROR).build());
                }
            }
        }
    }
    return Eventual.of(response(NOT_FOUND).build());
}
Also used : Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) Logger(org.slf4j.Logger) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) PUT(com.hotels.styx.api.HttpMethod.PUT) HttpResponse.response(com.hotels.styx.api.HttpResponse.response) NOT_FOUND(com.hotels.styx.api.HttpResponseStatus.NOT_FOUND) HttpRequest(com.hotels.styx.api.HttpRequest) ArrayList(java.util.ArrayList) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) List(java.util.List) Matcher(java.util.regex.Matcher) Collectors.toMap(java.util.stream.Collectors.toMap) WebServiceHandler(com.hotels.styx.api.WebServiceHandler) Map(java.util.Map) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Pattern(java.util.regex.Pattern) DELETE(com.hotels.styx.api.HttpMethod.DELETE) LinkedList(java.util.LinkedList) GET(com.hotels.styx.api.HttpMethod.GET) POST(com.hotels.styx.api.HttpMethod.POST) HttpMethod(com.hotels.styx.api.HttpMethod) Matcher(java.util.regex.Matcher)

Example 2 with INTERNAL_SERVER_ERROR

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

the class HttpPipelineHandlerTest method mapsUnrecoverableInternalErrorsToInternalServerError500ResponseCode.

@Test
public void mapsUnrecoverableInternalErrorsToInternalServerError500ResponseCode() {
    HttpHandler handler = (request, context) -> {
        throw new RuntimeException("Forced exception for testing");
    };
    EmbeddedChannel channel = buildEmbeddedChannel(handlerWithMocks(handler));
    channel.writeInbound(httpRequestAsBuf(GET, "http://foo.com/"));
    DefaultHttpResponse response = (DefaultHttpResponse) channel.readOutbound();
    assertThat(response.status(), is(io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR));
    verify(responseEnhancer).enhance(any(LiveHttpResponse.Transformer.class), any(LiveHttpRequest.class));
    verify(errorListener, only()).proxyErrorOccurred(any(LiveHttpRequest.class), any(InetSocketAddress.class), eq(INTERNAL_SERVER_ERROR), any(RuntimeException.class));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpResponse(com.hotels.styx.api.HttpResponse) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) State(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) REQUEST_TIMEOUT(com.hotels.styx.api.HttpResponseStatus.REQUEST_TIMEOUT) SENDING_RESPONSE(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.SENDING_RESPONSE) WARN(ch.qos.logback.classic.Level.WARN) TERMINATED(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.TERMINATED) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) Is.is(org.hamcrest.core.Is.is) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) NettyToStyxRequestDecoder(com.hotels.styx.server.netty.codec.NettyToStyxRequestDecoder) BAD_REQUEST(com.hotels.styx.api.HttpResponseStatus.BAD_REQUEST) Mockito.only(org.mockito.Mockito.only) DecoderException(io.netty.handler.codec.DecoderException) HttpHandler(com.hotels.styx.api.HttpHandler) Gauge(io.micrometer.core.instrument.Gauge) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) REQUEST_ENTITY_TOO_LARGE(com.hotels.styx.api.HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE) UtilKt.append(com.hotels.styx.javaconvenience.UtilKt.append) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test) Matchers.any(org.mockito.Matchers.any) List(java.util.List) CONTENT_LENGTH(com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DO_NOT_MODIFY_RESPONSE(com.hotels.styx.server.netty.connectors.ResponseEnhancer.DO_NOT_MODIFY_RESPONSE) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) HttpErrorStatusListener(com.hotels.styx.server.HttpErrorStatusListener) RequestTimeoutException(com.hotels.styx.server.RequestTimeoutException) ACCEPTING_REQUESTS(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.ACCEPTING_REQUESTS) Mockito.mock(org.mockito.Mockito.mock) WAITING_FOR_RESPONSE(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.WAITING_FOR_RESPONSE) LoggingTestSupport(com.hotels.styx.support.matchers.LoggingTestSupport) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LoggingEventMatcher.loggingEvent(com.hotels.styx.support.matchers.LoggingEventMatcher.loggingEvent) HttpMessageSupport.httpRequestAsBuf(com.hotels.styx.support.netty.HttpMessageSupport.httpRequestAsBuf) ArgumentCaptor(org.mockito.ArgumentCaptor) JustATestException(com.hotels.styx.support.JustATestException) EmitterProcessor(reactor.core.publisher.EmitterProcessor) CONNECTION(com.hotels.styx.api.HttpHeaderNames.CONNECTION) Matchers.anyObject(org.mockito.Matchers.anyObject) HttpResponseWriterFactory(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.HttpResponseWriterFactory) StyxClientException(com.hotels.styx.client.StyxClientException) CLOSE(com.hotels.styx.api.HttpHeaderValues.CLOSE) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) RequestStatsCollector(com.hotels.styx.server.RequestStatsCollector) INFO(ch.qos.logback.classic.Level.INFO) Eventual(com.hotels.styx.api.Eventual) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) GET(io.netty.handler.codec.http.HttpMethod.GET) UTF_8(java.nio.charset.StandardCharsets.UTF_8) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) BadRequestException(com.hotels.styx.server.BadRequestException) ChannelFuture(io.netty.channel.ChannelFuture) Mockito.verify(org.mockito.Mockito.verify) ContentOverflowException(com.hotels.styx.api.ContentOverflowException) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Channel(io.netty.channel.Channel) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) MeterRegistry(com.hotels.styx.api.MeterRegistry) SENDING_RESPONSE_CLIENT_CLOSED(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.SENDING_RESPONSE_CLIENT_CLOSED) OK(com.hotels.styx.api.HttpResponseStatus.OK) CentralisedMetrics(com.hotels.styx.metrics.CentralisedMetrics) ChannelHandler(io.netty.channel.ChannelHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) BAD_GATEWAY(com.hotels.styx.api.HttpResponseStatus.BAD_GATEWAY) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpHandler(com.hotels.styx.api.HttpHandler) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test)

Example 3 with INTERNAL_SERVER_ERROR

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

the class InstrumentedPluginTest method metricIsNotRecordedWhenErrorStatusIsReturnedByChain.

@Test
public void metricIsNotRecordedWhenErrorStatusIsReturnedByChain() {
    Chain chain = request -> aResponse(INTERNAL_SERVER_ERROR);
    String pluginName = "doNotRecordMe";
    InstrumentedPlugin plugin = instrumentedPlugin(pluginName, PASS_THROUGH);
    LiveHttpResponse response = Mono.from(plugin.intercept(someRequest, chain)).block();
    assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
    assertThat(getStatusCount(pluginName, "500"), is(0.0));
    assertThat(getErrorCount(pluginName), is(0.0));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) Eventual.error(com.hotels.styx.api.Eventual.error) Matchers.not(org.hamcrest.Matchers.not) PluginException(com.hotels.styx.api.plugins.spi.PluginException) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) PASS_THROUGH(com.hotels.styx.api.plugins.spi.Plugin.PASS_THROUGH) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Counter(io.micrometer.core.instrument.Counter) Eventual(com.hotels.styx.api.Eventual) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Mono(reactor.core.publisher.Mono) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) Metrics.formattedExceptionName(com.hotels.styx.api.Metrics.formattedExceptionName) Chain(com.hotels.styx.api.HttpInterceptor.Chain) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) MeterRegistry(com.hotels.styx.api.MeterRegistry) Environment(com.hotels.styx.Environment) OK(com.hotels.styx.api.HttpResponseStatus.OK) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) BAD_GATEWAY(com.hotels.styx.api.HttpResponseStatus.BAD_GATEWAY) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Mockito.mock(org.mockito.Mockito.mock) Chain(com.hotels.styx.api.HttpInterceptor.Chain) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 4 with INTERNAL_SERVER_ERROR

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

the class InstrumentedPluginTest method metricIsRecordedWhenResponseIsMappedToErrorStatus.

@Test
public void metricIsRecordedWhenResponseIsMappedToErrorStatus() {
    Chain chain = request -> aResponse(OK);
    String pluginName = "replaceStatus1";
    InstrumentedPlugin plugin = instrumentedPlugin(pluginName, (request, aChain) -> aChain.proceed(request).map(response -> responseWithNewStatusCode(response, INTERNAL_SERVER_ERROR)));
    LiveHttpResponse response = Mono.from(plugin.intercept(someRequest, chain)).block();
    assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
    assertThat(getStatusCount(pluginName, "500"), is(1.0));
    assertThat(getErrorCount(pluginName), is(1.0));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) Eventual.error(com.hotels.styx.api.Eventual.error) Matchers.not(org.hamcrest.Matchers.not) PluginException(com.hotels.styx.api.plugins.spi.PluginException) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) PASS_THROUGH(com.hotels.styx.api.plugins.spi.Plugin.PASS_THROUGH) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Counter(io.micrometer.core.instrument.Counter) Eventual(com.hotels.styx.api.Eventual) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Mono(reactor.core.publisher.Mono) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) Metrics.formattedExceptionName(com.hotels.styx.api.Metrics.formattedExceptionName) Chain(com.hotels.styx.api.HttpInterceptor.Chain) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) MeterRegistry(com.hotels.styx.api.MeterRegistry) Environment(com.hotels.styx.Environment) OK(com.hotels.styx.api.HttpResponseStatus.OK) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) BAD_GATEWAY(com.hotels.styx.api.HttpResponseStatus.BAD_GATEWAY) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Mockito.mock(org.mockito.Mockito.mock) Chain(com.hotels.styx.api.HttpInterceptor.Chain) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Eventual (com.hotels.styx.api.Eventual)4 INTERNAL_SERVER_ERROR (com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR)4 BAD_GATEWAY (com.hotels.styx.api.HttpResponseStatus.BAD_GATEWAY)3 OK (com.hotels.styx.api.HttpResponseStatus.OK)3 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)3 LiveHttpRequest.get (com.hotels.styx.api.LiveHttpRequest.get)3 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)3 LiveHttpResponse.response (com.hotels.styx.api.LiveHttpResponse.response)3 MeterRegistry (com.hotels.styx.api.MeterRegistry)3 MicrometerRegistry (com.hotels.styx.api.MicrometerRegistry)3 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)3 Optional (java.util.Optional)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Test (org.junit.jupiter.api.Test)3 Matchers.any (org.mockito.Matchers.any)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.never (org.mockito.Mockito.never)3 Mockito.verify (org.mockito.Mockito.verify)3 Mono (reactor.core.publisher.Mono)3