Skip to content

Commit

Permalink
Merge pull request #40 from jb-adams/fix/restore-error-messages
Browse files Browse the repository at this point in the history
override custom exception handling from commons lib for DRS-specific fields
  • Loading branch information
Jeremy Adams authored Jul 8, 2021
2 parents 1b86634 + dc8dd30 commit 216a6ff
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ plugins {

archivesBaseName = 'ga4gh-starter-kit-drs'
group 'org.ga4gh'
version '0.1.8'
version '0.1.9'

repositories {
// Use jcenter for resolving dependencies.
Expand All @@ -53,7 +53,7 @@ dependencies {
implementation 'javax.xml.bind:jaxb-api:2.2.8'
implementation 'org.springdoc:springdoc-openapi-ui:1.2.33'
implementation 'org.xerial:sqlite-jdbc:3.8.11.2'
implementation 'org.ga4gh:ga4gh-starter-kit-common:0.5.2'
implementation 'org.ga4gh:ga4gh-starter-kit-common:0.5.3'

testImplementation 'org.testng:testng:7.0.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.ga4gh.starterkit.common.config.DatabaseProps;
import org.ga4gh.starterkit.common.config.ServerProps;
import org.ga4gh.starterkit.drs.config.DrsServiceProps;
import org.ga4gh.starterkit.drs.exception.DrsCustomExceptionHandling;
import org.ga4gh.starterkit.drs.model.AwsS3AccessObject;
import org.ga4gh.starterkit.drs.model.Checksum;
import org.ga4gh.starterkit.drs.model.DrsObject;
Expand Down Expand Up @@ -69,6 +70,11 @@ public WebServerFactoryCustomizer servletContainer() {
public FilterRegistrationBean<AdminEndpointsFilter> adminEndpointsFilter() {
return new FilterRegistrationBean<AdminEndpointsFilter>(new AdminEndpointsFilter(Integer.valueOf(serverAdminPort)));
}

@Bean
public DrsCustomExceptionHandling customExceptionHandling() {
return new DrsCustomExceptionHandling();
}

@Bean
public FilterRegistrationBean<CorsFilter> corsFilter(
Expand Down
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);
}
}
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;
}
}

0 comments on commit 216a6ff

Please sign in to comment.