001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyrighted [year] [name of copyright owner]".
013 *
014 * Copyright © 2010–2011 ApexIdentity Inc. All rights reserved.
015 * Portions Copyrighted 2011-2015 ForgeRock AS.
016 */
017
018package org.forgerock.json;
019
020/**
021 * An exception that is thrown during JSON value operations.
022 */
023public class JsonValueException extends JsonException {
024
025    /** Serializable class a version number. */
026    static final long serialVersionUID = 1L;
027
028    /** The JSON value for which the exception was thrown. */
029    private final JsonValue value;
030
031    /**
032     * Constructs a new exception with the specified JSON value and {@code null}
033     * as its detail message.
034     *
035     * @param value
036     *            The JSON value.
037     */
038    public JsonValueException(JsonValue value) {
039        this.value = value;
040    }
041
042    /**
043     * Constructs a new exception with the specified JSON value and detail
044     * message.
045     *
046     * @param value
047     *            The JSON value.
048     * @param message
049     *            The message.
050     */
051    public JsonValueException(JsonValue value, String message) {
052        super(message);
053        this.value = value;
054    }
055
056    /**
057     * Constructs a new exception with the specified JSON value and cause.
058     *
059     * @param value
060     *            The JSON value.
061     * @param cause
062     *            The cause.
063     */
064    public JsonValueException(JsonValue value, Throwable cause) {
065        super(cause);
066        this.value = value;
067    }
068
069    /**
070     * Constructs a new exception with the specified JSON value, detail message
071     * and cause.
072     *
073     * @param value
074     *            The JSON value.
075     * @param message
076     *            The message.
077     * @param cause
078     *            The cause.
079     */
080    public JsonValueException(JsonValue value, String message, Throwable cause) {
081        super(message, cause);
082        this.value = value;
083    }
084
085    /**
086     * Returns the detail message string of this exception.
087     *
088     * @return The detail message string of this exception.
089     */
090    @Override
091    public String getMessage() {
092        StringBuilder sb = new StringBuilder();
093        String message = super.getMessage();
094        if (value != null) {
095            sb.append(value.getPointer().toString());
096        }
097        if (value != null && message != null) {
098            sb.append(": ");
099        }
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}