HTTP Get Response Error Capture- GCP Workflows

I have requirement to load data from BQ to PostGreSQL using GCP Workflows.

Basically i am doing a HTTP Post call to PostGreSQL and invoking "importContext" method to perfrom the import operation.

steps:
- callImport:
    call: http.post
    args:
      url: ${"https://sqladmin.googleapis.com/v1/projects/" + projectid + "/instances/" + instance + "/import"}
      auth:
        type: OAuth2
      body:
        importContext:
          uri: ${file}
          database: ${databaseschema}
          fileType: CSV
          csvImportOptions:
            table: ${importtable}
            columns : [a,b,c,b]
    result: operation

In my flow i am calling HTTP GET method to check if the import operation response is DONE , strangely in case of any ERROR while import the response is getting status code as DONE along with the Error tag in the body response .

Successful body Response:

{
   "body":{
      "endTime":"2023-07-26T12:15:55.629Z",
      "importContext":{
         "csvImportOptions":{
            "columns":[strings
            ],
            "table":"table_name"
         },
         "database":"postgres",
         "fileType":"CSV",
         "kind":"sql#importContext",
         "uri":"gs://workflow/2023-07-26000000000000.csv"
      },
      "insertTime":"2023-07-26T12:15:44.791Z",
      "kind":"sql#operation",
      "name":"af439df3-21ea-45b2-a92c-a4de00000024",
      "operationType":"IMPORT",
      "selfLink":"https://sqladmin.googleapis.com/v1/projects/",
      "startTime":"2023-07-26T12:15:45.027Z",
      "status":"DONE",
      "targetId":"pricing-dev-master",
      "targetLink":"https://sqladmin.googleapis.com/v1/projects/",
      "targetProject":"project-dev",
      "user":"sa@.iam.gserviceaccount.com"
   },
   "code":200,
   "headers":{
      "Alt-Svc":"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000",
      "Cache-Control":"private",
      "Content-Length":"1179",
      "Content-Type":"application/json; charset=UTF-8",
      "Date":"Wed, 26 Jul 2023 12:16:00 GMT",
      "Server":"ESF",
      "Vary":"Origin, X-Origin, Referer",
      "X-Content-Type-Options":"nosniff",
      "X-Frame-Options":"SAMEORIGIN",
      "X-Xss-Protection":"0"
   }
}

Error response Body :

{
   "body":{
      "endTime":"2023-07-26T10:19:49.527Z",
      "error":{
         "errors":[
            {
               "code":"ERROR_RDBMS",
               "kind":"sql#operationError",
               "message":"generic::failed_precondition: ERROR:  invalid input syntax for type integer: \"2023-07-11\"\nCONTEXT:  COPY table_name, line 1, column id: \"2023-07-11\"\n"
            }
         ],
         "kind":"sql#operationErrors"
      },
      "importContext":{
         "csvImportOptions":{
            "table":"table_name"
         },
         "database":"postgres",
         "fileType":"CSV",
         "kind":"sql#importContext",
      },
         "uri":"gs://workflow/2023-07-26000000000000.csv"
      "insertTime":"2023-07-26T10:19:38.945Z",
      "kind":"sql#operation",
      "name":"375f1c18-c2ac-4b1e-800c-650f00000024",
      "operationType":"IMPORT",
      "selfLink":"https://sqladmin.googleapis.com/v1/projects/",
      "startTime":"2023-07-26T10:19:39.118Z",
      "status":"DONE",
      "targetId":"pricing-dev-master",
      "targetLink":"https://sqladmin.googleapis.com/v1/projects/",
      "targetProject":"project-dev",
      "user":"sa@.iam.gserviceaccount.com"
   },
   **"code":200**,
   "headers":{
      "Alt-Svc":"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000",
      "Cache-Control":"private",
      "Content-Length":"1290",
      "Content-Type":"application/json; charset=UTF-8",
      "Date":"Wed, 26 Jul 2023 10:19:49 GMT",
      "Server":"ESF",
      "Vary":"Origin, X-Origin, Referer",
      "X-Content-Type-Options":"nosniff",
      "X-Frame-Options":"SAMEORIGIN",
      "X-Xss-Protection":"0"
   }
}

To handle this situtation i wanted to write a logic to make the workflow Fail in case of ERROR in import process. I tried to capture the body response in "maps" but then its not working. Can you please suggest is this the right way to append a map on every HTTP Get calls?

 - chekoperation:
        switch:
          - condition: ${operation.body.status != "DONE"}       
            next: wait
          - condition: ${"sql#operationError" in progress_Map} 
            raise: "Error in the load"
        next : completed    
    - completed:
        return: "done"
    - wait:
        call: sys.sleep
        args:
          seconds: 5
        next: getoperation
    - getoperation:
        call: http.get
        args:
          url: ${operation.body.selfLink}
          auth:
            type: OAuth2
        result: operation
        next: trackprogress
    # Creating Maps to add the response body into progress_Map 
    - trackprogress:
        assign:
        - progress_Map[file]: ${operation.body}
    - returnoutput:          
        return: ${progress_Map}
        next: chekoperation

I have the below code to handle '''switch''' to control the exeution but somehow its not working.

 

 - chekoperation:
    switch:
      - condition: ${operation.body.status != "DONE"}  
        # Tried this as well
      #- condition: ${not("DONE" in progress_Map)}      
        next: wait
      - condition: ${"sql#operationError" in progress_Map} 
           
        raise: "Error in the load"
    next : completed

 

 

 

 

 

 

0 2 569
2 REPLIES 2

Thank you for sharing the details of the issue faced .Posting on GCP workflow forum for this query would get the right help

https://cloud.google.com/workflows/docs/getting-support

Hi @gurusank ,


@gurusank wrote:

"error":{ "errors":[ { "code":"ERROR_RDBMS", "kind":"sql#operationError", "message":"generic::failed_precondition: ERROR: invalid input syntax for type integer: \"2023-07-11\"\nCONTEXT: COPY table_name, line 1, column id: \"2023-07-11\"\n" } ], "kind":"sql#operationErrors" },


Based from the error messages that you're getting, this suggests that there is an issue with data being inserted or updated in an integer column.

Here are steps you can take to address the problem:

  1. Examine the SQL query: Review the SQL query that is being executed during the HTTP POST call. Double-check if any values being inserted or updated are of the correct data type for the target columns.

  2. Check data integrity: Verify the data that you are trying to insert or update. Ensure that all the values are valid and can be correctly converted to the data types of the target columns in the database.

  3. Use parameterized queries: If you are not already using parameterized queries, switch to them. Parameterized queries help prevent SQL injection and handle data formatting automatically, reducing the chance of data type errors.

  4. Test with a simplified query: Consider testing the HTTP POST call with a simplified query that inserts or updates a single row with basic data. This can help isolate the problem and identify any specific data causing the error.

You might want to check this documentation regarding HTTP requests.