Skip to content

Categorizing Conflict error codes #1090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 14, 2023

Conversation

ayush3797
Copy link
Contributor

@ayush3797 ayush3797 commented Jan 10, 2023

Why make this change?

To recognize exceptional conditions like insertion of rows with duplicate key as Conflicting operations (return exception with HttpStatusCode 409), rather than throwing InternalServerError. This is just one example, and we can have other conflicting operations in the database as well.

What is this change?

Added a set ConflictExceptionCodes to DbExceptionParser.cs which will store the error numbers to be considered for returning exception with HttpStatusCode 409.

How was this tested?

Added regression test to InsertApiTestBase.cs class (overridden in each of the child class), which tries to insert a row with duplicate primary key into the database table and thus fails accordingly throwing exception with 409 status code.

Additional Change:

Till this point, the test result verification expected the actual and expected exception messages to be exactly the same. However, the test added in this PR InsertOneTestViolatingUniqueKeyConstraint returned a dynamic exception message changing on every test run for MsSql. For such dynamic cases, we can now have an option isExpectedErrorMsgSubstr which if set to true, will only look for a substring (which remains constant among multiple test runs) in the actual error message, rather than expecting them to be identical.

Sample Request(s)

  1. REST Endpoint: https://localhost:5001/api/commodities/
    Database chosen: MsSql
    Request: {
    "categoryName": "SciFi",
    "pieceid":"1",
    "categoryid": "1"
    }
    Response: {
    "error": {
    "code": "DatabaseOperationFailed",
    "message": "Violation of PRIMARY KEY constraint 'PK__stocks__9CC9595F28BE875B'. Cannot insert duplicate key in object 'dbo.stocks'. The duplicate key value is (1, 1).",
    "status": 409
    }
    }

@ayush3797 ayush3797 added this to the Jan2023 milestone Jan 10, 2023
@ayush3797 ayush3797 self-assigned this Jan 10, 2023
@ayush3797 ayush3797 linked an issue Jan 10, 2023 that may be closed by this pull request
@ayush3797 ayush3797 changed the title Categorising Conflict error codes Categorizing Conflict error codes Jan 10, 2023
Copy link
Contributor

@seantleonard seantleonard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some nits/ suggestions

Copy link
Contributor

@aaronburtle aaronburtle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, just a question, plus a request to use system.text.json if possible

Copy link
Contributor

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the change!

@ayush3797 ayush3797 enabled auto-merge (squash) January 14, 2023 01:28
@ayush3797 ayush3797 merged commit 49ed118 into main Jan 14, 2023
@ayush3797 ayush3797 deleted the dev/agarwalayush/conflictingDbOperations branch January 14, 2023 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Requests returning 409 (Conflict)
4 participants