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>
	</h5>
	<div class="page-card-body">
		<p>Your session has expired, please login again to continue.</p>
		<div>
			<a href='/' class='btn btn-primary btn-sm'>
			Home</a>
		</div>
	</div>
</div>
<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/__init__.py", line 352, in throw↵    msgprint(msg, raise_exception=exc, title=title, indicator='red')↵  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 338, in msgprint↵    _raise_exception()↵  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", 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/document.py", line 725, in _validate_mandatory↵    name=self.name))↵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?

hi
i had this issue when working with frappe as backend only api
one solution is to create your own api.py 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 –