Fix 500 server error in PUT request
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / ServerException.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.restconf.server.api;
9
10 import static java.util.Objects.requireNonNull;
11
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;
22
23 /**
24  * A server-side processing exception, reporting a single {@link ServerError}. This exception is not serializable on
25  * purpose.
26  */
27 @NonNullByDefault
28 public final class ServerException extends Exception {
29     @java.io.Serial
30     private static final long serialVersionUID = 0L;
31
32     private final ServerError error;
33
34     private ServerException(final String message, final ServerError error, final @Nullable Throwable cause) {
35         super(message, cause);
36         this.error = requireNonNull(error);
37     }
38
39     public ServerException(final String message) {
40         this(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, requireNonNull(message));
41     }
42
43     public ServerException(final String format, final Object @Nullable ... args) {
44         this(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, format, args);
45     }
46
47     public ServerException(final Throwable cause) {
48         this(ErrorType.APPLICATION, errorTagOf(cause), cause);
49     }
50
51     public ServerException(final String message, final @Nullable Throwable cause) {
52         this(ErrorType.APPLICATION, errorTagOf(cause), requireNonNull(message), cause);
53     }
54
55     public ServerException(final ErrorType type, final ErrorTag tag, final String message) {
56         this(type, tag, message, (Throwable) null);
57     }
58
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),
61             cause);
62     }
63
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);
67     }
68
69     public ServerException(final ErrorType type, final ErrorTag tag, final String format,
70             final Object @Nullable ... args) {
71         this(type, tag, format.formatted(args));
72     }
73
74     /**
75      * Return the reported {@link ServerError}.
76      *
77      * @return the reported {@link ServerError}
78      */
79     public ServerError error() {
80         return error;
81     }
82
83     @java.io.Serial
84     private void readObjectNoData() throws ObjectStreamException {
85         throw new NotSerializableException();
86     }
87
88     @java.io.Serial
89     private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
90         throw new NotSerializableException();
91     }
92
93     @java.io.Serial
94     private void writeObject(final ObjectOutputStream stream) throws IOException {
95         throw new NotSerializableException();
96     }
97
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;
103         } else {
104             return ErrorTag.OPERATION_FAILED;
105         }
106     }
107 }