-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#25] Make ACA exception handling more descriptive
- Loading branch information
Showing
16 changed files
with
322 additions
and
61 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
45 changes: 45 additions & 0 deletions
45
HIRS_AttestationCA/src/main/java/hirs/attestationca/AcaRestError.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,45 @@ | ||
package hirs.attestationca; | ||
|
||
/** | ||
* A simple POJO that will provide a clean error message to clients making | ||
* REST requests to the ACA. It is to be serialized to JSON for the return message. | ||
*/ | ||
public class AcaRestError { | ||
|
||
private String error; | ||
|
||
/** | ||
* Basic constructor necessary for Jackson JSON serialization to work properly. | ||
*/ | ||
public AcaRestError() { | ||
// Don't remove this constructor as it's required for JSON mapping | ||
} | ||
|
||
/** | ||
* Parameterized constructor for creating this class normally. | ||
* | ||
* @param error the error message to store in this object | ||
*/ | ||
public AcaRestError(final String error) { | ||
this.error = error; | ||
} | ||
|
||
/** | ||
* Simple getter to get the error message stored in this object. | ||
* | ||
* @return the error message | ||
*/ | ||
public String getError() { | ||
return error; | ||
} | ||
|
||
/** | ||
* Simple setter to get the error message stored in this object. | ||
* | ||
* @param error the new error message to store in this object | ||
*/ | ||
public void setError(final String error) { | ||
this.error = error; | ||
} | ||
|
||
} |
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
27 changes: 27 additions & 0 deletions
27
...stationCA/src/main/java/hirs/attestationca/exceptions/CertificateProcessingException.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,27 @@ | ||
package hirs.attestationca.exceptions; | ||
|
||
/** | ||
* Generic exception thrown while a {@link hirs.attestationca.AttestationCertificateAuthority} | ||
* is processing a newly created Attestation Certificate for a validated identity. | ||
*/ | ||
public class CertificateProcessingException extends RuntimeException { | ||
/** | ||
* Constructs a generic instance of this exception using the specified reason. | ||
* | ||
* @param reason for the exception | ||
*/ | ||
public CertificateProcessingException(final String reason) { | ||
super(reason); | ||
} | ||
|
||
/** | ||
* Constructs a instance of this exception with the specified reason and backing root | ||
* exception. | ||
* | ||
* @param reason for this exception | ||
* @param rootException causing this exception | ||
*/ | ||
public CertificateProcessingException(final String reason, final Throwable rootException) { | ||
super(reason, rootException); | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...tationca/IdentityProcessingException.java → ...ceptions/IdentityProcessingException.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
27 changes: 27 additions & 0 deletions
27
..._AttestationCA/src/main/java/hirs/attestationca/exceptions/UnexpectedServerException.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,27 @@ | ||
package hirs.attestationca.exceptions; | ||
|
||
/** | ||
* Generic exception thrown when a {@link hirs.attestationca.AttestationCertificateAuthority} | ||
* encounters an unexpected condition that can't be handled. | ||
*/ | ||
public class UnexpectedServerException extends RuntimeException { | ||
/** | ||
* Constructs a generic instance of this exception using the specified reason. | ||
* | ||
* @param reason for the exception | ||
*/ | ||
public UnexpectedServerException(final String reason) { | ||
super(reason); | ||
} | ||
|
||
/** | ||
* Constructs a instance of this exception with the specified reason and backing root | ||
* exception. | ||
* | ||
* @param reason for this exception | ||
* @param rootException causing this exception | ||
*/ | ||
public UnexpectedServerException(final String reason, final Throwable rootException) { | ||
super(reason, rootException); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
HIRS_AttestationCA/src/main/java/hirs/attestationca/exceptions/package-info.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,4 @@ | ||
/** | ||
* Custom exceptions of the {@link hirs.attestationca.AttestationCertificateAuthority}. | ||
*/ | ||
package hirs.attestationca.exceptions; |
72 changes: 72 additions & 0 deletions
72
...rc/main/java/hirs/attestationca/rest/AttestationCertificateAuthorityExceptionHandler.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,72 @@ | ||
package hirs.attestationca.rest; | ||
|
||
import hirs.attestationca.AcaRestError; | ||
import hirs.attestationca.exceptions.CertificateProcessingException; | ||
import hirs.attestationca.exceptions.IdentityProcessingException; | ||
import hirs.attestationca.exceptions.UnexpectedServerException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
/** | ||
* Handle processing of exceptions for ACA REST API. | ||
*/ | ||
@ControllerAdvice | ||
public class AttestationCertificateAuthorityExceptionHandler | ||
extends ResponseEntityExceptionHandler { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger( | ||
AttestationCertificateAuthorityExceptionHandler.class); | ||
|
||
/** | ||
* Method to handle errors of the type {@link CertificateProcessingException}, | ||
* {@link IdentityProcessingException}, and {@link IllegalArgumentException} | ||
* that are thrown when performing a RESTful operation. | ||
* | ||
* @param ex exception that was thrown | ||
* @param request the web request that started the RESTful operation | ||
* @return the response entity that will form the message returned to the client | ||
*/ | ||
@ExceptionHandler({ CertificateProcessingException.class, IdentityProcessingException.class, | ||
IllegalArgumentException.class }) | ||
public final ResponseEntity<Object> handleExpectedExceptions(final Exception ex, | ||
final WebRequest request) { | ||
LOGGER.error(String.format("The ACA has encountered an expected exception: %s", | ||
ex.getMessage()), ex); | ||
return handleGeneralException(ex, HttpStatus.BAD_REQUEST, request); | ||
} | ||
|
||
/** | ||
* Method to handle errors of the type {@link IllegalStateException} and | ||
* {@link UnexpectedServerException} that are thrown when performing a RESTful operation. | ||
* | ||
* @param ex exception that was thrown | ||
* @param request the web request that started the RESTful operation | ||
* @return the response entity that will form the message returned to the client | ||
*/ | ||
@ExceptionHandler({ IllegalStateException.class, UnexpectedServerException.class }) | ||
public final ResponseEntity<Object> handleUnexpectedExceptions(final Exception ex, | ||
final WebRequest request) { | ||
LOGGER.error(String.format("The ACA has encountered an unexpected exception: %s", | ||
ex.getMessage()), ex); | ||
return handleGeneralException(ex, HttpStatus.INTERNAL_SERVER_ERROR, request); | ||
} | ||
|
||
private ResponseEntity<Object> handleGeneralException(final Exception ex, | ||
final HttpStatus responseStatus, | ||
final WebRequest request) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
|
||
return handleExceptionInternal(ex, new AcaRestError(ex.getMessage()), | ||
headers, responseStatus, request); | ||
} | ||
|
||
} |
Oops, something went wrong.