I am trying to log error when working with some data in Snowflake data warehouse.
The error I am getting on specific data file is as follows and I need to get it into a table called DATA_LOAD_LOG:
INSERT INTO DWH_OPS.DATA_LOAD_LOG select 'DATA LOAD PROCEDURE', ?, ?, (datediff('milliseconds', ?, ?)/1000), current_schema(), current_database(), current_user(), current_role(), ?, parse_json('[{"Error":"Can't parse 'n/a' as date with format 'YYYY-MM-DD'","Survey Name":"data.csv","Stage Name":"@DWH_OPS.AZURE_BLOB","Execution Duration (in seconds)":5.405}]')::VARIANT
The error message is:
"Can't parse 'n/a' as date with format 'YYYY-MM-DD'"
the catch part is returning a different syntax error as there single quote before t and 'YYYY-MM-DD'. so the log is not added to the table.
here is the catch part script:
try {
// Try code
} catch (err) {
var obj = {};
var res = [];
obj["Error"] = "Exists";
obj["Error Message"] = err.message;
obj["Status"] = err.status;
obj["Error Code"] = err.code;
res.push(obj);
var insert_into_data_log =
"INSERT INTO DWH_OPS.DATA_LOAD_LOG select 'DATA LOAD PROCEDURE', current_timestamp(), " +
"current_timestamp(), 0, " +
"current_schema(), current_database(), current_user(), current_role(), " +
"'SCRIPT ERROR', parse_json('" +
JSON.stringify(res) +
"')::VARIANT";
var data_log_stmt = snowflake
.createStatement({ sqlText: insert_into_data_log })
.execute();
return err;
}
I tried to replace with space or to escape it using:
var errorMsg = err.message; obj['Error Message'] = errorMsg.replace("'","").replace("."," ").replace("\n"," ");
but it didn't work at all and it is throwing the same error.