Category: Question and Answers
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

GraphQL Error Handling

Issue

I am using a GraphQL API that always returns 200 status codes even for errors. However, when I try to specify an onError handler for an “Add New Row” action on a table widget in Appsmith, it doesn’t trigger the onError handler if the request returns an error. I am looking for a workaround to handle errors in this scenario.

Resolution

The issue seemed to be that the GraphQL API was returning an error message instead of a failure status code, which is why Appsmith's onError handler was not being triggered. To create a more customized error message, we can use a JavaScript function to parse the error message and display it in a more user-friendly manner. Additionally, we can use Altair GraphQL to execute the same query and observe the response from our API to better understand any potential issues. Here is an example of how we can handle the error message using JavaScript:

const handleAddRowError = (error) => {
const errorMessage = error?.response?.data?.errors?.[0]?.message || 'Unknown error';
alert(`Error adding new row: ${errorMessage}`);
}

In this example, we are checking the response data for any error messages and displaying them in an alert. We can then call this function in our onError handler for the "Add New Row" action on our table widget. Additionally, using Altair GraphQL can help us debug any potential issues with our API response and ensure it is properly returning the expected data.