Class JSONRPC2Error


  • public class JSONRPC2Error
    extends Object
    Represents a JSON-RPC 2.0 error that occurred during the processing of a request. This class is immutable.

    The protocol expects error objects to be structured like this:

    • code An integer that indicates the error type.
    • message A string providing a short description of the error. The message should be limited to a concise single sentence.
    • data Additional information, which may be omitted. Its contents is entirely defined by the application.

    Note that the "Error" word in the class name was put there solely to comply with the parlance of the JSON-RPC spec. This class doesn't inherit from java.lang.Error. It's a regular subclass of java.lang.Exception and, if thrown, it's to indicate a condition that a reasonable application might want to catch.

    This class also includes convenient final static instances for all standard JSON-RPC 2.0 errors:

    Note that the range -32099..-32000 is reserved for additional server errors.

    The mapping between JSON and Java entities (as defined by the underlying JSON Smart library):

         true|false  ---  java.lang.Boolean
         number      ---  java.lang.Number
         string      ---  java.lang.String
         array       ---  java.util.List
         object      ---  java.util.Map
         null        ---  null
     
    Author:
    Vladimir Dzhuvinov
    • Field Detail

      • PARSE_ERROR

        public static final JSONRPC2Error PARSE_ERROR
        JSON parse error (-32700).
      • INVALID_REQUEST

        public static final JSONRPC2Error INVALID_REQUEST
        Invalid JSON-RPC 2.0 request error (-32600).
      • METHOD_NOT_FOUND

        public static final JSONRPC2Error METHOD_NOT_FOUND
        Method not found error (-32601).
      • INVALID_PARAMS

        public static final JSONRPC2Error INVALID_PARAMS
        Invalid parameters error (-32602).
      • INTERNAL_ERROR

        public static final JSONRPC2Error INTERNAL_ERROR
        Internal JSON-RPC 2.0 error (-32603).
    • Constructor Detail

      • JSONRPC2Error

        public JSONRPC2Error()
      • JSONRPC2Error

        public JSONRPC2Error​(int code,
                             String message)
        Creates a new JSON-RPC 2.0 error with the specified code and message. The optional data is omitted.
        Parameters:
        code - The error code (standard pre-defined or application-specific).
        message - The error message.
      • JSONRPC2Error

        public JSONRPC2Error​(int code,
                             String message,
                             Object data)
        Creates a new JSON-RPC 2.0 error with the specified code, message and data.
        Parameters:
        code - The error code (standard pre-defined or application-specific).
        message - The error message.
        data - Optional error data, must map to a valid JSON type.
    • Method Detail

      • getMessage

        public String getMessage()
      • setMessage

        public void setMessage​(String message)
      • getCode

        public int getCode()
        Gets the JSON-RPC 2.0 error code.
        Returns:
        The error code.
      • setCode

        public void setCode​(int code)
      • getData

        public Object getData()
        Gets the JSON-RPC 2.0 error data.
        Returns:
        The error data, null if none was specified.
      • setData

        public void setData​(Object data)
      • appendMessage

        public JSONRPC2Error appendMessage​(String apx)
        Appends the specified string to the message of this JSON-RPC 2.0 error.
        Parameters:
        apx - The string to append to the original error message.
        Returns:
        A new JSON-RPC 2.0 error with the appended message.
      • equals

        public boolean equals​(Object object)
        Overrides Object.equals().
        Overrides:
        equals in class Object
        Parameters:
        object - The object to compare to.
        Returns:
        true if both objects are instances if this class and their error codes are identical, false if not.