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