'Slicing of Snowflake error message through Python

I have a snowflake procedure throwing below error message if the source and target count does not match, which is being caught in AWS Glue python exception block:

100132 (P0000): JavaScript execution error: Uncaught The table: test_table is NOT balanced. Following are the balancing statistics: 
TABLE_NAME: test_table
SOURCE_COUNT: 17022 
TARGET_COUNT: 6585
BALANCED: N
 in SF_TABLE_PROC at '          throw err;' position 3
stackstrace: 
SF_TABLE_PROC line: 38

But I want to remove snowflake's default error message "100132 (P0000): JavaScript execution error: Uncaught" and the later part "in SF_TABLE_PROC at ' throw err;' position 3 stackstrace: SF_TABLE_PROC line: 38".

Ideally my error message should only contain the main message body:

The table: test_table is NOT balanced. Following are the balancing statistics: 
TABLE_NAME: test_table
SOURCE_COUNT: 17022 
TARGET_COUNT: 6585
BALANCED: N

Snowflake proc block which throws the error message:

try
{
    if (tgt_count != src_count)
    {
        throw '\n' + 'The table: test_table is NOT balanced. Following are the balancing statistics: ' + '\n' + '\nTABLE_NAME: test_table' + '\nSOURCE_COUNT: ' + src_count + '\nTARGET_COUNT: ' + tgt_count + '\nBALANCED: N' + '\n';
    }
}
catch(err)
{   
    throw err;
}

where tgt_count: count(1) from target table and

src_count: count(1) from source table.

Is there any way I can slice the error message in snowflake or in python? When I tried to slice the error message in python it gave me an error: TypeError: 'ProgrammingError' object is not subscriptable Below is my python code:

except Exception as error:
    print("Error: ", error[53:90])
    raise(error)

Please help me in retrieving the exact error message.



Solution 1:[1]

You can try something like this:

from snowflake.connector import ProgrammingError

then:

except ProgrammingError as error:
    strError = str(error)
    print("Error: ", strError[53:90])
    raise(error)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sergiu