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