Skip to content

Correcting behaviour of BadRequests returning InternalServerError #783

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

Conversation

ayush3797
Copy link
Contributor

@ayush3797 ayush3797 commented Sep 5, 2022

Why make this change?

  • Closes [Fuzzing] HTTP 500 - Insert Operation conflicting with ForeignKey constraint #620
    Previously, for exceptions that were arising because of users sending bad request to the database rather than an Internal Server failure of the database, we were still throwing exception with status code 500 (InternalServerError) rather than (400) BadRequest. This PR changes that behaviour to thrown exception with status code 400.

Useful Links

  1. MsSql Error Codes
  2. MySql Error Codes
  3. PostgreSql Error Codes

What is this change?

We will now have 3 seperate subclasses of DbExceptionParser. The 3 subclasses for MySql, MsSql, PostgreSql will have their own implementation of the method IsBadRequestException(), which depending on the database's way of classifying exceptional conditions via SqlState(MySql/PgSql) or Number(MsSql), will return a boolean value indicating whether the exception is to be considered to have occurred because of a bad request.

Also the severity of Stylecop ruleCA1051 was reduced to suggestion to declare a protected field in DbExceptionParser class.

How was this tested?

  • Integration Tests

@ayush3797 ayush3797 self-assigned this Sep 5, 2022
@ayush3797 ayush3797 changed the title Dev/agarwalayush/insert operation conflict with fk constraint Correcting behaviour of BadRequests returning InternalServerError Sep 5, 2022
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.

Some questions..

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

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 last nit comments to resolve and then looks good!

/// <returns>true/false</returns>
public virtual bool IsBadRequestException(DbException e)
{
return e.SqlState is not null && badRequestErrorCodes.Contains(e.SqlState);
Copy link
Contributor

Choose a reason for hiding this comment

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

mentioned this in another comment, but I think that comment might have been buried from updates. If MsSql is the only case here e.SqlState can be null, and MsSql never calls into this function, do we still need the null check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think SQLSTATE will be null in mysql,pgsql but as a defensive approach I am putting this null check because the string SQLSTATE is nullable in declaration.

@ayush3797 ayush3797 merged commit 8329455 into main Sep 11, 2022
@ayush3797 ayush3797 deleted the dev/agarwalayush/insertOperationConflictWithFKConstraint branch September 11, 2022 09:06
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.

[Fuzzing] HTTP 500 - Insert Operation conflicting with ForeignKey constraint
5 participants