How to get a standard HTTP error status code from error of type "ODataV4Format" ?

Deepak Agarwal 20 Reputation points
2025-03-04T08:41:51.8833333+00:00

Hi,

I am working on translation of document which is kept in Azure storage. I am using asynchronous processing with DocumentTranslationClient class. Code is pretty much standard as per documentation as below -

poller = client.begin_translation(source_url, target_url,
                                                  self.translation_language,
                                                  storage_type="File")
                result = poller.result(timeout=120)


I am getting an exception here as

HttpResponseError('(InvalidDocumentAccessLevel): Cannot access source document location with the current permissions.')

I understand the error is of format ODataV4Format hence the actual status HTTP status code is masked somewhere.

We have a common Exception handling framework which works on status code and based on which there is some business logic.

Queries -

  • How can I extract the actual error HTTP status code from the exception?
  • What is the standard framework which Microsoft suggests to handle exception messages where you may or may not get HTTP status codes (consider a common framework leveraging multiple Azure services of different categories). Thanks!
Azure AI Language
Azure AI Language
An Azure service that provides natural language capabilities including sentiment analysis, entity extraction, and automated question answering.
472 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Manas Mohanty 1,695 Reputation points Microsoft External Staff
    2025-03-04T10:38:42.2033333+00:00

    Hi Deepak Agarwal

    Here is the answer to your queries.

    How can I extract the actual error HTTP status code from the exception?

    You can find and map the respective error codes details from here.

    What is the standard framework which Microsoft suggests handling exception messages where you may or may not get HTTP status codes (consider a common framework leveraging multiple Azure services of different categories). Thanks!

    1. In most of failed events, it shows some response code, in case it is a long-awaited or stuck job/operation, you can drill down into activity log of resource or log analytics workspace which is recording all intermittent or failed events from past.
    2. You can take a network trace for Virtual network cases to find failed events.
    3. We can add --verbose or --debug in CLI commands to get the full details
    4. We can use logging library in Python SDK to log the error message in clean way.
    5. We can also use try/catch block to find the error message.
    for document in result:
        print(f"Document ID: {document.id}")
        print(f"Document status: {document.status}")
        if document.status == "Succeeded":
            print(f"Source document location: {document.source_document_url}")
            print(f"Translated document location: {document.translated_document_url}")
            print(f"Translated to language: {document.translated_to}\n")
        elif document.error:
            print(f"Error Code: {document.error.code}, Message: {document.error.message}\n")
        else:
            print("The document status is unknown or there is no status code. Please check in activitiy logs/log analytics workspace\n")
        poller = client.begin_translation(source_url, target_url, self.translation_language, storage_type="File")
        result = poller.result(timeout=120)
    except HttpResponseError as e:
        status_code = e.status_code
        print(f"HTTP Status Code: {status_code}")
        print(f"Error Message: {e.message}")
    
    

    Hope it addresses your query.

    Thank you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.