com.netflix.hystrix.exception.HystrixBadRequestException Java Examples

The following examples show how to use com.netflix.hystrix.exception.HystrixBadRequestException. 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: ResultErrorDecoder.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@Override
  public Exception decode(String methodKey, Response response) {
if((response.status() >= 200 && response.status() < 300) || 
		response.status()==HttpStatus.BAD_REQUEST.value() ||
		response.status()==HttpStatus.INTERNAL_SERVER_ERROR.value() ){
	try {
		HttpMessageConverterExtractor<SimpleDataResult<Object>> extractor = new HttpMessageConverterExtractor<SimpleDataResult<Object>>(SimpleDataResult.class, this.httpMessageConverters.getConverters());
		SimpleDataResult<Object> result = extractor.extractData(new FeignResponseAdapter(response));
		log.error("method[{}], error code: {}, result: {}", methodKey, response.status(), result);
		//防止普通异常也被熔断,如果不转为 HystrixBadRequestException 并且 fallback 也抛了异常, it will be enabled short-circuited get "Hystrix circuit short-circuited and is OPEN" when client frequently invoke
		if(result!=null){
			return new HystrixBadRequestException(result.getMessage(), new ServiceException(result.getMessage(), result.getCode()));
		}
	} catch (IOException e) {
		throw new BaseException("error feign response : " + e.getMessage(), e);
	}
}

  	Exception exception = defaultDecoder.decode(methodKey, response);
  	return exception;
  }
 
Example #2
Source File: ExtResponseEntityDecoder.java    From onetwo with Apache License 2.0 6 votes vote down vote up
protected Object handleDataResult(FeignResponseAdapter response, Object res) {
	Object result = res;
	if (result instanceof DataResult) {
		DataResult<?> dr = (DataResult<?>) result;
		if(isCutoutError(response, dr)){
			ServiceException cause = new ServiceException(dr.getMessage(), dr.getCode());
			String message = "cutoutError, remote service error:"+dr.getMessage();
			JFishLoggerFactory.findMailLogger().error(message);
			
			throw new HystrixRuntimeException(FailureType.SHORTCIRCUIT, OkHttpRibbonCommand.class, message, cause, null);
		}else if(!dr.isSuccess()){
			throw new HystrixBadRequestException(dr.getMessage(), new ServiceException(dr.getMessage(), dr.getCode()));
		}
		res = dr.getData();
	}
	return result;
}
 
Example #3
Source File: ExtResponseEntityDecoder.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes" })
	@Deprecated
	protected Object decode2(FeignResponseAdapter response, Type type) throws IOException, FeignException {
		Object res = null;
		try {
			res = decodeByType(response, type);
		} catch (HttpMessageNotReadableException e) {
			//正常解码失败后尝试用SimpleDataResult解码
			response.getBody().reset();
			SimpleDataResult dr = decodeByType(response, SimpleDataResult.class);
			if(dr.isError()){
//				throw new ServiceException(dr.getMessage(), dr.getCode());
				throw new HystrixBadRequestException(dr.getMessage(), new ServiceException(dr.getMessage(), dr.getCode()));
			}
			res = dr.getData();
		}
		return res;
	}
 
Example #4
Source File: Oauth2FeignErrorInterceptor.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Decode exception.
 *
 * @param methodKey the method key
 * @param response  the response
 *
 * @return the exception
 */
@Override
public Exception decode(final String methodKey, final Response response) {
	if (response.status() >= HttpStatus.BAD_REQUEST.value() && response.status() < HttpStatus.INTERNAL_SERVER_ERROR.value()) {
		return new HystrixBadRequestException("request exception wrapper");
	}

	ObjectMapper mapper = new ObjectMapper();
	try {
		HashMap map = mapper.readValue(response.body().asInputStream(), HashMap.class);
		Integer code = (Integer) map.get("code");
		String message = (String) map.get("message");
		if (code != null) {
			ErrorCodeEnum anEnum = ErrorCodeEnum.getEnum(code);
			if (anEnum != null) {
				if (anEnum == ErrorCodeEnum.GL99990100) {
					throw new IllegalArgumentException(message);
				} else {
					throw new BusinessException(anEnum);
				}
			} else {
				throw new BusinessException(ErrorCodeEnum.GL99990500, message);
			}
		}
	} catch (IOException e) {
		log.info("Failed to process response body");
	}
	return defaultErrorDecoder.decode(methodKey, response);
}
 
Example #5
Source File: UserService.java    From spring-cloud-docker-microservice-book-code with Apache License 2.0 4 votes vote down vote up
public User findById(Long id) {
    throw new HystrixBadRequestException("business exception happens.", new Exception(""));
}
 
Example #6
Source File: HystrixConfigurationTest.java    From fullstop with Apache License 2.0 4 votes vote down vote up
@HystrixCommand(fallbackMethod = "fallback")
public String badRequest() {
    // simulate failed input validation. Fallback won't be executed.
    throw new HystrixBadRequestException("Bla", new IllegalArgumentException("validation failed"));
}