001    package com.thetransactioncompany.jsonrpc2;
002    
003    
004    /** 
005     * Thrown to indicate an exception during the parsing of a JSON-RPC 2.0 
006     * message string.
007     *
008     * <p>The JSON-RPC 2.0 specification and user group forum can be found 
009     * <a href="http://groups.google.com/group/json-rpc">here</a>.
010     *
011     * @author Vladimir Dzhuvinov
012     */
013    public class JSONRPC2ParseException extends Exception {
014            
015            
016            /**
017             * Serial version UID.
018             */
019            private static final long serialVersionUID = 3376608778436136410l;
020    
021    
022            /**
023             * Indicates a parse exception caused by a JSON message not conforming
024             * to the JSON-RPC 2.0 protocol.
025             */
026            public static int PROTOCOL = 0;
027            
028            
029            /**
030             * Indicates a parse exception caused by invalid JSON.
031             */
032            public static int JSON = 1;
033            
034            
035            /**
036             * The parse exception cause type. Default is {@link #PROTOCOL}.
037             */
038            private int causeType = PROTOCOL;
039            
040            
041            
042            /** 
043             * The string that could't be parsed.
044             */
045            private String unparsableString = null;
046            
047            
048            /** 
049             * Creates a new parse exception with the specified message. The cause 
050             * type is set to {@link #PROTOCOL}.
051             *
052             * @param message The exception message.
053             */
054            public JSONRPC2ParseException(final String message) {
055            
056                    super(message);
057            }
058            
059            
060            /**
061             * Creates a new parse exception with the specified message and the 
062             * original string that didn't parse. The cause type is set to
063             * {@link #PROTOCOL}.
064             *
065             * @param message          The exception message.
066             * @param unparsableString The unparsable string.
067             */
068            public JSONRPC2ParseException(final String message, final String unparsableString) {
069            
070                    super(message);
071                    this.unparsableString = unparsableString;
072            }
073            
074            
075            /**
076             * Creates a new parse exception with the specified message, cause type 
077             * and the original string that didn't parse.
078             *
079             * @param message          The exception message.
080             * @param causeType        The exception cause type, either 
081             *                         {@link #PROTOCOL} or {@link #JSON}.
082             * @param unparsableString The unparsable string.
083             */
084            public JSONRPC2ParseException(final String message, final int causeType, final String unparsableString) {
085            
086                    super(message);
087                    
088                    if (causeType != PROTOCOL && causeType != JSON)
089                            throw new IllegalArgumentException("Cause type must be either PROTOCOL or JSON");
090                    
091                    this.causeType = causeType;
092                    this.unparsableString = unparsableString;
093            }
094            
095            
096            /**
097             * Gets the parse exception cause type.
098             *
099             * @return The cause type, either {@link #PROTOCOL} or {@link #JSON}.
100             */
101            public int getCauseType() {
102            
103                    return causeType;
104            }
105            
106            
107            /**
108             * Gets original string that caused the parse exception (if specified).
109             *
110             * @return The string that didn't parse, {@code null} if none.
111             */
112            public String getUnparsableString() {
113            
114                    return unparsableString;
115            }
116    }