Manas Solanki
Jul 6, 2017 · 4 min read

Testing and debugging is an integral part of software development cycle. Fixing bugs improve the stability of the software but sometimes it can be quite cumbersome. So here I will be talking about some cool debugging technique the particularly for the Frappe framework perspective, but most of these can also be applied for debugging other frameworks written in Javascript and Python. I will be assuming that you will have a basic idea of the frappe framework, Python and Javascript.

We will start from the front end side ie. Javascript and then cover the python accordingly. I will be using Chrome as the browser, same can be applied for all the browsers.


First is the most basic method for finding the value of any javascript variable i.e. console.log(variable_name); The value can be found printed on the console of the developer tools of the browser when you refresh the browser.

Now assume you are in the code written by someone else and you want to find out how exactly the function call is happening, so you can use console.trace(variable_name); in that case, and the value with the whole stack trace of the function calls can be viewed in the browser console.


Now suppose this seems boring and tedious to you… changing scripts… refreshing the browser… evaluating the code… repeat again… You want to go one step further and see how the things are happening in the real time. As Javascript is asynchronous and sometimes ajax call makes it harder to understand things, so the given Chrome debugging tool makes it much easier.

debugger;

Chrome Dev Debugging Tool

Whenever the program is being executed and debugger statement comes the execution is stopped, now you can play the execution, or can jump over/into the next function call, put the breakpoints in your code. And the most interesting part is that the value of the local variables can be seen just hovering the mouse over it.


Sometimes what happens that there isn’t any issue in the front end and the bug lies in the back end itself. Now for debugging the backend, you need to print the values in the server console. so you can simply write the print (variable_name) in your Python code for that.

But what if you need to know how the execution is going on and something like the javascript debugger. For that, you can use the python built-in debugging tool pdb. You can import the pdb and can use the traceback method for the debugging.

import pdb#use this statement where you want a breakpoint in your code.
pdb.set_trace()

Now whenever the set_traceback is reached the interpreter stops the execution and gives control to the debugger. Then you can navigate accordingly. But if you are using frappe you can use one shortcut to find out the function call stack. You can just simply use frappe.throw("Anything here") This will print the full traceback in the bench console as Frappé automatically handles this exception.

For ex. if frappe.throw("This will give the full traceback") statement is used in the item.py file, a traceback as given below can be found in the bench console.

Traceback in the bench console from a frappe.throw() statement

Now maybe you are thinking how will I find whether the issue is because of front-end script or backend. so you can always cross check requests to find out whether you are sending the wrong request from the client side, or getting wrong data from the backend, or getting the right data but mishandled in the front end.


What if your logic is correct in both backend and frontend but somehow the data in the database isn’t getting stored properly. In this case, you can always access the database via bench --site <sitename> mysql . This will give you access to MySQL console and you can run MySQL queries there. If you don’t feel comfortable in MySQL queries you can also access data via the bench --site <sitename> console . This will open up Python console for you. You can get and manipulate data there via the frappe database API

Accessing MySQL/MariaDB console via bench commands
Accessing Python console via bench commands

Hope above given tricks and methods will help you to find out the problem more quickly and give you insights for any code. If you can’t still find out the solution you can always post your query on https://discuss.erpnext.com/

Please comment below if you know some cool trick for debugging.

Happy Programming…!!!

Frappé Thoughts

On Open Source, Design, Development and Community. By the makers of ERPNext.

Manas Solanki

Written by

Sr. Software Developer at Deepflux, Ex-Elucidata, Ex-ERPNext & Frappe Core Developer

Frappé Thoughts

On Open Source, Design, Development and Community. By the makers of ERPNext.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade