Skip to content

Commit

Permalink
update exception handler, enable extensive logging in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GordeaS authored and GordeaS committed Mar 18, 2024
1 parent 91094db commit a6f54ba
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

public interface UserSetConfiguration {

public static final String BEAN_SET_MONGO_STORE = "set_db_morphia_datastore_set";
public static final String BEAN_SET_PERSITENCE_SERVICE = "set_db_setService";


public String getComponentName();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ springdoc.show-actuator=true
## server configurations
server.port = 8080
server.error.include-message=always
server.error.include-stacktrace=on_param
server.error.include-exception=false
server.error.include-stacktrace= always
server.error.include-exception=true
server.error.see-also=https://pro.europeana.eu/page/apis
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package eu.europeana.set.web.config;

public abstract class BeanNames {
public static final String BEAN_SET_MONGO_STORE = "set_db_morphia_datastore_set";
public static final String BEAN_SET_PERSITENCE_SERVICE = "set_db_setService";
public static final String BEAN_I18N_SERVICE = "i18nService";


}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MongoConfig {

private ApiMongoConnector mongoConnector;

@Bean(UserSetConfiguration.BEAN_SET_MONGO_STORE)
@Bean(BeanNames.BEAN_SET_MONGO_STORE)
public Datastore createDataStore() {
Datastore ds = getMongoConnector().createDatastore(mongoConnectionUrl, mongoTrustStore, mongoTrustStorePass, -1, MODEL_PACKAGES );
//Ensures consistency when Mongo is deployed in a replica-set
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package eu.europeana.set.web.config;

import org.springframework.context.annotation.Configuration;

@Configuration()
public class UserSetAutoConfig{

// @Bean("messageSource")
// public MessageSource getMessageSource() {
// ReloadableResourceBundleMessageSource messageSource =
// new ReloadableResourceBundleMessageSource();
// messageSource.setBasename("classpath:messages");
// messageSource.setDefaultEncoding("utf-8");
// messageSource.setDefaultLocale(Locale.ENGLISH);
// return messageSource;
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package eu.europeana.set.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;
import eu.europeana.api.commons.web.service.AbstractRequestPathMethodService;

/** This service is used to populate the Allow header in API responses. */
@Service
public class RequestPathMethodService extends AbstractRequestPathMethodService {

@Autowired
public RequestPathMethodService(WebApplicationContext applicationContext) {
super(applicationContext);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
package eu.europeana.set.web.service.controller.exception;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;

import org.springframework.web.bind.annotation.ExceptionHandler;
import eu.europeana.api.commons.config.i18n.I18nService;
import eu.europeana.api.commons.web.controller.exception.AbstractExceptionHandlingController;
import eu.europeana.api.commons.error.EuropeanaApiErrorResponse;
import eu.europeana.api.commons.web.exception.EuropeanaGlobalExceptionHandler;
import eu.europeana.set.web.config.BeanNames;
import eu.europeana.set.web.service.RequestPathMethodService;

@ControllerAdvice
public class GlobalExceptionHandler extends AbstractExceptionHandlingController {
public class GlobalExceptionHandler extends EuropeanaGlobalExceptionHandler {

@Resource
I18nService i18nService;

protected I18nService getI18nService() {
return i18nService;
}


/**
* Constructor for the initialization of the Exception handler
* @param requestPathMethodService builtin service for path method mapping
* @param i18nService the internationalization service
*/
@Autowired
public GlobalExceptionHandler(RequestPathMethodService requestPathMethodService,
@Qualifier(BeanNames.BEAN_I18N_SERVICE) I18nService i18nService) {
this.requestPathMethodService = requestPathMethodService;
this.i18nService = i18nService;
}

/**
* HttpMessageNotReadableException thrown when the request body is not parsable to the declared input of the handler method
* @param e the exception indicating the request message parsing error
* @param httpRequest the request object
* @return the api response
*/
@ExceptionHandler
public ResponseEntity<EuropeanaApiErrorResponse> handleRequestBodyNotParsableError(HttpMessageNotReadableException e, HttpServletRequest httpRequest) {
HttpStatus responseStatus = HttpStatus.BAD_REQUEST;
EuropeanaApiErrorResponse response = (new EuropeanaApiErrorResponse.Builder(httpRequest, e, stackTraceEnabled()))
.setStatus(responseStatus.value())
.setError(responseStatus.getReasonPhrase())
.setMessage("Invalid request body: " + e.getMessage())
.setSeeAlso(getSeeAlso())
.build();

return ResponseEntity
.status(responseStatus)
.headers(createHttpHeaders(httpRequest))
.body(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package eu.europeana.set.web.service.controller.exception;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import eu.europeana.api.commons.web.http.HttpHeaders;

/**
* Created by luthien on 2019-08-13.
*/
@RestController
public class UserSetErrorController extends AbstractErrorController {

public UserSetErrorController(ErrorAttributes errorAttributes) {
super(errorAttributes);
}


@GetMapping(value = "/error", produces = {HttpHeaders.CONTENT_TYPE_JSON_UTF8, HttpHeaders.CONTENT_TYPE_JSONLD})
@ResponseBody
public Map<String, Object> error(final HttpServletRequest request) {
return this.getErrorAttributes(request, ErrorAttributeOptions.defaults());
}

}

0 comments on commit a6f54ba

Please sign in to comment.