diff options
author | liamfallon <liam.fallon@est.tech> | 2019-03-26 09:26:00 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-03-26 09:26:00 +0000 |
commit | 35c6f2df3df2de7f54f717c2167f7b170494cdc9 (patch) | |
tree | da0710bb9dec90881ab5e4b084595af3cbc9d1d0 /models-base | |
parent | b6649e710c6aad05b6cbb64fa61d6aa0ad82f12a (diff) |
Add ErrorResponse to policy framework exceptions
The ErrorResponse object is now contained in Policy Framework
exceptions.
Issue-ID: POLICY-1095
Change-Id: Ib0ce6cdbbead939afefc4afa3f507eb1a28c4a5c
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-base')
6 files changed, 41 insertions, 139 deletions
diff --git a/models-base/pom.xml b/models-base/pom.xml index 712bc9108..0523c3324 100644 --- a/models-base/pom.xml +++ b/models-base/pom.xml @@ -17,8 +17,7 @@ SPDX-License-Identifier: Apache-2.0 ============LICENSE_END========================================================= --> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> @@ -30,4 +29,12 @@ <artifactId>policy-models-base</artifactId> <name>${project.artifactId}</name> <description>[${project.parent.artifactId}] module provides basic model handling for the ONAP Policy Framework</description> + + <dependencies> + <dependency> + <groupId>org.onap.policy.models</groupId> + <artifactId>policy-models-errors</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> </project> diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModelException.java b/models-base/src/main/java/org/onap/policy/models/base/PfModelException.java index ce44e51f3..46c5bd311 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfModelException.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfModelException.java @@ -25,18 +25,20 @@ import javax.ws.rs.core.Response; import lombok.Getter; import lombok.ToString; -import org.apache.commons.lang3.exception.ExceptionUtils; +import org.onap.policy.models.errors.concepts.ErrorResponse; +import org.onap.policy.models.errors.concepts.ErrorResponseInfo; +import org.onap.policy.models.errors.concepts.ErrorResponseUtils; /** * This class is a base exception from which all model exceptions are sub classes. */ @Getter @ToString -public class PfModelException extends Exception implements PfModelExceptionInfo { +public class PfModelException extends Exception implements ErrorResponseInfo { private static final long serialVersionUID = -8507246953751956974L; - // The status code on the exception - private final Response.Status statusCode; + // The error response of the exception + private final ErrorResponse errorResponse = new ErrorResponse(); // The object on which the exception was thrown private final transient Object object; @@ -60,7 +62,8 @@ public class PfModelException extends Exception implements PfModelExceptionInfo */ public PfModelException(final Response.Status statusCode, final String message, final Object object) { super(message); - this.statusCode = statusCode; + errorResponse.setResponseCode(statusCode); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); this.object = object; } @@ -86,45 +89,8 @@ public class PfModelException extends Exception implements PfModelExceptionInfo public PfModelException(final Response.Status statusCode, final String message, final Exception exception, final Object object) { super(message, exception); - this.statusCode = statusCode; + errorResponse.setResponseCode(statusCode); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); this.object = object; } - - /** - * Get the message from this exception and its causes. - * - * @return the cascaded messages from this exception and the exceptions that caused it - */ - @Override - public String getCascadedMessage() { - return buildCascadedMessage(this); - } - - /** - * Build a cascaded message from an exception and all its nested exceptions. - * - * @param throwable the top level exception - * @return cascaded message string - */ - public static String buildCascadedMessage(Throwable throwable) { - final StringBuilder builder = new StringBuilder(); - builder.append(throwable.getMessage()); - - for (Throwable t = throwable; t != null; t = t.getCause()) { - builder.append("\ncaused by: "); - builder.append(t.getMessage()); - } - - return builder.toString(); - } - - /** - * Get the stack trace of the exception as a string. - * - * @return the stack trace of this message as a string - */ - @Override - public String getStackTraceAsString() { - return ExceptionUtils.getStackTrace(this); - } } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModelExceptionInfo.java b/models-base/src/main/java/org/onap/policy/models/base/PfModelExceptionInfo.java deleted file mode 100644 index 2fe244cec..000000000 --- a/models-base/src/main/java/org/onap/policy/models/base/PfModelExceptionInfo.java +++ /dev/null @@ -1,59 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.base; - -import javax.ws.rs.core.Response; - -/** - * Interface implemented bu Policy framework model exceptions to allow uniform reading of status codes and cascaded - * messages. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public interface PfModelExceptionInfo { - - /** - * Get the status code associated with an exception. - * @return the status code - */ - public Response.Status getStatusCode(); - - /** - * Get the messages for all the cascaded exceptions in an exception. - * - * @return the cascaded message - */ - public String getCascadedMessage(); - - /** - * Get the object associated with an exception. - * - * @return the object associated with an exception - */ - public Object getObject(); - - /** - * Get the stack trace of the exception as a string. - * - * @return the stack trace of this message as a string - */ - public String getStackTraceAsString(); -} diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java b/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java index 32855c2a4..472412865 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java @@ -25,18 +25,20 @@ import javax.ws.rs.core.Response; import lombok.Getter; import lombok.ToString; -import org.apache.commons.lang3.exception.ExceptionUtils; +import org.onap.policy.models.errors.concepts.ErrorResponse; +import org.onap.policy.models.errors.concepts.ErrorResponseInfo; +import org.onap.policy.models.errors.concepts.ErrorResponseUtils; /** * This class is a base model run time exception from which all model run time exceptions are sub classes. */ @Getter @ToString -public class PfModelRuntimeException extends RuntimeException implements PfModelExceptionInfo { +public class PfModelRuntimeException extends RuntimeException implements ErrorResponseInfo { private static final long serialVersionUID = -8507246953751956974L; - // The return code on the exception - private final Response.Status statusCode; + // The error response of the exception + private final ErrorResponse errorResponse = new ErrorResponse(); // The object on which the exception was thrown private final transient Object object; @@ -61,7 +63,8 @@ public class PfModelRuntimeException extends RuntimeException implements PfModel public PfModelRuntimeException(final Response.Status statusCode, final String message, final Object object) { super(message); this.object = object; - this.statusCode = statusCode; + errorResponse.setResponseCode(statusCode); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); } /** @@ -87,26 +90,7 @@ public class PfModelRuntimeException extends RuntimeException implements PfModel final Object object) { super(message, exception); this.object = object; - this.statusCode = statusCode; - } - - /** - * Get the message from this exception and its causes. - * - * @return the message of this exception and all the exceptions that caused this exception - */ - @Override - public String getCascadedMessage() { - return PfModelException.buildCascadedMessage(this); - } - - /** - * Get the stack trace of the exception as a string. - * - * @return the stack trace of this message as a string - */ - @Override - public String getStackTraceAsString() { - return ExceptionUtils.getStackTrace(this); + errorResponse.setResponseCode(statusCode); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java b/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java index 0a5b6a0a6..664e3ddbc 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java @@ -28,6 +28,7 @@ import java.io.IOException; import javax.ws.rs.core.Response; import org.junit.Test; +import org.onap.policy.models.errors.concepts.ErrorResponse; public class ExceptionsTest { @@ -41,7 +42,8 @@ public class ExceptionsTest { String key = "A String"; PfModelException ae = new PfModelException(Response.Status.OK, "Message", new IOException("IO exception message"), key); - assertEquals("Message\ncaused by: Message\ncaused by: IO exception message", ae.getCascadedMessage()); + ErrorResponse errorResponse = ae.getErrorResponse(); + assertEquals("Message\nIO exception message", String.join("\n", errorResponse.getErrorDetails())); assertEquals(key, ae.getObject()); assertNotNull(new PfModelRuntimeException(Response.Status.OK, "Message")); @@ -52,8 +54,9 @@ public class ExceptionsTest { String rkey = "A String"; PfModelRuntimeException re = new PfModelRuntimeException(Response.Status.OK, "Runtime Message", new IOException("IO runtime exception message"), rkey); - assertEquals("Runtime Message\ncaused by: Runtime Message\ncaused by: IO runtime exception message", - re.getCascadedMessage()); + errorResponse = re.getErrorResponse(); + assertEquals("Runtime Message\nIO runtime exception message", + String.join("\n", errorResponse.getErrorDetails())); assertEquals(key, re.getObject()); } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfModelExceptionInfoTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfModelExceptionInfoTest.java index 1257975ad..183b44c13 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfModelExceptionInfoTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfModelExceptionInfoTest.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertEquals; import javax.ws.rs.core.Response; import org.junit.Test; +import org.onap.policy.models.errors.concepts.ErrorResponseInfo; /** * Test PfModelExceptionInfo interface. @@ -49,15 +50,15 @@ public class PfModelExceptionInfoTest { } } - private String getErrorMessage(final PfModelExceptionInfo pfme) { + private String getErrorMessage(final ErrorResponseInfo eri) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Server returned: "); - stringBuilder.append(pfme.getStatusCode().toString()); + stringBuilder.append(eri.getErrorResponse().getResponseCode().toString()); + stringBuilder.append("Error Message:\n"); + stringBuilder.append(eri.getErrorResponse().getErrorMessage()); stringBuilder.append("\nDetailed Message:\n"); - stringBuilder.append(pfme.getCascadedMessage()); - stringBuilder.append("\nStack Trace:\n"); - stringBuilder.append(pfme.getStackTraceAsString()); + stringBuilder.append(String.join("\n", eri.getErrorResponse().getErrorDetails())); return stringBuilder.toString(); } |