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