Class CosmosException

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
com.azure.core.exception.AzureException
com.azure.cosmos.CosmosException
All Implemented Interfaces:
Serializable

public class CosmosException extends com.azure.core.exception.AzureException
This class defines a custom exception type for all operations on CosmosClient in the Azure Cosmos DB database service. Applications are expected to catch CosmosException and handle errors as appropriate when calling methods on CosmosClient.

Errors coming from the service during normal execution are converted to CosmosException before returning to the application with the following exception:

When a BE error is encountered during a QueryIterable<T> iteration, an IllegalStateException is thrown instead of CosmosException.

When a transport level error happens that request is not able to reach the service, an IllegalStateException is thrown instead of CosmosException.

See Also:
  • Constructor Details

    • CosmosException

      protected CosmosException(int statusCode, String message, Map<String,String> responseHeaders, Throwable cause)
      Creates a new instance of the CosmosException class.
      Parameters:
      statusCode - the http status code of the response.
      message - the string message.
      responseHeaders - the response headers.
      cause - the inner exception
    • CosmosException

      protected CosmosException(int statusCode, String errorMessage)
      Creates a new instance of the CosmosException class.
      Parameters:
      statusCode - the http status code of the response.
      errorMessage - the error message.
    • CosmosException

      protected CosmosException(int statusCode, Exception innerException)
      Creates a new instance of the CosmosException class.
      Parameters:
      statusCode - the http status code of the response.
      innerException - the original exception.
    • CosmosException

      protected CosmosException(int statusCode, com.azure.cosmos.implementation.CosmosError cosmosErrorResource, Map<String,String> responseHeaders)
      Creates a new instance of the CosmosException class.
      Parameters:
      statusCode - the http status code of the response.
      cosmosErrorResource - the error resource object.
      responseHeaders - the response headers.
    • CosmosException

      protected CosmosException(String resourceAddress, int statusCode, com.azure.cosmos.implementation.CosmosError cosmosErrorResource, Map<String,String> responseHeaders)
      Creates a new instance of the CosmosException class.
      Parameters:
      resourceAddress - the address of the resource the request is associated with.
      statusCode - the http status code of the response.
      cosmosErrorResource - the error resource object.
      responseHeaders - the response headers.
    • CosmosException

      protected CosmosException(String resourceAddress, int statusCode, com.azure.cosmos.implementation.CosmosError cosmosErrorResource, Map<String,String> responseHeaders, Throwable cause)
      Creates a new instance of the CosmosException class.
      Parameters:
      resourceAddress - the address of the resource the request is associated with.
      statusCode - the http status code of the response.
      cosmosErrorResource - the error resource object.
      responseHeaders - the response headers.
      cause - the inner exception
    • CosmosException

      protected CosmosException(String message, Exception exception, Map<String,String> responseHeaders, int statusCode, String resourceAddress)
      Creates a new instance of the CosmosException class.
      Parameters:
      message - the string message.
      statusCode - the http status code of the response.
      exception - the exception object.
      responseHeaders - the response headers.
      resourceAddress - the address of the resource the request is associated with.
  • Method Details

    • getMessage

      public String getMessage()
      Overrides:
      getMessage in class Throwable
    • getShortMessage

      public String getShortMessage()
      Returns the error message without any diagnostics - using this method is only useful when also logging the getDiagnostics() separately. Without diagnostics it will often be impossible to determine the root cause of an error.
      Returns:
      the error message without any diagnostics
    • getActivityId

      public String getActivityId()
      Gets the activity ID associated with the request.
      Returns:
      the activity ID.
    • getStatusCode

      public int getStatusCode()
      Gets the http status code.
      Returns:
      the status code.
    • getSubStatusCode

      public int getSubStatusCode()
      Gets the sub status code. The sub status code is exposed for informational purposes only - new sub status codes can be added anytime and applications should never take a dependency on certain sub status codes. For applications treating errors based on status code is sufficient.
      Returns:
      the sub status code.
    • getRetryAfterDuration

      public Duration getRetryAfterDuration()
      Gets the recommended time duration after which the client can retry failed requests
      Returns:
      the recommended time duration after which the client can retry failed requests.
    • getResponseHeaders

      public Map<String,String> getResponseHeaders()
      Gets the response headers as key-value pairs
      Returns:
      the response headers
    • getDiagnostics

      public CosmosDiagnostics getDiagnostics()
      Gets the Cosmos Diagnostic Statistics associated with this exception.
      Returns:
      Cosmos Diagnostic Statistics associated with this exception.
    • getRequestCharge

      public double getRequestCharge()
      Gets the request charge as request units (RU) consumed by the operation.

      For more information about the RU and factors that can impact the effective charges please visit Request Units in Azure Cosmos DB

      Returns:
      the request charge.
    • toString

      public String toString()
      Overrides:
      toString in class Throwable