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.io.Serial;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.List;
21 * A general base exception for an operation failure.
23 * @author Thomas Pantelis
25 public class OperationFailedException extends Exception {
27 private static final long serialVersionUID = 1L;
29 private final ImmutableList<RpcError> errorList;
32 * Constructs a new instance with the specified detail message and cause.
34 * @param message the detail message
35 * @param cause the cause
36 * @throws NullPointerException if {@code message} is {@code null}
38 public OperationFailedException(final String message, final Throwable cause) {
39 super(requireNonNull(message), cause);
44 * Constructs a new instance with the specified detail message and error.
46 * @param message the detail message
47 * @param error {@link RpcError} instance that provides additional error information about this exception
48 * @throws NullPointerException if any argument is {@code null}
50 public OperationFailedException(final String message, final RpcError error) {
51 super(requireNonNull(message));
52 errorList = ImmutableList.of(error);
56 * Constructs a new instance with the specified detail message, cause and errors.
58 * @param message the detail message
59 * @param cause the cause
60 * @param errors {@link RpcError} instances that provide additional error information about this exception
61 * @throws NullPointerException if either {@code message} or {@code errors} is {@code null}, or if {@code errors}
62 * contains a {@code null} element.
64 public OperationFailedException(final String message, final Throwable cause, final Collection<RpcError> errors) {
65 super(requireNonNull(message), cause);
66 errorList = errors.isEmpty() ? null : ImmutableList.copyOf(errors);
70 * Constructs a new instance with the specified detail message and errors.
72 * @param message the detail message
73 * @param errors {@link RpcError} instances that provide additional error information about this exception
74 * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
76 public OperationFailedException(final String message, final Collection<? extends RpcError> errors) {
77 super(requireNonNull(message));
78 errorList = ImmutableList.copyOf(errors);
82 * Constructs a new instance with the specified detail message and errors.
84 * @param message the detail message
85 * @param errors {@link RpcError} instances that provide additional error information about this exception
86 * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
88 public OperationFailedException(final String message, final RpcError... errors) {
89 this(message, null, errors);
93 * Constructs a new instance with the specified detail message, cause and errors.
95 * @param message the detail message
96 * @param cause the cause
97 * @param errors {@link RpcError} instances that provide additional error information about this exception
99 public OperationFailedException(final String message, final Throwable cause, final RpcError... errors) {
100 this(message, cause, Arrays.asList(errors));
104 * Returns additional error information about this exception.
106 * @return a List of RpcErrors. There is always at least one RpcError.
108 public List<RpcError> getErrorList() {
109 return errorList != null ? errorList : ImmutableList.of(
110 RpcResultBuilder.newError(ErrorType.APPLICATION, null, getMessage(), null, null, getCause()));
114 public final String toString() {
115 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
118 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
119 return helper.add("message", getMessage()).add("errorList", getErrorList());