How To Fix an Internal Server Error in Flask

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is miguel greenberg and today we are going to talk about internal server errors in flask this is a question that i get a lot many times beginners when they're following a tutorial a flask tutorial they get this internal server error and they ask me how to fix it how to fix an internal server error so what i'm going to do in this video is i'm going to try to give you a few pointers so that you understand why this error occurs and then what to do next as a step in fixing the error in the application so as an example here i have this is the the application from the flask mega tutorial and uh here let's say i'm i'm playing with the application and everything seems to work uh and then i decide to check out the explore link and when i click there boom i get the dreaded internal server error so beginners are very confused about this because this is an error that doesn't give you any information this is a a very terrorist screen there is zero details about what happened you just know that something didn't work so what you need to keep in mind is that flask and many other web frameworks use the internal server error sort of an umbrella error that catches any conditions or problems that occur with your application so flask doesn't know who's using the application it might be a user it it doesn't know that it is you the developer so just as a as a precautionary measure it doesn't show any details about the application on the web browser and this is why you get this screen that doesn't give you any information uh all that you know is that something failed so what do you do when you get this what you need to do is you need to go to the terminal session in which you are running the flask server so i'm going to go there right now and here you can see that in the log from the flask server there is a stack trace and this is the actual error this is the error that caused the internal server error page to show up on the web browser so really to fix the internal server error what you need to do is you need to research this stack trace and figure out what failed in the application so in general i start reading python stack traces from the bottom and here in the last line you can see exactly what happened here i'm trying to access the arg attribute from the request object and obviously i made this on purpose the uh the attribute is called args in plural and because of this typo i get there so what i will do now is i would edit this source file and you can see the uh the source file right here so this is this is the last coming coming from the bottom this is this is actually the first from the bottom the first of the files mentioned in the stack trace that is in the application you can see that uh the previous one is inside versoy which is a flask dependency so you look for your source code you look for the line and you can see in this line i'm doing request.org instead of request.args which is what i should be doing so so i will go edit this line 54 in routes pi and then everything will work so i want to show you one more thing uh you may find this inconvenient if you are the developer you're working on your application uh you're doing everything yourself in your own computer uh you may find it inconvenient that you need to switch between the browser and the terminal whenever an error happens so flask has a mode in which it can show you this this same information in the web browser this is called the debugger the flask debugger so let's go ahead and turn that on so that you can see how that looks uh it is a little bit more uh convenient actually so to do that you need to set an environment variable that tells flask that you want debug mode enabled so the variable is called flask m and i'm going to set it to development and this is what sets debug mode so if you are on a mac or unix machine you could do it like this this sets the environment variable for for the process that you're running here uh if you prefer to set it permanently you can do export flux flashcam equals development and then run flask run separately in the next line and then finally if you are on windows you will do set flashcam equals development and then after that you you run flask run on a separate command so either way they all are going to end up defining this variable when flask run starts and now you can see that when i run this i get debug mode on this indicates that the debugger is enabled so let's go back to the browser let's go to homepage and let's click explore one more time and now i get this nicer output which of course this should only be done when you are developing your application locally never ever ever enable this on a application that is deployed for people to use so never when you have this on the internet this is actually very dangerous it may allow an attacker to run code on your server so under any simple sense never enable this uh it did happen there's been attacks for servers that have this enabled you can google it you you'll find information about very popular very well-known companies that had this problem in the past so just keep that in mind but anyway when you do it locally this is totally safe and here you can see the stack trace presented in a nicer format uh you can see the same lines in the stack trace the error the actual errors mentioned at the bottom so it's all the same the additional things that you can do here for example let's find so so this is the line of the error so i can open the code that allows you to see a little bit of the code you can see some context and also you have this terminal option you can open a terminal that if for for the same reason of security is locked with a pin so i'm going to go back to my terminal and this needs to be done once i'm going to take the pin from the the flash ground command and enter here just paste it and now once that is enabled i can open a terminal session here and for example i can i can query things so request is my request object request.arg is not available i get another stack trace and if i do request.args i get the correct output which is what i should have done in this code um so these are the two options when you are developing so what do you do when you have a flask application deployed for production obviously you're not going to enable this so on your production version the user if it hits an error is going to see either the internal server error page or maybe a customized page because flask allows you to customize the internal server error page as well as many other errors so you can create your own page for for this error and all the others uh but that's that's all they're gonna they're going to see they're not going to see any information about the error so what you need to do is you need to make sure that the output that goes normally out on the terminal so this output that you that you get on the terminal when you run for development uh that needs to go to a log file so you are going to have a log file and then whenever there is an error the the stack trace will be logged so you can have the log file to go check for errors in addition to that you can do some fancier things like for example send yourself an email whenever there's an error and include the stack trace in the email you can do that too so i'm not gonna do that in this video but uh but my flask mega tutorial does cover all these options you you are going to learn how to set up your logging for production and also how to get an email if the application fails in production so anyway i hope this helps reduce the number of people who ask me how to fix an internal server error hopefully everything is more clear now and you will be able to debug these errors on your own and figure out what the mistake is thank you so much see you on the next video bye
Info
Channel: Miguel Grinberg
Views: 34,573
Rating: undefined out of 5
Keywords:
Id: p_so34WgnyY
Channel Id: undefined
Length: 10min 18sec (618 seconds)
Published: Thu Aug 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.