Skip to content

Commit

Permalink
Merge pull request #165 from ajordens/propagate-429
Browse files Browse the repository at this point in the history
Pass any 429's through without triggering any Hystrix events
  • Loading branch information
ajordens committed Dec 7, 2015
2 parents 45fd7aa + e08eecc commit 7022127
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.netflix.hystrix.exception.HystrixBadRequestException;
import retrofit.RetrofitError;
import java.util.Collection;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static retrofit.RetrofitError.Kind.HTTP;

Expand Down Expand Up @@ -48,4 +49,12 @@ public static Exception classifyError(RetrofitError error) {
}
}

public static Exception classifyError(RetrofitError error, Collection<Integer> supportedHttpStatuses) {
if (error.getKind() == HTTP && supportedHttpStatuses.contains(error.getResponse().getStatus())) {
return new UpstreamBadRequest(error);
} else {
return error;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ package com.netflix.spinnaker.gate.services.commands

import com.netflix.hystrix.HystrixCommand
import com.netflix.hystrix.HystrixCommandKey
import com.netflix.spinnaker.gate.retrofit.UpstreamBadRequest
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.springframework.http.HttpStatus
import retrofit.RetrofitError

import static HystrixFactory.createHystrixCommandPropertiesSetter
import static HystrixFactory.toGroupKey
import static retrofit.RetrofitError.Kind.HTTP

@Slf4j
@CompileStatic
Expand All @@ -49,7 +53,11 @@ abstract class AbstractHystrixCommand<T> extends HystrixCommand<T> {

@Override
protected T run() throws Exception {
return work()
try {
return work()
} catch (RetrofitError error) {
throw UpstreamBadRequest.classifyError(error, HttpStatus.values().findAll { it.is4xxClientError() }*.value() as Collection<Integer>)
}
}

protected T getFallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@

package com.netflix.spinnaker.gate.config

import com.netflix.hystrix.exception.HystrixRuntimeException
import com.netflix.spectator.api.Registry
import com.netflix.spinnaker.gate.retrofit.UpstreamBadRequest
import com.netflix.spinnaker.kork.web.interceptors.MetricsInterceptor
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.filter.ShallowEtagHeaderFilter
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
import retrofit.RetrofitError

import javax.servlet.Filter
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@Configuration
@ComponentScan
Expand All @@ -46,4 +57,36 @@ public class GateWebConfig extends WebMvcConfigurerAdapter {
Filter eTagFilter() {
new ShallowEtagHeaderFilter()
}

@Bean
UpstreamBadRequestExceptionHandler upstreamBadRequestExceptionHandler() {
return new UpstreamBadRequestExceptionHandler()
}

@ControllerAdvice
static class UpstreamBadRequestExceptionHandler {
@ResponseBody
@ExceptionHandler(UpstreamBadRequest)
public Map handleUpstreamBadRequest(
HttpServletRequest request,
HttpServletResponse response,
UpstreamBadRequest exception
) {
response.setStatus(exception.status)

def failureCause = exception.cause
if (failureCause instanceof RetrofitError) {
failureCause = failureCause.cause ?: failureCause
}

return [
failureCause: failureCause.toString(),
error: HttpStatus.valueOf(exception.status).reasonPhrase,
message: exception.message,
status: exception.status,
url: exception.url,
timestamp: System.currentTimeMillis()
]
}
}
}

0 comments on commit 7022127

Please sign in to comment.