-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update exception handler, enable extensive logging in integration tests
- Loading branch information
GordeaS
authored and
GordeaS
committed
Mar 18, 2024
1 parent
91094db
commit a6f54ba
Showing
8 changed files
with
122 additions
and
11 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
9 changes: 9 additions & 0 deletions
9
set-web/src/main/java/eu/europeana/set/web/config/BeanNames.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,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"; | ||
|
||
|
||
} |
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
18 changes: 18 additions & 0 deletions
18
set-web/src/main/java/eu/europeana/set/web/config/UserSetAutoConfig.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,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; | ||
// } | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
set-web/src/main/java/eu/europeana/set/web/service/RequestPathMethodService.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 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); | ||
} | ||
} |
52 changes: 46 additions & 6 deletions
52
...c/main/java/eu/europeana/set/web/service/controller/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 |
---|---|---|
@@ -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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...c/main/java/eu/europeana/set/web/service/controller/exception/UserSetErrorController.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,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()); | ||
} | ||
|
||
} |