APIs & Services
...
Error Handling
Union Errors
10 min
introduction to union types in graphql graphql's union types allow fields to return one of several different types of error responses this is particularly useful when a query might return a successful result or various types of errors, such as validation or generic errors key components of the union type implementation success type represents the normal, successful response example organization type containing the organization details error types validationerror includes details about validation issues, such as missing or invalid fields genericerror handles any generic errors that don’t fall under validation, such as internal server issues usage examples in client side applications to handle union types in client side applications, you can leverage the typename field, which helps identify the exact type of the response handling success and error on the client the migration to union types in your graphql responses significantly enhances the api’s usability, clarity, and error management by leveraging this approach, you can provide a more robust and predictable contract to api consumers, ensuring that both successful and error states are handled consistently error structure generic error validation error generic errors when an api request fails, the api gateway can return a genericerror to provide essential information about the failure this type of error is used for system level issues such as server malfunctions, internal failures, or when the request cannot be processed due to infrastructure related problems the genericerror is designed to give the client a basic but meaningful description of the error, so they can identify the problem and take appropriate action when the generic error filter is configured, the api gateway examines the incoming message and attempts to infer the type of message to be returned query body’s information typename indicates the type of error, which in this case will be genericerror it helps clients distinguish between different error types, such as genericerror vs validationerror errormessage a brief description of the issue that occurred this message helps explain the error, though it may not provide specific technical details errorcode displays the error code correlationid an id used to correlate logs and metrics across services and processes for example lets hit this query in profile api , for an incoming json message, the api gateway sends an appropriate json response request response validation error validation error occurs when a request is malformed or the request failed our validation rules it occurs when the required parameters are missing (either in body or in query string) query body’s information typename it shows the type of error that has occurred, either generic or validation errormessage elaborates the requirement for the input values errorcode displays the error code correlationid the id used to correlate logs and metrics across services and processes errordetails displays the detailed information about the error different types of standard errors for example for example lets hit this query in profile api request response different types of standard errors error type description missing field constraint error due to missing mandatory fields contradictory fields constraint error due to contradictory input fields exceed maximum size constraint error due to exceeding maximum allowed size for a field permission constraint error due to lack of permission to access certain data data not found error when no matching data found based on input invalid input constraint error due to invalid input values in the search query too many results constraint error when too many results are requested per page or too many ids are sent in the query missing required field error due to missing mandatory fields in the input conflicting fields error when both before and after fields are provided simultaneously, which creates a conflict range limit exceeded error when gte exceeds lte or date ranges are invalid (fromdate is after todate) exceeds maximum allowed size error when a field’s value exceeds the allowed limit invalid field type error when fields expecting a specific type (e g , integer, float) contain incompatible values string length requirement not met error when string fields do not meet minimum character requirements boolean type expected error when fields expecting a boolean value contain non boolean inputs important note all graphql queries must be embedded with the below query