Hashgraph DID SDK: Handling Exceptions
This guide explains how to handle exceptions and errors when using the Hashgraph DID SDK. It covers common error scenarios, best practices for error handling, and strategies for debugging and troubleshooting.
Error classes
The Hashgraph DID SDK introduces a custom error class, DIDError
, to handle exceptions and errors. This class extends the native Error
class and provides additional properties and methods for error handling.
The DIDError
class includes the following properties:
-
code
: A unique error code that identifies the type of error. -
description
: A human-readable error message that describes the error in detail. -
isDIDError
: A boolean flag that indicates whether the error is an instance ofDIDError
.

Error codes
The DIDError
class defines a set of error codes that correspond to different types of errors. These error codes help developers identify the root cause of an error and take appropriate action to resolve it.
The following table lists the error codes defined in the DIDError
class:
Error Code | Description |
---|---|
|
The provided DID is invalid or malformed. |
|
The provided DID URL is invalid, malformed or not supported. |
|
The provided DID method is not supported. |
|
The provided DID representation is not supported. |
|
The provided public key is invalid or malformed. |
|
The provided public key has an invalid length. |
|
The provided public key type in invalid. |
|
The provided public key type is not supported. |
|
An internal error occurred while processing the request. |
|
The requested resource was not found. |
|
The provided signature is invalid or malformed. |
|
The provided multibase encoding is invalid or not supported. |
|
The provided argument is invalid or missing. |
The ErrorCodes
enumerated type can be imported from the @hashgraph-did-js-sdk/core
package to access the error codes in your application.
Error handling
When using the Hashgraph DID SDK, it is essential to implement robust error handling to gracefully handle exceptions and errors. Proper error handling ensures that your application can recover from errors, provide meaningful feedback to users, and prevent unexpected behavior.
The following best practices can help you handle exceptions and errors effectively:
-
Use try-catch blocks: Wrap code that may throw exceptions in try-catch blocks to catch and handle errors gracefully.
-
Check error codes: Use the error codes provided by the
DIDError
class to identify the type of error and take appropriate action. -
Catch other errors: Remember that errors not derived from
DIDError
may still occur. Catch these errors and handle them accordingly. -
Implement fallback mechanisms: Define fallback mechanisms to handle errors that cannot be resolved immediately, such as retrying the operation or displaying a user-friendly error message.
The following example demonstrates how to handle exceptions and errors when resolving a DID:
try {
const didDocument = await resolveDID(did);
console.log(didDocument);
} catch (error: unknown) {
if (error.isDIDError) {
const didError = error as DIDError;
console.error(`Error resolving DID: ${didError.description} (${didError.code})`);
} else {
console.error(`An unexpected error occurred: ${error}`);
}
}
Next Steps
-
Explore
resolveDID
: Dive deeper into the resolveDID function to understand its parameters, error handling, and advanced usage. -
Manage DIDs: Learn how to use createDID, updateDID, and deactivateDID to effectively manage DIDs on Hedera.
-
Implement the
Signer
: Practice generating key pairs, signing messages, and verifying signatures using the Signer class. -
Utilize the
Publisher
: Integrate the Publisher class into your application for seamless transaction submission.