2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.restconf.server.api;
10 import static java.util.Objects.requireNonNull;
12 import java.io.IOException;
13 import java.io.NotSerializableException;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutputStream;
16 import java.io.ObjectStreamException;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.restconf.api.ErrorMessage;
20 import org.opendaylight.yangtools.yang.common.ErrorTag;
21 import org.opendaylight.yangtools.yang.common.ErrorType;
24 * A server-side processing exception, reporting a single {@link ServerError}. This exception is not serializable on
28 public final class ServerException extends Exception {
30 private static final long serialVersionUID = 0L;
32 private final ServerError error;
34 private ServerException(final String message, final ServerError error, final @Nullable Throwable cause) {
35 super(message, cause);
36 this.error = requireNonNull(error);
39 public ServerException(final String message) {
40 this(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, requireNonNull(message));
43 public ServerException(final String format, final Object @Nullable ... args) {
44 this(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, format, args);
47 public ServerException(final Throwable cause) {
48 this(ErrorType.APPLICATION, errorTagOf(cause), cause);
51 public ServerException(final String message, final @Nullable Throwable cause) {
52 this(ErrorType.APPLICATION, errorTagOf(cause), requireNonNull(message), cause);
55 public ServerException(final ErrorType type, final ErrorTag tag, final String message) {
56 this(type, tag, message, (Throwable) null);
59 public ServerException(final ErrorType type, final ErrorTag tag, final Throwable cause) {
60 this(cause.toString(), new ServerError(type, tag, new ErrorMessage(cause.getMessage()), null, null, null),
64 public ServerException(final ErrorType type, final ErrorTag tag, final String message,
65 final @Nullable Throwable cause) {
66 this(requireNonNull(message), new ServerError(type, tag, message), cause);
69 public ServerException(final ErrorType type, final ErrorTag tag, final String format,
70 final Object @Nullable ... args) {
71 this(type, tag, format.formatted(args));
75 * Return the reported {@link ServerError}.
77 * @return the reported {@link ServerError}
79 public ServerError error() {
84 private void readObjectNoData() throws ObjectStreamException {
85 throw new NotSerializableException();
89 private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
90 throw new NotSerializableException();
94 private void writeObject(final ObjectOutputStream stream) throws IOException {
95 throw new NotSerializableException();
98 private static ErrorTag errorTagOf(final @Nullable Throwable cause) {
99 if (cause instanceof UnsupportedOperationException) {
100 return ErrorTag.OPERATION_NOT_SUPPORTED;
101 } else if (cause instanceof IllegalArgumentException) {
102 return ErrorTag.INVALID_VALUE;
104 return ErrorTag.OPERATION_FAILED;