Handle exceptions
Updated at:
Copy as MD
The TSDB SDK cannot be used to forcibly handle exceptions. All exceptions are thrown as RuntimeException subclasses. Catch and handle each exception type based on your application's requirements.
Exception types
| Exception class | HTTP status | Description |
|---|---|---|
HttpClientException | — | A general client-side error. |
HttpClientConnectionRefusedException | — | The TSDB server refused the connection. |
HttpClientInitException | — | The client failed to initialize. |
HttpClientSocketTimeoutException | — | The request timed out. |
HttpServerErrorException | 5xx | The server returned a 5xx error. Call getStatus() for the HTTP status code and getMessage() for the error message. |
HttpServerNotSupportException | 4xx | The server returned a 4xx error. Call getStatus() for the HTTP status code and getMessage() for the error message. |
HttpUnknowStatusException | — | The server returned an unrecognized response code. |
BufferQueueFullException | — | The buffer queue for asynchronous requests is full. |
Handle exceptions in synchronous operations
Use a try-catch block to catch specific exception types. Call getStatus() and getMessage() on server exceptions to retrieve the HTTP status code and error details:
try {
tsdb.ttl(10000);
} catch (HttpServerNotSupportException e) {
int status = e.getStatus();
String message = e.getMessage();
System.err.println(status + "," + message);
} catch (HttpServerErrorException e) {
int status = e.getStatus();
String message = e.getMessage();
System.err.println(status + "," + message);
}Handle exceptions in asynchronous operations
Override the failed() method in your callback class to handle exceptions. The SDK calls failed() when an asynchronous operation encounters an error.
QueryCallback example
QueryCallback cb = new QueryCallback() {
@Override
public void response(Query input, List<QueryResult> result) {
System.out.println("Query parameters:" + input);
System.out.println("Returned result:" + result);
}
// Override failed() to handle query errors.
@Override
public void failed(Query request, Exception ex) {
super.failed(request, ex);
}
};BatchPutCallback example
BatchPutCallback cb = new BatchPutCallback() {
@Override
public void response(List<Point> input, Result output) {
long afterNum = num.addAndGet(input.size());
System.out.println("Successful" + input.size() + ", Processed" + afterNum);
}
@Override
public void failed(List<Point> input, Exception ex) {
ex.printStackTrace();
long afterNum = num.addAndGet(input.size());
System.out.println("Failed" + input.size() + ", Processed" + afterNum);
}
};Is this page helpful?