Simulate server error - unit-testing

I use the App Engine for run my application and want to test how it will handle server errors.
Is there any possibility to simulate an error 500 via the WebTest ?

I got around it using a try except loop.
try:
self.testapp.get('/')
self.assertEqual(1, 2, 'GET request should have resulted in a 405 error') # Purposely fail
except webtest.app.AppError:
pass
Another way is the following:
self.assertEqual("500 Internal Server Error", self.testapp.post('/', params={}, expect_errors=True).status, 'POST Request should have resulted in a 500 error')
Both methods still will cause traceback to appear but the test passes

A 500 error is just what your webapp returns to the client when it gets an uncaught exception. It's not a specific failure - just what it shows to your users when something unexpected goes wrong. Instead, you should unit-test your handlers to ensure they act as expected.

Related

500 error vs 404 error which one is more desirable?

I am a student who is studying the development of django.
I have a question about 404 and 500 errors.
I handle 404,500 error as 404.html and 500.html respectively.
So, is there a difference between these two error events?
For example,
def example_post_404(request, pk):
get_object_or_404(Post, id=pk) # code that may occur 404 error
vs
def example_post_500(request, pk):
Post.objects.get(id=pk) # code that may occur 500 error
Did 500 error event put more pressure on the server than 404 error event?
Which code is more desirable?
My code is running on AWS EC2 ubuntu-16.04
The difference between any 400 error versus a 500 error is based on whether it is the fault of the client or the server that the request was not parsed.
for instance, a 404 error means the object was not found; what does this mean? it means that based on what the client was asking for, the server could not return a result.
another example, a 503 Service Unavailable; the server recieved the reponse, and although the client request is valid the server was unable to provide the response.
That is the difference between a 4XX error and a 5XX error, if you'd like more detailed information on what to respond with when an error occurs, please refer to the HTTP documentation:
https://www.rfc-editor.org/rfc/rfc2616
404 is page not found
500 is an internal system error
If a user requested a page/url that does not exist, return 404. If something happens to your system (like a bug, an unexpected error), return 500.
404 Error is Page not found to url calling.
500 is an internal error for our system(like Django un-authenticate user access)

How to ignore flasks http 400 exception in Sentry

I have setup a flask project in Sentry but have noticed a problem that I need to fix.
Currently if the flask application throws a HTTPException (for example for a validation exception) that exception creates an issue in Sentry. This clutters up the issues since it creates issues even for HTTP 400.
Are there any way to configure Sentry so it ignores all HTTPExceptions with code 4xx but still create Issues for all HTTPExceptions with code 5xx?
If I’m not mistaken, Sentry should only send not handled exceptions. So you could setup custom error handlers: http://flask.pocoo.org/docs/1.0/patterns/errorpages/?highlight=error%20handler#error-handlers
If you want to handle all default exceptions you could register an error handler like this:
from werkzeug.exceptions import default_exceptions
def register_error_handlers(app):
""" Register error handler for default exceptions """
for code in default_exceptions:
app.register_error_handler(code, your_error_handler)
where app is your flask App instance and your_error_handler takes the error as argument and returns a response. So you could filter for the 400 codes in this for loop to only handle 4xx errors.

How can I get a detailed log of django csrf failure?

I am troubleshooting a Django app.
Recently the app seems to Randomly generate CSRF verification errors:
CSRF verification failed. Request aborted.
(Resulting in a 403)
Where can I find detailed information on the cause of the verification failure?
The error you're seeing is on the client side - which won't by default know what's going wrong on your server unless you have set DEBUG = True (which you don't want to do in production).
If it was replicable on your staging server it would be easy to fix, since you could replicate the error with DEBUG = True on staging and quickly see where the verification fails in Django's csrf.py.
What you're looking for is the output of which of these error is occurring on your server.
If you implement logging in Django you'll be able to investigate and determine which of these errors was triggered on your production site. A service like Sentry makes this even simpler since it will send you the traceback anytime an error happens.

Debuging of 500 http response

I need to fix some issues on an application written in Tornado. It is REST canvas app which use socket to communicate with the server. Sometimes it generates 500 error when user tries to logout during some nodes' loading. I tried to replace 500 response with 402 type by using Try block in certain handler, but I am still getting 500 in the client. So I need to get exact line of Python code that generates error. Unfortunately I am not sure how to check those as all my browser is showing is 500 error, that's it.
If you start your application from the command line and then trigger the HTTP 500 error, you should see a traceback written to the console. Or at least it will appear in the log file.
Or, update the code that creates the tornado.web.Application instance in your application's main file, and pass debug=True to the Application() constructor. That turns on several options including serve_traceback. Then you should see a full traceback in the error response, not just a "500".

Debug transient 404 errors

I sometimes get http 404 errors in my client on a certain url on my tastypie API.
However, when I access it manually via my browswer, it works all the time with the exact URL that the client is requesting.
This is a GET call with some query parameters:
https://example.com/v2/xxx?limit=&offset=&timestamp=
How can I debug this?
UPDATE 1
It looks like the error is returned by tastypie because the response body is {"error_message": "Sorry, this request could not be processed. Please try again later."}
UPDATE 2
It seems that it is a NOT_FOUND_EXCEPTIONS in tastypie resources.py, but how can I debug a transient problem? The data is pretty static and does not change.