Method Responses
Overview
This guide explains how to handle responses from methods in the Growave SDK. Managing successful results and handling errors effectively is crucial for building reliable, user-friendly applications that interact with Growave's services.
Handling SDK responses
When you interact with the Growave SDK, methods typically return a Promise that can either resolve to a success object or reject with an error object. Properly handling these responses ensures that your application gracefully manages both expected and unexpected outcomes.
Success and error responses
The Promise returned by an SDK method requires handling both potential outcomes: success and error. You can use .then() method to to manage success and .catch() method for errors.
Example: Handling success and error responses
window.gw.sdk.someModule
.someMethod()
.then((response) => {
console.log("Operation successful:", response);
// Handle success, e.g., update the UI
})
.catch((error) => {
console.error("An error occurred:", error);
// Handle error, e.g., show an error message to the user
});
Response structures
-
Success response: When the operation is successful, the
Promiseresolves with an object that typically contains a status.{status: "success"; // Indicates that the operation was successful} -
Error response: If the operation fails, the
Promiserejects with an error object containing a code and message.{code: "errorCode", // Indicates the type of errormessage: "Detailed error message" // Provides more context about the error}
Common error codes
Understanding the error codes returned by the SDK helps you diagnose and respond to issues appropriately.
Error codes
-
generalError: Thrown when an error occurs on the application side, not on the SDK side.{code: "generalError",message: ""} -
insufficientArgs: Thrown when a method is called with insufficient required arguments, preventing execution.{code: "insufficientArgs",message: "Failed to execute 'addToWishlist' on 'sdk': 1 argument required, but only 0 present."} -
noSubscribers: Thrown if the method has no subscribers, preventing method execution.{code: "noSubscribers",message: "Method execution failed: no subscribers found."} -
timeoutError: Thrown if aPromiseremains unresolved after 15 seconds, triggering a timeout error.{code: "timeoutError",message: "The operation did not complete within 15 seconds. Please try again."}
Conclusion
Understanding how to handle method responses in the Growave SDK is essential for building robust applications. By handling both success and error responses within the same block of code, you create a more reliable and user-friendly experience. Always ensure your application is prepared to manage any outcomes from SDK methods and integrate these responses seamlessly into your application's logic.