Skip to content
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

fix: structure api error messages #214

Closed
wants to merge 10 commits into from

Conversation

Geeker1
Copy link
Contributor

@Geeker1 Geeker1 commented Mar 2, 2021

Description

Structuring error messages from api to users using django-rest custom exception handler.

Related Issue

#214

How to test it locally

docker-compose run --rm -e DJANGO_CONFIGURATION=Test web pytest /app/tests/test_utils.py /app/tests/test_error_handling

Changelog

Added test for custom exception handler function
Replaced Custom Renderer with Exception Handler

Added

Updated

Removed

Checklist

  • 🚀 is the code ready to be merged and go live?
  • 🛠 does it work (build) locally

Pull Request

  • 📰 good title
  • 📝good description
  • 🔖 issue linked
  • 📖 changelog filled out

Commits

  • commits are clean
  • commit messages are clean

Code Quality

  • 🚧 no commented out code
  • 🖨 no unnecessary logging
  • 🎱 no magic numbers
  • black was run locally (as part of the pre-commit hook)

Testing

  • ✅ added (appropriate) unit tests
  • 💢 edge cases in tests were considered
  • ✅ ran tests locally & are passing

Copy link
Contributor

@milafrerichs milafrerichs left a comment

Choose a reason for hiding this comment

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

good start, but some wrong turns that can be fixed.

you should add unit tests to test_utils.py and rename the test_response to test_error_handling or so.

here are some resources for testing Django with DjangoTest plus and DRF:

https://www.django-rest-framework.org/api-guide/testing/
https://www.revsys.com/tidbits/django-test-plus/

wazimap_ng/config/common.py Outdated Show resolved Hide resolved
tests/test_response.py Outdated Show resolved Hide resolved

def error_handler(request, **kwargs):

if request.path.startswith('/api/v1'):
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of using the path you should rather look for the content_type and if it's json return json otherwise leave as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this fix is to structure all error messages in json, I'll remove the if statement and render all 500 error responses in json.

Copy link
Contributor

Choose a reason for hiding this comment

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

no, the issue was that all API requests should respond with JSON.
Not all requests. Otherwise the Admin will show JSON errors and the DRF preview potentially as well.

That's why we need the check for content type here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. During test , I had to assign content_type to underlying request object so function could be tested without errors.

wazimap_ng/utils.py Outdated Show resolved Hide resolved
wazimap_ng/utils.py Outdated Show resolved Hide resolved
wazimap_ng/utils.py Outdated Show resolved Hide resolved
wazimap_ng/utils.py Outdated Show resolved Hide resolved
factory = RequestFactory()
request = factory.get('/api/v1/jkhj')

response = error_handler(request)
Copy link
Contributor

Choose a reason for hiding this comment

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

you're headed in the wrong direction here.
you want to test the result of the exception raised by DRF and django and not call your own error_handler

@milafrerichs milafrerichs linked an issue Mar 12, 2021 that may be closed by this pull request

def test_custom_exception(self):
response = self.get('/api/v1/datasets/3452/')
Copy link
Contributor

Choose a reason for hiding this comment

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

this is the integration test and should go to test_error_handler

def test_error_json(self):
request = self.get('/foo/bar')

response = error_handler(request)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a unit test since it's calling the error_handler method and should go in test_utils.py

}
}

response = self.get('dataset-detail', pk=234)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is the correct test for the 404.
thanks. we need a similar one for the 500

Copy link
Contributor Author

@Geeker1 Geeker1 Mar 13, 2021

Choose a reason for hiding this comment

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

During testing, django raises the exception inplace of calling the handler500 function which returns the json response. I wrote the unit test for the handler500 function which is the test_error_handler_func, should I write the live server test case for 500 error ?

wazimap_ng/utils.py Show resolved Hide resolved

response = self.get('dataset-detail', pk=234)

data = json.loads(response.content)
Copy link
Contributor

Choose a reason for hiding this comment

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

take a look at the docs from DRF: https://www.django-rest-framework.org/api-guide/testing/#checking-the-response-data

you can use .data instead of .response

Copy link
Collaborator

@goyal1092 goyal1092 left a comment

Choose a reason for hiding this comment

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

@Geeker1 I am not able to see error message on custom api functions like all_detail api end point if I pass invalid porfile id

the solution will work only DRF views but we want to implement it all api endpoints including cutsom api_views

@adieyal
Copy link
Contributor

adieyal commented Jun 6, 2021

What is the status with this PR - I see it's still in draft - is it still being worked on?

@Geeker1
Copy link
Contributor Author

Geeker1 commented Jun 7, 2021

What is the status with this PR - I see it's still in draft - is it still being worked on?

Yes, it is. Will soon push updates

@paulmwatson paulmwatson closed this Nov 2, 2023
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.

API should always respond with json data
5 participants