mathesar icon indicating copy to clipboard operation
mathesar copied to clipboard

Fix constraints endpoint's error behaviour

Open Anish9901 opened this issue 2 years ago • 5 comments

Fixes #1030

This PR Fixes many small unhandled exceptions and 500s caused by the constraints API endpoint.

Technical details

  • An empty columns array:
{"type":"unique","columns":[]}
  • Response:
{
    "code": 4005,
    "message": "Columns field cannot be empty.",
    "field": null,
    "detail": null
}
  • Invalid "type"
{"type":"foo","columns":[1]}
  • Response:
{
    "code": 4005,
    "message": "Unknown type passed.",
    "field": null,
    "detail": null
}

Checklist

  • [x] My pull request has a descriptive title (not a vague title like Update index.md).
  • [x] My pull request targets the master branch of the repository
  • [x] My commit messages follow best practices.
  • [x] My code follows the established code style of the repository.
  • [ ] I added tests for the changes I made (if applicable).
  • [ ] I added or updated documentation (if applicable).
  • [x] I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

Anish9901 avatar May 31 '22 19:05 Anish9901

This pull request has not been updated in 45 days and is being marked as stale. It will automatically be closed in 30 days if not updated by then.

github-actions[bot] avatar Jul 15 '22 21:07 github-actions[bot]

Codecov Report

Merging #1406 (0e43557) into master (f704629) will decrease coverage by 0.05%. The diff coverage is 72.72%.

@@            Coverage Diff             @@
##           master    #1406      +/-   ##
==========================================
- Coverage   92.26%   92.20%   -0.06%     
==========================================
  Files         139      139              
  Lines        6051     6071      +20     
==========================================
+ Hits         5583     5598      +15     
- Misses        468      473       +5     
Flag Coverage Δ
pytest-backend 92.20% <72.72%> (-0.06%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mathesar/api/serializers/constraints.py 92.04% <71.42%> (-5.06%) :arrow_down:
mathesar/api/db/viewsets/constraints.py 86.66% <100.00%> (+0.45%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us.

codecov-commenter avatar Jul 28 '22 14:07 codecov-commenter

@silentninja I am getting a KeyError while sending this request:

{"type":"unique","columns":[9999]}

I suspect the error is generated by this part of the code https://github.com/centerofci/mathesar/blob/4ccea635e390732823f1294811d6a623ab864043/mathesar/api/serializers/shared_serializers.py#L116-L118 How can I solve this error?

Anish9901 avatar Jul 28 '22 15:07 Anish9901

@silentninja I am getting a KeyError while sending this request:

{"type":"unique","columns":[9999]}

The actual error is from this line

https://github.com/centerofci/mathesar/blob/4716b0fb999b164a7354ad52dc4bb985e7481bfc/mathesar/api/exceptions/mixins.py#L64

The column id 999 does not exist and the queryset throws up an error correctly but when it gets passed to the error handling mixin, it gets handled incorrectly. You would need to fix the error handling mixin to convert ManyRelatedField type errors correctly

silentninja avatar Aug 02 '22 17:08 silentninja

I noticed that you are not using the correct error class when throwing up an error. Each error type should also have a unique error code so that the frontend can use the code to figure out the error that is thrown

Actually, the mathesar.api.exceptions.validation_exceptions module was not added to the codebase while I worked on this issue hence the wrong error class. Sorry for the delay.

Anish9901 avatar Aug 02 '22 19:08 Anish9901

@silentninja I am also thinking of fixing the following bugs in a later PR as they are very closely related and I haven't figured out how to handle the KeyError yet. I am changing the PR description accordingly.

  • Non-existent column id, e.g.:

    {"type":"unique","columns":[9999]}
    

and

  • Attempting to set a unique constraint by supplying the id of a column that exists in a different table from the table whose id is supplied in the URL.

Anish9901 avatar Aug 17 '22 06:08 Anish9901

I am also thinking of fixing the following bugs in a later PR as they are very closely related and I haven't figured out how to handle the https://github.com/centerofci/mathesar/pull/1406#issuecomment-1198305111 yet

Okay cool, just create an issue for it before you start working on it so that I can point you to the source of the problem.

silentninja avatar Aug 23 '22 16:08 silentninja

Ah, it didn't occur to me that I should define specific exception classes instead of already written once.

Anish9901 avatar Aug 24 '22 06:08 Anish9901

Thanks for the PR @Anish9901, I made some changes to error classes as it was a small fix and merged the PR.

silentninja avatar Aug 24 '22 07:08 silentninja