View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions Copyrighted [year] [name of copyright owner]".
13   *
14   * Copyright © 2010–2011 ApexIdentity Inc. All rights reserved.
15   * Portions Copyrighted 2011-2015 ForgeRock AS.
16   */
17  
18  package org.forgerock.json;
19  
20  /**
21   * An exception that is thrown during JSON value operations.
22   */
23  public class JsonValueException extends JsonException {
24  
25      /** Serializable class a version number. */
26      static final long serialVersionUID = 1L;
27  
28      /** The JSON value for which the exception was thrown. */
29      private final JsonValue value;
30  
31      /**
32       * Constructs a new exception with the specified JSON value and {@code null}
33       * as its detail message.
34       *
35       * @param value
36       *            The JSON value.
37       */
38      public JsonValueException(JsonValue value) {
39          this.value = value;
40      }
41  
42      /**
43       * Constructs a new exception with the specified JSON value and detail
44       * message.
45       *
46       * @param value
47       *            The JSON value.
48       * @param message
49       *            The message.
50       */
51      public JsonValueException(JsonValue value, String message) {
52          super(message);
53          this.value = value;
54      }
55  
56      /**
57       * Constructs a new exception with the specified JSON value and cause.
58       *
59       * @param value
60       *            The JSON value.
61       * @param cause
62       *            The cause.
63       */
64      public JsonValueException(JsonValue value, Throwable cause) {
65          super(cause);
66          this.value = value;
67      }
68  
69      /**
70       * Constructs a new exception with the specified JSON value, detail message
71       * and cause.
72       *
73       * @param value
74       *            The JSON value.
75       * @param message
76       *            The message.
77       * @param cause
78       *            The cause.
79       */
80      public JsonValueException(JsonValue value, String message, Throwable cause) {
81          super(message, cause);
82          this.value = value;
83      }
84  
85      /**
86       * Returns the detail message string of this exception.
87       *
88       * @return The detail message string of this exception.
89       */
90      @Override
91      public String getMessage() {
92          StringBuilder sb = new StringBuilder();
93          String message = super.getMessage();
94          if (value != null) {
95              sb.append(value.getPointer().toString());
96          }
97          if (value != null && message != null) {
98              sb.append(": ");
99          }
100         if (message != null) {
101             sb.append(message);
102         }
103         return sb.toString();
104     }
105 
106     /**
107      * Returns the JSON value for which the exception was thrown.
108      *
109      * @return The JSON value for which the exception was thrown.
110      */
111     public JsonValue getJsonValue() {
112         return value;
113     }
114 }