How to avoid the internal server errors in REST API?

I have api endpoint that returns server side authentication error. But I have my own custom error in understanding way. How to achieve those?

{
  "session_expired": 1,
  "errors": [
    {
      "type": "AuthenticationError",
      "exception": "Traceback (most recent call last):\n  File \"apps/frappe/frappe/app.py\", line 101, in application\n    validate_auth()\n  File \"apps/frappe/frappe/auth.py\", line 609, in validate_auth\n    validate_auth_via_api_keys(authorization_header)\n  File \"apps/frappe/frappe/auth.py\", line 675, in validate_auth_via_api_keys\n    validate_api_key_secret(api_key, api_secret, authorization_source)\n  File \"apps/frappe/frappe/auth.py\", line 702, in validate_api_key_secret\n    raise frappe.AuthenticationError\nfrappe.exceptions.AuthenticationError\n"
    }
  ]
}

But I need to return my own error code

  return {
                "status": "error",
                "message": _("Unauthorized. API secret is incorrect."),
                "code": 401
            }