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