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;
20 * A general base exception for an operation failure.
22 * @author Thomas Pantelis
24 public class OperationFailedException extends Exception {
25 private static final long serialVersionUID = 1L;
27 private final ImmutableList<RpcError> errorList;
30 * Constructs a new instance with the specified detail message and cause.
32 * @param message the detail message
33 * @param cause the cause
34 * @throws NullPointerException if {@code message} is {@code null}
36 public OperationFailedException(final String message, final Throwable cause) {
37 super(requireNonNull(message), cause);
42 * Constructs a new instance with the specified detail message and error.
44 * @param message the detail message
45 * @param error {@link RpcError} instance that provides additional error information about this exception
46 * @throws NullPointerException if any argument is {@code null}
48 public OperationFailedException(final String message, final RpcError error) {
49 super(requireNonNull(message));
50 errorList = ImmutableList.of(error);
54 * Constructs a new instance with the specified detail message, cause and errors.
56 * @param message the detail message
57 * @param cause the cause
58 * @param errors {@link RpcError} instances that provide additional error information about this exception
59 * @throws NullPointerException if either {@code message} or {@code errors} is {@code null}, or if {@code errors}
60 * contains a {@code null} element.
62 public OperationFailedException(final String message, final Throwable cause, final Collection<RpcError> errors) {
63 super(requireNonNull(message), cause);
64 errorList = errors.isEmpty() ? null : ImmutableList.copyOf(errors);
68 * Constructs a new instance with the specified detail message and errors.
70 * @param message the detail message
71 * @param errors {@link RpcError} instances that provide additional error information about this exception
72 * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
74 public OperationFailedException(final String message, final Collection<? extends RpcError> errors) {
75 super(requireNonNull(message));
76 this.errorList = ImmutableList.copyOf(errors);
80 * Constructs a new instance with the specified detail message and errors.
82 * @param message the detail message
83 * @param errors {@link RpcError} instances that provide additional error information about this exception
84 * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
86 public OperationFailedException(final String message, final RpcError... errors) {
87 this(message, null, errors);
91 * Constructs a new instance with the specified detail message, cause and errors.
93 * @param message the detail message
94 * @param cause the cause
95 * @param errors {@link RpcError} instances that provide additional error information about this exception
97 public OperationFailedException(final String message, final Throwable cause, final RpcError... errors) {
98 this(message, cause, Arrays.asList(errors));
102 * Returns additional error information about this exception.
104 * @return a List of RpcErrors. There is always at least one RpcError.
106 public List<RpcError> getErrorList() {
107 return errorList != null ? errorList : ImmutableList.of(
108 RpcResultBuilder.newError(ErrorType.APPLICATION, null, getMessage(), null, null, getCause()));
112 public final String toString() {
113 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
116 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
117 return helper.add("message", getMessage()).add("errorList", getErrorList());