-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add first exception * Start using global exception in core service * Configure global exception in user service
- Loading branch information
1 parent
a847b47
commit 1ffb7db
Showing
9 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...ervice/src/main/java/org/sagebionetworks/challenge/exception/EntityNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.sagebionetworks.challenge.exception; | ||
|
||
import org.sagebionetworks.challenge.util.exception.SimpleChallengeGlobalException; | ||
|
||
public class EntityNotFoundException extends SimpleChallengeGlobalException { | ||
public EntityNotFoundException() { | ||
super("Requested entity not present in the DB.", GlobalErrorCode.ERROR_ENTITY_NOT_FOUND); | ||
} | ||
|
||
public EntityNotFoundException(String message) { | ||
super(message, GlobalErrorCode.ERROR_ENTITY_NOT_FOUND); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...e-core-service/src/main/java/org/sagebionetworks/challenge/exception/GlobalErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.sagebionetworks.challenge.exception; | ||
|
||
public class GlobalErrorCode { | ||
public static final String ERROR_ENTITY_NOT_FOUND = "CHALLENGE-CORE-SERVICE-1000"; | ||
public static final String INSUFFICIENT_FUNDS = "CHALLENGE-CORE-SERVICE-1001"; | ||
} |
28 changes: 28 additions & 0 deletions
28
...service/src/main/java/org/sagebionetworks/challenge/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.sagebionetworks.challenge.exception; | ||
|
||
import org.sagebionetworks.challenge.util.exception.SimpleChallengeGlobalException; | ||
import org.sagebionetworks.challenge.util.exception.ErrorResponse; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
import java.util.Locale; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(SimpleChallengeGlobalException.class) | ||
protected ResponseEntity handleGlobalException( | ||
SimpleChallengeGlobalException simpleChallengeGlobalException, Locale locale) { | ||
return ResponseEntity.badRequest() | ||
.body(ErrorResponse.builder().code(simpleChallengeGlobalException.getCode()) | ||
.message(simpleChallengeGlobalException.getMessage()).build()); | ||
} | ||
|
||
@ExceptionHandler({Exception.class}) | ||
protected ResponseEntity<String> handleException(Exception e, Locale locale) { | ||
return ResponseEntity.badRequest().body("Exception occur inside API " + e); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...main/java/org/sagebionetworks/challenge/configuration/CustomFeignClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.sagebionetworks.challenge.configuration; | ||
|
||
import feign.codec.ErrorDecoder; | ||
import org.springframework.cloud.openfeign.FeignClientProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class CustomFeignClientConfiguration extends FeignClientProperties.FeignClientConfiguration { | ||
|
||
@Bean | ||
public ErrorDecoder errorDecoder() { | ||
return new CustomFeignErrorDecoder(); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
...ce/src/main/java/org/sagebionetworks/challenge/configuration/CustomFeignErrorDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.sagebionetworks.challenge.configuration; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import feign.Response; | ||
import feign.codec.ErrorDecoder; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.io.IOUtils; | ||
import org.sagebionetworks.challenge.exception.SimpleChallengeGlobalException; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@Slf4j | ||
public class CustomFeignErrorDecoder implements ErrorDecoder { | ||
@Override | ||
public Exception decode(String methodKey, Response response) { | ||
|
||
SimpleChallengeGlobalException simpleChallengeGlobalException = | ||
extractChallengeCoreGlobalException(response); | ||
|
||
switch (response.status()) { | ||
case 400: | ||
log.error("Error in request went through feign client {} ", | ||
simpleChallengeGlobalException.getMessage() + " - " | ||
+ simpleChallengeGlobalException.getCode()); | ||
return simpleChallengeGlobalException; | ||
case 401: | ||
log.error("Unauthorized Request Through Feign"); | ||
return new Exception("Unauthorized Request Through Feign"); | ||
case 404: | ||
log.error("Unidentified Request Through Feign"); | ||
return new Exception("Unidentified Request Through Feign"); | ||
default: | ||
log.error("Error in request went through feign client"); | ||
return new Exception("Common Feign Exception"); | ||
} | ||
|
||
} | ||
|
||
private SimpleChallengeGlobalException extractChallengeCoreGlobalException(Response response) { | ||
SimpleChallengeGlobalException exceptionMessage = null; | ||
Reader reader = null; | ||
// capturing error message from response body. | ||
try { | ||
reader = response.body().asReader(StandardCharsets.UTF_8); | ||
String result = IOUtils.toString(reader); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
exceptionMessage = mapper.readValue(result, SimpleChallengeGlobalException.class); | ||
} catch (IOException e) { | ||
log.error("IO Exception on reading exception message feign client" + e); | ||
} finally { | ||
try { | ||
if (reader != null) { | ||
reader.close(); | ||
} | ||
} catch (IOException e) { | ||
log.error("IO Exception on reading exception message feign client" + e); | ||
} | ||
} | ||
return exceptionMessage; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...ice/src/main/java/org/sagebionetworks/challenge/service/rest/ChallengeCoreRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...llenge-util/src/main/java/org/sagebionetworks/challenge/util/exception/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.sagebionetworks.challenge.util.exception; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
public class ErrorResponse { | ||
private String code; | ||
private String message; | ||
} |
20 changes: 20 additions & 0 deletions
20
...ain/java/org/sagebionetworks/challenge/util/exception/SimpleChallengeGlobalException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.sagebionetworks.challenge.util.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class SimpleChallengeGlobalException extends RuntimeException { | ||
|
||
private String code; | ||
private String message; | ||
|
||
public SimpleChallengeGlobalException(String message) { | ||
super(message); | ||
} | ||
} |