Bug 1362: New AsyncWriteTransaction#submit method
[controller.git] / opendaylight / md-sal / sal-common-api / src / main / java / org / opendaylight / controller / md / sal / common / api / data / TransactionCommitFailedException.java
1 /*
2  * Copyright (c) 2014 Cisco 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.controller.md.sal.common.api.data;
9
10 import java.util.Arrays;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.yang.common.RpcError;
14 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
15 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
16
17 import com.google.common.collect.ImmutableList;
18
19 /**
20  *
21  * Failed commit of asynchronous transaction
22  *
23  * This exception is raised and returned when transaction commit
24  * failed.
25  *
26  */
27 public class TransactionCommitFailedException extends Exception {
28
29     private static final long serialVersionUID = 1L;
30
31     private final List<RpcError> errorList;
32
33     public TransactionCommitFailedException(final String message, final RpcError... errors) {
34         this(message, null, errors);
35     }
36
37     public TransactionCommitFailedException(final String message, final Throwable cause,
38                                             final RpcError... errors) {
39         super(message, cause);
40
41         if( errors != null && errors.length > 0 ) {
42             errorList = ImmutableList.<RpcError>builder().addAll( Arrays.asList( errors ) ).build();
43         }
44         else {
45             // Add a default RpcError.
46             errorList = ImmutableList.of(RpcResultBuilder.newError(ErrorType.APPLICATION, null,
47                     getMessage(), null, null, getCause()));
48         }
49     }
50
51     /**
52      * Returns additional error information about this exception.
53      *
54      * @return a List of RpcErrors. There is always at least one RpcError.
55      */
56     public List<RpcError> getErrorList() {
57         return errorList;
58     }
59
60     @Override
61     public String getMessage() {
62         return new StringBuilder( super.getMessage() ).append(", errors: ").append( errorList ).toString();
63     }
64 }