Sdílet prostřednictvím


CosmosException Class

public class CosmosException
extends 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 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.

Constructor Summary

Modifier Constructor Description
protected CosmosException(int statusCode, CosmosError cosmosErrorResource, Map<String,String> responseHeaders)

Creates a new instance of the CosmosException class.

protected CosmosException(int statusCode, Exception innerException)

Creates a new instance of the CosmosException class.

protected CosmosException(int statusCode, String errorMessage)

Creates a new instance of the CosmosException class.

protected CosmosException(int statusCode, String message, Map<String,String> responseHeaders, Throwable cause)

Creates a new instance of the CosmosException class.

protected CosmosException(String resourceAddress, int statusCode, CosmosError cosmosErrorResource, Map<String,String> responseHeaders)

Creates a new instance of the CosmosException class.

protected CosmosException(String resourceAddress, int statusCode, CosmosError cosmosErrorResource, Map<String,String> responseHeaders, Throwable cause)

Creates a new instance of the CosmosException class.

protected CosmosException(String message, Exception exception, Map<String,String> responseHeaders, int statusCode, String resourceAddress)

Creates a new instance of the CosmosException class.

Method Summary

Modifier and Type Method and Description
String getActivityId()

Gets the activity ID associated with the request.

CosmosDiagnostics getDiagnostics()

Gets the Cosmos Diagnostic Statistics associated with this exception.

String getMessage()
double getRequestCharge()

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

Map<String,String> getResponseHeaders()

Gets the response headers as key-value pairs

Duration getRetryAfterDuration()

Gets the recommended time duration after which the client can retry failed requests

String getShortMessage()

Returns the error message without any diagnostics - using this method is only useful when also logging the getDiagnostics() separately.

int getStatusCode()

Gets the http status code.

int getSubStatusCode()

Gets the sub status code.

String toString()

Methods inherited from java.lang.Object

Methods inherited from java.lang.Throwable

Constructor Details

CosmosException

protected CosmosException(int statusCode, CosmosError cosmosErrorResource, Map 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(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, 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, String message, Map 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(String resourceAddress, int statusCode, CosmosError cosmosErrorResource, Map 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, CosmosError cosmosErrorResource, Map 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 responseHeaders, int statusCode, String resourceAddress)

Creates a new instance of the CosmosException class.

Parameters:

message - the string message.
exception - the exception object.
responseHeaders - the response headers.
statusCode - the http status code of the response.
resourceAddress - the address of the resource the request is associated with.

Method Details

getActivityId

public String getActivityId()

Gets the activity ID associated with the request.

Returns:

the activity ID.

getDiagnostics

public CosmosDiagnostics getDiagnostics()

Gets the Cosmos Diagnostic Statistics associated with this exception.

Returns:

Cosmos Diagnostic Statistics associated with this exception.

getMessage

public String getMessage()

Overrides:

CosmosException.getMessage()

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.

getResponseHeaders

public Map getResponseHeaders()

Gets the response headers as key-value pairs

Returns:

the response headers

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.

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

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.

toString

public String toString()

Overrides:

CosmosException.toString()

Applies to