Errors and error codes for REST API calls

Hello,

I’m developing an app to use ERPNext by consuming the REST APIs. I’m able to call the APIs as necessary, but the problem is that when an invalid parameter is used (e.g. incalid password for the login API), the API doesn’t send a proper error with a code in the response. Alternatively, it sends an HTML file (which I believe is the web page displayed on the UI of ERPNext) that includes the error inside it. I even tried to find a pattern to locate the error but unfortunately, the HTML file doesn’t seem to be parsable.

Am I not looking at the right place? And has anyone figured out a way to work around this?

You can use the structure like below:

try:
    response = {}
    response.update({"status_code": 200})
except Exception as e:
    http_status_code = getattr(e, "http_status_code", 500)
    response["status_code"] = http_status_code
finally:
    return response

HTTP response status codes

Thanks @abhijit_kumbhar. This code will only tell me if there was an error, it won’t give me the reason for the error (e.g. “Not permitted” or “Item code does not exist”…etc.).

Is there no way to extract the error message or error core from the HTML file in a structured way?