-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from jb-adams/fix/restore-error-messages
override custom exception handling from commons lib for DRS-specific fields
- Loading branch information
Showing
4 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
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
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
33 changes: 33 additions & 0 deletions
33
src/main/java/org/ga4gh/starterkit/drs/exception/DrsCustomExceptionHandling.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,33 @@ | ||
package org.ga4gh.starterkit.drs.exception; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.ga4gh.starterkit.common.constant.DateTimeConstants; | ||
import org.ga4gh.starterkit.common.exception.CustomException; | ||
import org.ga4gh.starterkit.common.util.webserver.CustomExceptionHandling; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
public class DrsCustomExceptionHandling extends CustomExceptionHandling { | ||
|
||
@ExceptionHandler(CustomException.class) | ||
public ResponseEntity<Object> handleCustomExceptions(CustomException err) { | ||
HttpStatus httpStatus = getCustomHttpStatus(err); | ||
return yieldResponseEntity(httpStatus, err); | ||
} | ||
|
||
private HttpStatus getCustomHttpStatus(CustomException err) { | ||
return err.getClass().getAnnotation(ResponseStatus.class).value(); | ||
} | ||
|
||
private ResponseEntity<Object> yieldResponseEntity(HttpStatus httpStatus, Exception ex) { | ||
DrsCustomExceptionResponse response = new DrsCustomExceptionResponse(); | ||
response.setTimestamp(LocalDateTime.now().format(DateTimeConstants.DATE_FORMATTER)); | ||
response.setStatusCode(httpStatus.value()); | ||
response.setError(httpStatus.getReasonPhrase()); | ||
response.setMsg(ex.getMessage()); | ||
return new ResponseEntity<>(response, httpStatus); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/ga4gh/starterkit/drs/exception/DrsCustomExceptionResponse.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,21 @@ | ||
package org.ga4gh.starterkit.drs.exception; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import org.ga4gh.starterkit.common.exception.CustomExceptionResponse; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public class DrsCustomExceptionResponse extends CustomExceptionResponse { | ||
|
||
private String msg; | ||
|
||
public void setMsg(String msg) { | ||
this.msg = msg; | ||
} | ||
|
||
public String getMsg() { | ||
return msg; | ||
} | ||
} |