Commit 7140b2c2 authored by sgandhi@nisum.com's avatar sgandhi@nisum.com

EPE-006 : Added exception handler and removed configuration class

parent c1f321e7
......@@ -32,6 +32,7 @@ ext {
dependencies {
//TODO: Make it
compile group: 'org.json', name: 'json', version: '20190722'
compile 'org.mapstruct:mapstruct:1.3.1.Final'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
......
package com.nisum.offertransactionservice.client;
import com.nisum.offertransactionservice.config.FeignClientConfiguration;
import com.nisum.offertransactionservice.dto.PERequest;
import com.nisum.offertransactionservice.dto.PEResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
@FeignClient(name = "${pe.application.name}", configuration = FeignClientConfiguration.class)
@FeignClient(name = "${pe.application.name}")
public interface FeignClientService {
@PostMapping(value = "${endpoint.url.promotionEngineUrl}")
......
package com.nisum.offertransactionservice.config;
import com.nisum.offertransactionservice.genericexception.GlobalApiGenericException;
import feign.Response;
import feign.codec.ErrorDecoder;
public class CustomErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
String httpStatusDesc = null;
switch (response.status()) {
case 400:
httpStatusDesc = "Bad Request";
break;
case 404:
httpStatusDesc = "Not found";
break;
case 500:
httpStatusDesc = "Internal Error";
break;
default:
httpStatusDesc = "Generic error";
}
return new GlobalApiGenericException(Integer.toString(response.status()), true, null);
}
}
package com.nisum.offertransactionservice.config;
import feign.Logger;
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignClientConfiguration {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
public ErrorDecoder errorDecoder() {
return new CustomErrorDecoder();
}
}
\ No newline at end of file
package com.nisum.offertransactionservice.handler;
import feign.FeignException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
@RestControllerAdvice
public class FeignExceptionHandler {
@ExceptionHandler({FeignException.BadRequest.class,FeignException.InternalServerError.class,FeignException.NotFound.class})
public Map<String, Object> handleFeignStatusException(FeignException e, HttpServletResponse response) {
response.setStatus(e.status());
return new JSONObject(e.contentUTF8()).toMap();
}
}
......@@ -59,11 +59,9 @@ public class OfferCallingPEService {
PERequest peRequest = INSTANCE.map(offerTransactionRequest);
peRequest.setEligibleOffers(offerLookupDTOList);
log.info("Promotional Engine Feign client call Start");
/*PEResponse peResponseFlux = clientService.getPeResponseFlux(peRequest);*/
ResponseEntity<PEResponse> peResponseFlux = feignClientService.callPEService(peRequest);
ResponseEntity<PEResponse> peResponse = feignClientService.callPEService(peRequest);
log.debug("Promotional Engine Feign client call End");
offerTransactionResponse = INSTANCE.map(peResponseFlux.getBody());
offerTransactionResponse = INSTANCE.map(peResponse.getBody());
offerTransactionResponse.setTransactionId(offerTransactionRequest.getTransactionId());
log.debug("Offer Transaction Response {}", offerTransactionResponse);
return offerTransactionResponse;
......
......@@ -8,5 +8,4 @@ endpoint.url.spBaseUrl=http://localhost:7070
server.port = 7072
spring.application.name=ots
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
\ No newline at end of file
//TODO: add refresh eureka endpoint properties
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment