Improve GraphQL error handling by mapping all DataApiBuilderException types to appropriate HTTP status codes#2692
Draft
Improve GraphQL error handling by mapping all DataApiBuilderException types to appropriate HTTP status codes#2692
Conversation
…LResultSerializer Co-authored-by: Aniruddh25 <3513779+Aniruddh25@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Inspect all DataApiBuilderExceptions to determine the correct status code of HotChocolate responses
Improve GraphQL error handling by mapping all DataApiBuilderException types to appropriate HTTP status codes
May 19, 2025
Contributor
|
/azp run |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
JerryNixon
approved these changes
Sep 4, 2025
This comment was marked as resolved.
This comment was marked as resolved.
…ve unit tests Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
Contributor
Author
I've addressed the actionable feedback from your review in commit 7c04624: ✅ #3 Tests Added: Created comprehensive unit test suite with 31 tests covering:
✅ #4 Switch Expression: Refactored to use idiomatic C# switch expression with pattern matching for cleaner, more maintainable code
If there's a newer main branch or reference implementation showing the middleware approach, please share the branch name so I can rebase and adapt the changes accordingly. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why make this change?
Issue #2691 requests that all
DataApiBuilderException.SubStatusCodesshould be inspected to determine the correct HTTP status code for GraphQL responses, rather than only handlingDatabaseInputError. Previously, the serializer would returnBadRequest(400) for database input errors but default to server error (500) for all other exceptions, making it difficult for clients to distinguish between different error types.What is this change?
This PR enhances the
DabGraphQLResultSerializerclass to properly map all types ofDataApiBuilderException.SubStatusCodesto their appropriate HTTP status codes in GraphQL responses using a clean switch expression pattern.The serializer now maps:
Implementation Details:
MapSubStatusCodeToHttpStatusCodemethod with pattern matchingThis implementation ensures that GraphQL responses have the appropriate HTTP status codes that match the nature of the error, which improves API consistency and allows clients to better handle different error types.
How was this tested?
Test Coverage:
DabGraphQLResultSerializerTests.csSample Request(s)
When a GraphQL query encounters an authorization error:
Before: HTTP 500 Internal Server Error
After: HTTP 403 Forbidden (when
AuthorizationCheckFailedSubStatusCode is set)When a GraphQL query references a non-existent entity:
Before: HTTP 500 Internal Server Error
After: HTTP 404 Not Found (when
EntityNotFoundSubStatusCode is set)💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.