diff options
author | Pamela Dragosh <pdragosh@research.att.com> | 2019-03-26 17:08:31 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-03-26 17:08:31 +0000 |
commit | a2550a75f9308ec2b00f93523373f079057701fe (patch) | |
tree | f2eb8643fc1b8817b9673297913cb90e999d3bd6 /models-errors/src | |
parent | 2c5a396554a34cddc2f43d98c72665d5b1496f85 (diff) | |
parent | 35c6f2df3df2de7f54f717c2167f7b170494cdc9 (diff) |
Merge "Add ErrorResponse to policy framework exceptions"
Diffstat (limited to 'models-errors/src')
4 files changed, 148 insertions, 1 deletions
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)); + } + } +} |