Replace SQL error Message

I’ve found another topic about this but thought it would probably be recommended that I would start another one since it’s old and from another person.
I would like to change the error message when we exceed character limit: e.g. “Failed to execute SQL. Error: SQLState: 22001 Error Code: 8152 Message: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]String or binary data would be truncated. SQLState: 01000 Error Code: 3621 Message: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The statement has been terminated. (8152)”

The suggestion was to use Message_Showing (in this case for Add/Copy page).
— I’ve replaced:

if (strpos($msg, “some standard message”) !== FALSE) // The original message contains some keywords you want to replace
$msg = “My custom message”;


— with ( by changing “some standard message” to the actual error message):

if (strpos($msg, “Failed to execute SQL. Error: SQLState: 22001 Error Code: 8152 Message: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]String or binary data would be truncated. SQLState: 01000 Error Code: 3621 Message: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The statement has been terminated. (8152)”) !== FALSE) // The original message contains some keywords you want to replace
$msg = “You have exceeded the maximum character limit. Please review your data”;


Am I seeing this wrong? Because nothing happened.
Thanks in advance

You should not include all the error message, since it may be contains some html or another character format. You should only include a part of the message, for example, only the error number.

Worked like a charm. Thanks!