Merge branch 'master' of ../controller
[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
9 package org.opendaylight.yangtools.yang.common;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Arrays;
16 import java.util.List;
17 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
18
19 /**
20  * A general base exception for an operation failure.
21  *
22  * @author Thomas Pantelis
23  */
24 public class OperationFailedException extends Exception {
25
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 errors.
32      *
33      * @param message the detail message
34      * @param errors {@link RpcError} instances that provide additional error information about
35      *               this exception
36      */
37     public OperationFailedException(final String message, final RpcError... errors) {
38         this(message, null, errors);
39     }
40
41     /**
42      * Constructs a new instance with the specified detail message, cause and errors.
43      *
44      * @param message the detail message
45      * @param cause the cause
46      * @param errors {@link RpcError} instances that provide additional error information about
47      *               this exception
48      */
49     public OperationFailedException(final String message, final Throwable cause,
50                                     final RpcError... errors) {
51         super(requireNonNull(message), cause);
52
53         if (errors != null && errors.length > 0) {
54             errorList = ImmutableList.copyOf(Arrays.asList(errors));
55         } else {
56             // Add a default RpcError.
57             errorList = ImmutableList.of(RpcResultBuilder.newError(ErrorType.APPLICATION, null,
58                     getMessage(), null, null, getCause()));
59         }
60     }
61
62     /**
63      * Returns additional error information about this exception.
64      *
65      * @return a List of RpcErrors. There is always at least one RpcError.
66      */
67     public List<RpcError> getErrorList() {
68         return errorList;
69     }
70
71     @Override
72     public String toString() {
73         return MoreObjects.toStringHelper(this).add("message", getMessage())
74                 .add("errorList", errorList).toString();
75     }
76 }