Resolving Internal Error on Badgeyay
Badgeyay is in development stage and is frequently seen to encounter bugs. One such bug is the Internal Server Error in Badgeyay.
What was the bug?
The bug was with the badge generator’s backend code. The generator was trying to server the zip file that was not present. After going through the log I noticed that it was because a folder was missing from Badgeyay’s directory.
I immediately filed an issue #58 which stated the bug and how could it be resolved. After being assigned to the issue I did my work and created a Pull Request that was merged soon.
The Pull Request can be found here.
Resolving the bug
With the help of extensive error management and proper code and log analysis I was able to figure out a fix for this bug. It was in-fact due to a missing folder that was deleted by a subsequent code during zipfile/pdf generation. It was supposed to be recreated every time it was deleted. I quickly designed a function that solved this error for future usage of Badgeyay.
How was it resolved?
First I started by checking if the “BADGES_FOLDER” was not present. And if it was not present then the folder was created using the commands below
if not os.path.exists(BADGES_FOLDER): os.mkdir(BADGES_FOLDER)
Then, I added docstring to the remaining part of the code. It was used to empty all the files and folder inside the “BADGES_FOLDER”. We could have to delete two things, a folder or a file.
So proper instructions are added to handle file deletion and folder deletion.
for file in os.listdir(BADGES_FOLDER): file_path = os.path.join(BADGES_FOLDER, file) try: if os.path.isfile(file_path): os.unlink(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception: traceback.print_exc()
Here “os.unlink” is a function that is used to delete a file. And “shutil.rmtree” is a function that deletes the whole folder at once. It is similar to “sudo rm -rf /directory”. Proper error handling is done as well to ensure stability of program as well.
There were many problems that I had to face during this bug.
- It was my first time solving a bug, so I was nervous.
- I had no knowledge about “shutil” library.
- I was a new-comer.
But I took these problems as challenges and was able to fix this bug that caused the INTERNAL SERVER ERROR : 500 .
- BadgeYay Repository : https://github.com/fossasia/badgeyay
- Pull Request for the same : https://github.com/fossasia/badgeyay/pull/59
- Issue for the same : https://github.com/fossasia/badgeyay/issues/58
- Learn about OS Module : https://docs.python.org/2/library/os.html
- Learn about SHUTIL module : https://docs.python.org/2/library/shutil.html
- Read about Error Handling : https://docs.python.org/3/tutorial/errors.html
- Learn how to delete folder and file in Python : https://stackoverflow.com/questions/6996603/how-to-delete-a-file-or-folder
You must be logged in to post a comment.