2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static java.util.Objects.requireNonNull;
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 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
21 * A general base exception for an operation failure.
23 * @author Thomas Pantelis
25 public class OperationFailedException extends Exception {
26 private static final long serialVersionUID = 1L;
28 private final ImmutableList<RpcError> errorList;
31 * Constructs a new instance with the specified detail message and cause.
33 * @param message the detail message
34 * @param cause the cause
35 * @throws NullPointerException if {@code message} is {@code null}
37 public OperationFailedException(final String message, final Throwable cause) {
38 super(requireNonNull(message), cause);
43 * Constructs a new instance with the specified detail message and error.
45 * @param message the detail message
46 * @param error {@link RpcError} instance that provides additional error information about this exception
47 * @throws NullPointerException if any argument is {@code null}
49 public OperationFailedException(final String message, final RpcError error) {
50 super(requireNonNull(message));
51 errorList = ImmutableList.of(error);
55 * Constructs a new instance with the specified detail message, cause and errors.
57 * @param message the detail message
58 * @param cause the cause
59 * @param errors {@link RpcError} instances that provide additional error information about this exception
60 * @throws NullPointerException if either {@code message} or {@code errors} is {@code null}, or if {@code errors}
61 * contains a {@code null} element.
63 public OperationFailedException(final String message, final Throwable cause, final Collection<RpcError> errors) {
64 super(requireNonNull(message), cause);
65 errorList = errors.isEmpty() ? null : ImmutableList.copyOf(errors);
69 * Constructs a new instance with the specified detail message and errors.
71 * @param message the detail message
72 * @param errors {@link RpcError} instances that provide additional error information about this exception
73 * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
75 public OperationFailedException(final String message, final Collection<? extends RpcError> errors) {
76 super(requireNonNull(message));
77 this.errorList = ImmutableList.copyOf(errors);
81 * Constructs a new instance with the specified detail message and errors.
83 * @param message the detail message
84 * @param errors {@link RpcError} instances that provide additional error information about this exception
85 * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
87 public OperationFailedException(final String message, final RpcError... errors) {
88 this(message, null, errors);
92 * Constructs a new instance with the specified detail message, cause and errors.
94 * @param message the detail message
95 * @param cause the cause
96 * @param errors {@link RpcError} instances that provide additional error information about this exception
98 public OperationFailedException(final String message, final Throwable cause, final RpcError... errors) {
99 this(message, null, Arrays.asList(errors));
103 * Returns additional error information about this exception.
105 * @return a List of RpcErrors. There is always at least one RpcError.
107 public List<RpcError> getErrorList() {
108 return errorList != null ? errorList : ImmutableList.of(
109 RpcResultBuilder.newError(ErrorType.APPLICATION, null, getMessage(), null, null, getCause()));
113 public final String toString() {
114 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
117 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
118 return helper.add("message", getMessage()).add("errorList", getErrorList());