diff options
Diffstat (limited to 'models-errors')
5 files changed, 149 insertions, 2 deletions
diff --git a/models-errors/pom.xml b/models-errors/pom.xml index ab998537e..4e297868e 100644 --- a/models-errors/pom.xml +++ b/models-errors/pom.xml @@ -27,7 +27,7 @@ <version>2.0.0-SNAPSHOT</version> </parent> - <artifactId>models-errors</artifactId> + <artifactId>policy-models-errors</artifactId> <name>${project.artifactId}</name> <description>The models for Policy API's to return Error/warning message details.</description> diff --git a/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponse.java b/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponse.java index b072ba1f6..88960f8ae 100644 --- a/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponse.java +++ b/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponse.java @@ -22,6 +22,7 @@ package org.onap.policy.models.errors.concepts; import com.google.gson.annotations.SerializedName; +import java.io.Serializable; import java.util.List; import javax.ws.rs.core.Response; @@ -36,7 +37,8 @@ import lombok.Data; * */ @Data -public class ErrorResponse { +public class ErrorResponse implements Serializable { + private static final long serialVersionUID = 6760066094588944729L; @SerializedName("code") private Response.Status responseCode; diff --git a/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponseInfo.java b/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponseInfo.java new file mode 100644 index 000000000..ed7104b04 --- /dev/null +++ b/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponseInfo.java @@ -0,0 +1,36 @@ +/*- + * ============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.errors.concepts; + +/** + * Interface implemented by Policy framework model exceptions to allow uniform reading of error responses. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public interface ErrorResponseInfo { + + /** + * Get the error response. + * + * @return the error response + */ + public ErrorResponse getErrorResponse(); +} diff --git a/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponseUtils.java b/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponseUtils.java new file mode 100644 index 000000000..6346f9a90 --- /dev/null +++ b/models-errors/src/main/java/org/onap/policy/models/errors/concepts/ErrorResponseUtils.java @@ -0,0 +1,56 @@ +/*- + * ============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.errors.concepts; + +import java.util.ArrayList; +import java.util.List; + +/** + * Utility class for managing {@link ErrorResponse objects} + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public final class ErrorResponseUtils { + /** + * Private constructor used to prevent sub class instantiation. + */ + private ErrorResponseUtils() {} + + /** + * Store the cascaded messages from an exception and all its nested exceptions in an ErrorResponse object. + * + * @param throwable the top level exception + */ + public static void getExceptionMessages(final ErrorResponse errorResponse, final Throwable throwable) { + errorResponse.setErrorMessage(throwable.getMessage()); + + List<String> cascascadedErrorMessages = new ArrayList<>(); + + for (Throwable t = throwable; t != null; t = t.getCause()) { + cascascadedErrorMessages.add(t.getMessage()); + } + + if (!cascascadedErrorMessages.isEmpty()) { + errorResponse.setErrorDetails(cascascadedErrorMessages); + } + } +} + diff --git a/models-errors/src/test/java/org/onap/policy/models/errors/concepts/ErrorResponseUtilsTest.java b/models-errors/src/test/java/org/onap/policy/models/errors/concepts/ErrorResponseUtilsTest.java new file mode 100644 index 000000000..4f4ef395a --- /dev/null +++ b/models-errors/src/test/java/org/onap/policy/models/errors/concepts/ErrorResponseUtilsTest.java @@ -0,0 +1,53 @@ +/*- + * ============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.errors.concepts; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; + +/** + * Test the {@link ErrorResponseUtils} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ErrorResponseUtilsTest { + @Test + public void testErrorResponseUtils() { + try { + try { + throw new NumberFormatException("Exception 0"); + } + catch (Exception nfe) { + throw new IOException("Exception 1", nfe); + } + } catch (Exception ioe) { + ErrorResponse errorResponse = new ErrorResponse(); + ErrorResponseUtils.getExceptionMessages(errorResponse, ioe); + + assertEquals("Exception 1", errorResponse.getErrorMessage()); + assertEquals("Exception 1", errorResponse.getErrorDetails().get(0)); + assertEquals("Exception 0", errorResponse.getErrorDetails().get(1)); + } + } +} |