Migrate RFC references to rfc-editor.org
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / OperationFailedException.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.collect.ImmutableList;
15 import java.io.Serial;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.List;
19
20 /**
21  * A general base exception for an operation failure.
22  *
23  * @author Thomas Pantelis
24  */
25 public class OperationFailedException extends Exception {
26     @Serial
27     private static final long serialVersionUID = 1L;
28
29     private final ImmutableList<RpcError> errorList;
30
31     /**
32      * Constructs a new instance with the specified detail message and cause.
33      *
34      * @param message the detail message
35      * @param cause the cause
36      * @throws NullPointerException if {@code message} is {@code null}
37      */
38     public OperationFailedException(final String message, final Throwable cause) {
39         super(requireNonNull(message), cause);
40         errorList = null;
41     }
42
43     /**
44      * Constructs a new instance with the specified detail message and error.
45      *
46      * @param message the detail message
47      * @param error {@link RpcError} instance that provides additional error information about this exception
48      * @throws NullPointerException if any argument is {@code null}
49      */
50     public OperationFailedException(final String message, final RpcError error) {
51         super(requireNonNull(message));
52         errorList = ImmutableList.of(error);
53     }
54
55     /**
56      * Constructs a new instance with the specified detail message, cause and errors.
57      *
58      * @param message the detail message
59      * @param cause the cause
60      * @param errors {@link RpcError} instances that provide additional error information about this exception
61      * @throws NullPointerException if either {@code message} or {@code errors} is {@code null}, or if {@code errors}
62      *                              contains a {@code null} element.
63      */
64     public OperationFailedException(final String message, final Throwable cause, final Collection<RpcError> errors) {
65         super(requireNonNull(message), cause);
66         errorList = errors.isEmpty() ? null : ImmutableList.copyOf(errors);
67     }
68
69     /**
70      * Constructs a new instance with the specified detail message and errors.
71      *
72      * @param message the detail message
73      * @param errors {@link RpcError} instances that provide additional error information about this exception
74      * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
75      */
76     public OperationFailedException(final String message, final Collection<? extends RpcError> errors) {
77         super(requireNonNull(message));
78         errorList = ImmutableList.copyOf(errors);
79     }
80
81     /**
82      * Constructs a new instance with the specified detail message and errors.
83      *
84      * @param message the detail message
85      * @param errors {@link RpcError} instances that provide additional error information about this exception
86      * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
87      */
88     public OperationFailedException(final String message, final RpcError... errors) {
89         this(message, null, errors);
90     }
91
92     /**
93      * Constructs a new instance with the specified detail message, cause and errors.
94      *
95      * @param message the detail message
96      * @param cause the cause
97      * @param errors {@link RpcError} instances that provide additional error information about this exception
98      */
99     public OperationFailedException(final String message, final Throwable cause, final RpcError... errors) {
100         this(message, cause, Arrays.asList(errors));
101     }
102
103     /**
104      * Returns additional error information about this exception.
105      *
106      * @return a List of RpcErrors. There is always at least one RpcError.
107      */
108     public List<RpcError> getErrorList() {
109         return errorList != null ? errorList : ImmutableList.of(
110             RpcResultBuilder.newError(ErrorType.APPLICATION, null, getMessage(), null, null, getCause()));
111     }
112
113     @Override
114     public final String toString() {
115         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
116     }
117
118     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
119         return helper.add("message", getMessage()).add("errorList", getErrorList());
120     }
121 }