001package org.consensusj.jsonrpc;
002
003/**
004 * JSON-RPC returned HTTP status other than 200 (and unfortunately also sometimes when their is response.error)
005 * Additional information is usually in JSON-RPC response
006 * TODO: See TODO in parent class {@link JsonRpcException}
007 * @see org.consensusj.jsonrpc.JsonRpcException
008 */
009public class JsonRpcStatusException extends JsonRpcException {
010    public final String httpMessage;
011    public final int httpCode;
012    public final int jsonRpcCode;
013
014    /**
015     * Http response body as a string. Null if not-available (check deserialized JSON in this case)
016     */
017    public final String response;
018
019    /**
020     * Deserialized response message, if available. Null if not-available.
021     * Result is usually null when an error occurs, but the type of the result is
022     * unspecified and could be either {@link java.util.Map}, {@link com.fasterxml.jackson.databind.JsonNode},
023     * or the result type of the failed request depending upon where and how the exception was
024     * created.
025     */
026    public final JsonRpcResponse<?> responseJson;
027
028    /**
029     * Canonical Constructor
030     *
031     * @param message Error message from Json if available, else http status message
032     * @param httpCode HTTP status code, e.g. 404
033     * @param httpMessage HTTP status message, e.g. "Not found" (removed from HTTP/2 and HTTP/3)
034     * @param jsonRPCCode Integer error code in JSON response, if any
035     * @param responseBody responseBody body as string (null if JSON available)
036     * @param responseBodyJson responseBody body as Json Map (null if JSON not-available)
037     */
038    public JsonRpcStatusException(String message, int httpCode, String httpMessage, int jsonRPCCode, String responseBody, JsonRpcResponse<?> responseBodyJson ) {
039        super(message);
040        this.httpCode = httpCode;
041        this.httpMessage = httpMessage;
042        this.jsonRpcCode = jsonRPCCode;
043        this.response = responseBody;
044        this.responseJson = responseBodyJson;
045    }
046
047    /**
048     * Same as canonical, but without the {@code httpCode} parameter. (which is not present in java.net.http, HTTP/2, etc.)
049     */
050    public JsonRpcStatusException(String message, int httpCode, int jsonRPCCode, String responseBody, JsonRpcResponse<?> responseBodyJson ) {
051        this(message, httpCode, "", jsonRPCCode, responseBody, responseBodyJson);
052    }
053
054    /**
055     * Constructor for when we were able to deserialize a JSON response
056     * @param httpCode http status code
057     * @param responseBodyJson deserialized JSON
058     */
059    public JsonRpcStatusException(int httpCode, JsonRpcResponse<?> responseBodyJson) {
060        this(responseBodyJson.getError().getMessage(),
061                httpCode,
062                responseBodyJson.getError() != null ? responseBodyJson.getError().getCode() : 0,
063                null,
064                responseBodyJson);
065    }
066
067    /**
068     * Constructor for when we were unable to deserialize a JSON response
069     * @param httpCode http status code
070     * @param responseBody response body as a string
071     */
072    public JsonRpcStatusException(int httpCode, String responseBody) {
073        this(responseBody, httpCode, 0, responseBody, null);
074    }
075}