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