Skip to content

Commit

Permalink
Merge pull request #11754 from thalesmg/fix-postgres-error-reason-log…
Browse files Browse the repository at this point in the history
…-m-20231011

fix(postgres): format unicode error messages from driver
  • Loading branch information
thalesmg committed Oct 13, 2023
2 parents 0144ed9 + b07dddd commit 7a6c756
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/emqx_connector/src/emqx_connector.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% -*- mode: erlang -*-
{application, emqx_connector, [
{description, "EMQX Data Integration Connectors"},
{vsn, "0.1.32"},
{vsn, "0.1.33"},
{registered, []},
{mod, {emqx_connector_app, []}},
{applications, [
Expand Down
61 changes: 48 additions & 13 deletions apps/emqx_connector/src/emqx_connector_pgsql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,18 @@ on_sql_query(InstId, PoolName, Type, NameOrSQL, Data) ->
pgsql_connector_query_return,
#{error => Reason}
),
?SLOG(error, #{
msg => "postgresql_connector_do_sql_query_failed",
connector => InstId,
type => Type,
sql => NameOrSQL,
reason => Reason
}),
?SLOG(
error,
maps:merge(
#{
msg => "postgresql_connector_do_sql_query_failed",
connector => InstId,
type => Type,
sql => NameOrSQL
},
translate_to_log_context(Reason)
)
),
case Reason of
sync_required ->
{error, {recoverable_error, Reason}};
Expand Down Expand Up @@ -452,10 +457,12 @@ init_prepare(State = #{prepare_sql := Prepares, pool_name := PoolName}) ->
{ok, Sts} ->
State#{prepare_statement := Sts};
Error ->
LogMeta = #{
msg => <<"postgresql_init_prepare_statement_failed">>, error => Error
},
?SLOG(error, LogMeta),
LogMsg =
maps:merge(
#{msg => <<"postgresql_init_prepare_statement_failed">>},
translate_to_log_context(Error)
),
?SLOG(error, LogMsg),
%% mark the prepare_sql as failed
State#{prepare_sql => {error, Prepares}}
end
Expand Down Expand Up @@ -500,10 +507,20 @@ prepare_sql_to_conn(Conn, [{Key, SQL} | PrepareList], Statements) when is_pid(Co
{error, {error, error, _, undefined_table, _, _} = Error} ->
%% Target table is not created
?tp(pgsql_undefined_table, #{}),
?SLOG(error, LogMeta#{msg => "postgresql_parse_failed", error => Error}),
LogMsg =
maps:merge(
LogMeta#{msg => "postgresql_parse_failed"},
translate_to_log_context(Error)
),
?SLOG(error, LogMsg),
{error, undefined_table};
{error, Error} = Other ->
?SLOG(error, LogMeta#{msg => "postgresql_parse_failed", error => Error}),
LogMsg =
maps:merge(
LogMeta#{msg => "postgresql_parse_failed"},
translate_to_log_context(Error)
),
?SLOG(error, LogMsg),
Other
end.

Expand All @@ -529,3 +546,21 @@ handle_batch_result([{error, Error} | _Rest], _Acc) ->
{error, {unrecoverable_error, Error}};
handle_batch_result([], Acc) ->
{ok, Acc}.

translate_to_log_context(#error{} = Reason) ->
#error{
severity = Severity,
code = Code,
codename = Codename,
message = Message,
extra = Extra
} = Reason,
#{
driver_severity => Severity,
driver_error_codename => Codename,
driver_error_code => Code,
driver_error_message => emqx_logger_textfmt:try_format_unicode(Message),
driver_error_extra => Extra
};
translate_to_log_context(Reason) ->
#{reason => Reason}.
1 change: 1 addition & 0 deletions changes/ee/fix-11754.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved log formatting for Postgres bridge when there are unicode characters in the error messages returned by the driver.

0 comments on commit 7a6c756

Please sign in to comment.