Add YangError.getErrorTag()
[yangtools.git] / data / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / codec / YangInvalidValueException.java
1 /*
2  * Copyright (c) 2019 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.data.api.codec;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
17 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
18 import org.opendaylight.yangtools.yang.common.YangError;
19 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
20
21 /**
22  * Dedicated exception for reporting conditions where {@code error-tag} should be reported as {@code invalid-value}.
23  * This is covered by <a href="https://tools.ietf.org/html/rfc7950#section-8.3.1">RFC7950 section 8.3.1</a>:
24  * <pre>
25  *   If a leaf data value does not match the type constraints for the
26  *   leaf, including those defined in the type's "range", "length", and
27  *   "pattern" properties, the server MUST reply with an
28  *   "invalid-value" &lt;error-tag&gt; in the &lt;rpc-error&gt;, and with the
29  *   error-app-tag (Section 7.5.4.2) and error-message
30  *   (Section 7.5.4.1) associated with the constraint, if any exist.
31  * </pre>
32  *
33  * <p>
34  * This error tag also references <a href="https://tools.ietf.org/html/rfc6241#appendix-A">RFC6241 Appendix A</a>,
35  * which defines the appropriate severity and adds more semantics.
36  */
37 @Beta
38 public class YangInvalidValueException extends IllegalArgumentException implements YangError {
39     private static final long serialVersionUID = 1L;
40
41     private final @NonNull ErrorType errorType;
42     private final @Nullable String errorAppTag;
43     private final @Nullable String errorMessage;
44
45     public YangInvalidValueException(final ErrorType errorType, final ConstraintMetaDefinition constraint,
46             final String message) {
47         super(requireNonNull(message));
48         this.errorType = requireNonNull(errorType);
49         this.errorAppTag = constraint.getErrorAppTag().orElse(null);
50         this.errorMessage = constraint.getErrorMessage().orElse(null);
51     }
52
53     @Override
54     public final ErrorType getErrorType() {
55         return errorType;
56     }
57
58     @Override
59     public final ErrorSeverity getSeverity() {
60         return ErrorSeverity.ERROR;
61     }
62
63     @Override
64     public final String getErrorTag() {
65         return "invalid-value";
66     }
67
68     @Override
69     public final Optional<String> getErrorAppTag() {
70         return Optional.ofNullable(errorAppTag);
71     }
72
73     @Override
74     public final Optional<String> getErrorMessage() {
75         return Optional.ofNullable(errorMessage);
76     }
77 }