2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.common.errors;
10 import static java.util.Objects.requireNonNull;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.ErrorTag;
17 import org.opendaylight.yangtools.yang.common.ErrorType;
18 import org.opendaylight.yangtools.yang.common.RpcError;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 * Unchecked exception to communicate error information, as defined
24 * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-3.9">"errors" YANG Data Template</a>.
27 * @author Thomas Pantelis
30 public class RestconfDocumentedException extends RuntimeException {
32 private static final long serialVersionUID = 3L;
34 private final List<RestconfError> errors;
36 // FIXME: this field should be non-null
37 private final transient @Nullable EffectiveModelContext modelContext;
40 * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
41 * to OPERATION_FAILED.
44 * A string which provides a plain text string describing the error.
46 public RestconfDocumentedException(final String message) {
47 this(message, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED);
51 * Constructs an instance with an error message, error type, error tag and exception cause.
53 * @param message A string which provides a plain text string describing the error.
54 * @param errorType The enumerated type indicating the layer where the error occurred.
55 * @param errorTag The enumerated tag representing a more specific error cause.
56 * @param cause The underlying exception cause.
58 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
59 final Throwable cause) {
60 this(cause, new RestconfError(errorType, errorTag, message, null, cause.getMessage(), null));
64 * Constructs an instance with an error message, error type, and error tag.
66 * @param message A string which provides a plain text string describing the error.
67 * @param errorType The enumerated type indicating the layer where the error occurred.
68 * @param errorTag The enumerated tag representing a more specific error cause.
70 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
71 this(new RestconfError(errorType, errorTag, message));
75 * Constructs an instance with an error message, error type, error tag and error path.
77 * @param message A string which provides a plain text string describing the error.
78 * @param errorType The enumerated type indicating the layer where the error occurred.
79 * @param errorTag The enumerated tag representing a more specific error cause.
80 * @param errorPath The instance identifier representing error path
82 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
83 final YangInstanceIdentifier errorPath) {
84 this(new RestconfError(errorType, errorTag, message, errorPath));
88 * Constructs an instance with an error message and exception cause.
89 * The underlying exception is included in the error-info.
91 * @param message A string which provides a plain text string describing the error.
92 * @param cause The underlying exception cause.
94 public RestconfDocumentedException(final String message, final Throwable cause) {
95 this(cause, new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, message, null,
96 cause.getMessage(), null));
100 * Constructs an instance with the given error.
102 public RestconfDocumentedException(final RestconfError error) {
107 * Constructs an instance with the given errors.
109 public RestconfDocumentedException(final String message, final Throwable cause, final List<RestconfError> errors) {
110 // FIXME: We override getMessage so supplied message is lost for any public access
111 // this was lost also in original code.
113 if (errors.isEmpty()) {
114 this.errors = List.of(new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, message));
116 this.errors = List.copyOf(errors);
123 * Constructs an instance with the given RpcErrors.
125 public RestconfDocumentedException(final String message, final Throwable cause,
126 final Collection<? extends RpcError> rpcErrors) {
127 this(message, cause, convertToRestconfErrors(rpcErrors));
130 private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
131 if (rpcErrors == null || rpcErrors.isEmpty()) {
135 final var errorList = new ArrayList<RestconfError>();
136 for (var rpcError : rpcErrors) {
137 errorList.add(new RestconfError(rpcError));
142 public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
144 errors = List.of(error);
148 public RestconfDocumentedException(final Throwable cause, final RestconfError error,
149 final EffectiveModelContext modelContext) {
151 errors = List.of(error);
152 this.modelContext = requireNonNull(modelContext);
155 public RestconfDocumentedException(final Throwable cause, final List<RestconfError> errors) {
157 if (errors.isEmpty()) {
158 throw new IllegalArgumentException("At least one error is required");
160 this.errors = List.copyOf(errors);
165 public String getMessage() {
166 return "errors: " + errors;
170 * Reference to {@link EffectiveModelContext} in which this exception was generated. This method will return
171 * {@code null} if this exception was serialized or if the context is not available.
173 * @return Reference model context
175 public @Nullable EffectiveModelContext modelContext() {
179 public List<RestconfError> getErrors() {