Added Controller level Exception handling
This commit is contained in:
parent
e324211595
commit
f031a79718
|
@ -6,6 +6,7 @@ import eu.dnetlib.validator2.validation.guideline.openaire.*;
|
|||
import eu.dnetlib.validatorapi.entities.RuleInfo;
|
||||
import eu.dnetlib.validatorapi.entities.ValidationJob;
|
||||
import eu.dnetlib.validatorapi.entities.ValidationRuleResult;
|
||||
import eu.dnetlib.validatorapi.exceptions.ValidationException;
|
||||
import eu.dnetlib.validatorapi.repositories.ValidationIssueRepository;
|
||||
import eu.dnetlib.validatorapi.repositories.ValidationJobRepository;
|
||||
import eu.dnetlib.validatorapi.repositories.ValidationResultRepository;
|
||||
|
@ -14,10 +15,12 @@ import eu.dnetlib.validatorapi.routes.SimpleOaiPmhRoute;
|
|||
import org.apache.camel.CamelContext;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
@ -131,9 +134,11 @@ public class ValidationController {
|
|||
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Validation job stopped unexpectedly." + e.getMessage());
|
||||
System.out.println("ERROR " + e.getMessage());
|
||||
log.error("Validation job stopped unexpectedly. " + e.getMessage());
|
||||
validationJob.progress = "STOPPED";
|
||||
validationJob.exceptionClass = e.getClass().getCanonicalName();
|
||||
validationJob.exceptionMessage = e.getMessage();
|
||||
|
||||
|
||||
} finally {
|
||||
validationJob.endDate = new Date();
|
||||
|
@ -179,10 +184,10 @@ public class ValidationController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = {"/realValidator"}, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public ValidationJob validateWithApacheCamel(@RequestParam(name = "guidelines") String guidelinesProfileName,
|
||||
@RequestParam(name = "baseUrl", defaultValue = "http://repositorium.sdum.uminho.pt/oai/request") String baseURL, //not in use now
|
||||
@RequestParam(name="numberOfRecords", defaultValue = "10") int numberOfRecords,
|
||||
@RequestParam(name="set", required = false) Optional<String> set
|
||||
public ResponseEntity<ValidationJob> validateWithApacheCamel(@RequestParam(name = "guidelines") String guidelinesProfileName,
|
||||
@RequestParam(name = "baseUrl", defaultValue = "http://repositorium.sdum.uminho.pt/oai/request") String baseURL, //not in use now
|
||||
@RequestParam(name="numberOfRecords", defaultValue = "10") int numberOfRecords,
|
||||
@RequestParam(name="set", required = false) Optional<String> set
|
||||
) {
|
||||
|
||||
AbstractOpenAireProfile profile = initializeOpenAireProfile(guidelinesProfileName);
|
||||
|
@ -191,7 +196,8 @@ public class ValidationController {
|
|||
|
||||
if (profile == null && fairProfile == null) {
|
||||
log.error("Exception: No valid guidelines " + guidelinesProfileName + ".");
|
||||
new Exception("Validation Job stopped unexpectedly. No valid guidelines " +
|
||||
|
||||
throw new ValidationException("Validation Job stopped unexpectedly. No valid guidelines " +
|
||||
guidelinesProfileName + " were provided.");
|
||||
}
|
||||
|
||||
|
@ -228,18 +234,19 @@ public class ValidationController {
|
|||
|
||||
camelContext.addRoutes(oaiPmhRouteBuilder);
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Validation job stopped unexpectedly." + e.getMessage());
|
||||
System.out.println("ERROR " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("Validation job stopped unexpectedly. " + e.getMessage());
|
||||
validationJob.progress = "STOPPED";
|
||||
validationJob.endDate = new Date();
|
||||
validationJob.score = resultSum / validationJob.recordsTested;
|
||||
validationJob.status = validationResultRepository.getStatus(validationJob.id);
|
||||
validationJob.exceptionClass = e.getClass().getCanonicalName();
|
||||
validationJob.exceptionMessage = e.getMessage();
|
||||
validationJobRepository.save(validationJob);
|
||||
return ResponseEntity.status(HttpStatus.SC_UNPROCESSABLE_ENTITY).body(validationJob);
|
||||
}
|
||||
|
||||
return validationJob;
|
||||
return ResponseEntity.ok(validationJob);
|
||||
}
|
||||
|
||||
@RequestMapping(value = {"/getSets"}, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
|
@ -492,8 +499,10 @@ public class ValidationController {
|
|||
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Validation job stopped unexpectedly." + e.getMessage());
|
||||
log.error("Validation job stopped unexpectedly. " + e.getMessage());
|
||||
validationJob.progress = "STOPPED";
|
||||
validationJob.exceptionClass = e.getClass().getCanonicalName();
|
||||
validationJob.exceptionMessage = e.getMessage();
|
||||
|
||||
} finally {
|
||||
validationJob.endDate = new Date();
|
||||
|
@ -552,7 +561,6 @@ public class ValidationController {
|
|||
|
||||
//String response = camelContext.createProducerTemplate().requestBody("direct:oaipmhRequest", endpoint, String.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -37,6 +37,13 @@ public class ValidationJob {
|
|||
@Column(name="score")
|
||||
public double score;
|
||||
|
||||
@Column(name="exception_class")
|
||||
public String exceptionClass;
|
||||
|
||||
@Column(name="exception_message")
|
||||
public String exceptionMessage;
|
||||
|
||||
|
||||
public ValidationJob(){
|
||||
startDate = new Date();
|
||||
}
|
||||
|
@ -61,6 +68,8 @@ public class ValidationJob {
|
|||
", progress='" + progress + '\'' +
|
||||
", status='" + status + '\'' +
|
||||
", score=" + score +
|
||||
", exceptionClass='" + exceptionClass + '\'' +
|
||||
", exceptionMessage='" + exceptionMessage + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
|
|
@ -1,2 +1,17 @@
|
|||
package eu.dnetlib.validatorapi.exceptions;public class ErrorMessage {
|
||||
package eu.dnetlib.validatorapi.exceptions;
|
||||
|
||||
public class ErrorMessage {
|
||||
private String message;
|
||||
|
||||
ErrorMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,2 +1,18 @@
|
|||
package eu.dnetlib.validatorapi.exceptions;public class ExceptionHandler {
|
||||
package eu.dnetlib.validatorapi.exceptions;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
@ControllerAdvice
|
||||
public class ExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ResponseBody
|
||||
@org.springframework.web.bind.annotation.ExceptionHandler(value = {ValidationException.class})
|
||||
protected ResponseEntity<ErrorMessage> handleValidationError(RuntimeException exception, WebRequest request) {
|
||||
return new ResponseEntity(new ErrorMessage(exception.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,2 +1,11 @@
|
|||
package eu.dnetlib.validatorapi.exceptions;public class ValidationException {
|
||||
package eu.dnetlib.validatorapi.exceptions;
|
||||
|
||||
public class ValidationException extends RuntimeException {
|
||||
public ValidationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ValidationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue