For the sake of clarity, most examples in the documentation do not fully demonstrate how to handle errors, preferring instead to focus on the topic at hand. However, the Diatheke SDK does report errors, and client applications should be prepared to handle them.
A description of how errors are handled for each language is given below.
The C++ SDK uses exceptions
to report errors. Errors originating from the SDK will have the class
type Diatheke::ClientError
,
which inherits from the std::exception
class.
To handle these errors, simply create a try-catch block around the SDK code.
For example:
try
{
// Call SDK functions in here. The function calls may be in this
// try block, or within functions that the try block calls.
}
catch (const Diatheke::ClientError &err)
{
// Handle the error here. The specific error message can be
// retrieved using err.what().
}
The Go SDK uses the built-in error
type to return errors from functions
(see here for general
information about handling errors in Go). Most SDK functions will return
an error in addition to their other return values. For example:
// Create a new client connection
client, err := diatheke.NewClient("127.0.0.1:9002")
if err != nil {
// Handle the error here.
}
The Python SDK uses built-in exceptions to report errors. They can be handled by wrapping the client code in a try … except statement. For example:
try:
# Call SDK functions in here. The SDK functions may be
# called directly, or within other functions that this
# block eventually calls.
except BaseException as err:
# Handle the error here. To have better control over how
# individual errors are handled, you may use more concrete
# exception types in the except statement.
finally:
# Do any final cleanup here.
In the Swift SDK Every method that can thow an error has DiathekeFailureCallback
completion handler to report errors. For example:
client.newSession(model: model, success: { (sessionID) in
// Handle success response here
}) { (error) in
// Handle the error here
print(error.localizedDescription)
}
diathekeStub.sessionEventStream(DiathekeOuterClass.SessionID.newBuilder().build(), new StreamObserver<DiathekeOuterClass.DiathekeEvent>() {
@Override
public void onNext(DiathekeOuterClass.DiathekeEvent value) {
// Handle success response here
}
@Override
public void onError(Throwable t) {
// Handle the error here
Log.e("DIATHEKE_ERROR",t.getMessage());
}
@Override
public void onCompleted() {
}
});