Move message formatting to StatementSourceException
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / StatementSourceException.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.yangtools.yang.model.api.meta;
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.NonNull;
18
19 /**
20  * An exception identifying a problem detected at a particular YANG statement. Exposes {@link #sourceRef()} to that
21  * statement.
22  */
23 public class StatementSourceException extends RuntimeException {
24     @java.io.Serial
25     private static final long serialVersionUID = 2L;
26
27     private final @NonNull StatementSourceReference sourceRef;
28
29     public StatementSourceException(final StatementSourceReference sourceRef, final String message) {
30         super(createMessage(sourceRef, message));
31         this.sourceRef = requireNonNull(sourceRef);
32     }
33
34     public StatementSourceException(final StatementSourceReference sourceRef, final String message,
35             final Throwable cause) {
36         super(createMessage(sourceRef, message), cause);
37         this.sourceRef = requireNonNull(sourceRef);
38     }
39
40     public StatementSourceException(final StatementSourceReference sourceRef, final String format,
41             final Object... args) {
42         this(sourceRef, format.formatted(args));
43     }
44
45     private static String createMessage(final StatementSourceReference sourceRef, final String message) {
46         return requireNonNull(message) + " [at " + requireNonNull(sourceRef) + ']';
47     }
48
49     /**
50      * Return the reference to the source which caused this exception.
51      *
52      * @return the reference to the source which caused this exception
53      */
54     public final @NonNull StatementSourceReference sourceRef() {
55         return sourceRef;
56     }
57
58     @java.io.Serial
59     private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
60         throwNSE();
61     }
62
63     @java.io.Serial
64     private void readObjectNoData() throws ObjectStreamException {
65         throwNSE();
66     }
67
68     @java.io.Serial
69     private void writeObject(final ObjectOutputStream stream) throws IOException {
70         throwNSE();
71     }
72
73     protected final void throwNSE() throws NotSerializableException {
74         throw new NotSerializableException(getClass().getName());
75     }
76 }