ERPNext API - Error not shown properly in api response

Eg: Login API
If i pass correct user name & password, the response we get a json as below

{“home_page”:“/desk”,“message”:“Logged In”,“full_name”:“XXX YYY”}

But if incorrect user & password is passed, the response is html page as below. I am not sure how to capture the error and show to the user in my app which uses this API.

<div class='page-card'>
	<h5 class='page-card-head'>
		<span class='indicator red'>
			Session Expired</span>
	<div class="page-card-body">
		<p>Your session has expired, please login again to continue.</p>
			<a href='/' class='btn btn-primary btn-sm'>
<p class='text-muted text-center small' style='margin-top: -20px;'>Status: 401</p>

Same was with other api calls as well, give error like below

frappe.throw("Item quantity can not be zero")↵  File "/home/frappe/frappe-bench/apps/frappe/frappe/", line 352, in throw↵    msgprint(msg, raise_exception=exc, title=title, indicator='red')↵  File "/home/frappe/frappe-bench/apps/frappe/frappe/", line 338, in msgprint↵    _raise_exception()↵  File "/home/frappe/frappe-bench/apps/frappe/frappe/", line 311, in _raise_exception↵    raise raise_exception(msg)↵ValidationError: Item quantity can not be zero


self._validate_mandatory()↵  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/", line 725, in _validate_mandatory↵↵MandatoryError: [Sales Invoice, INOXNSA10I00001]: items↵

Error is not in consistent format in all the cases, the tags are different. I would not be able to write a function to parse this api response and get the actual error to show it to user.

Pls Help

Can anybody help here please?

i had this issue when working with frappe as backend only api
one solution is to create your own file in your custom app
and the other solution is to override the way frappe return response .
in my case i only need few custom apis since i use frappe without erpnext as backend …
but if you need all the apis you need the second option …
— this is from my experience maybe other developers have better solution than mine –