@@ -46,7 +46,7 @@ create unique index u_cache_q_source_target on google_translate.cache
4646
4747comment on table google_translate.cache is ' Cache for Google Translate API calls' ;
4848
49- create or replace function google_translate ._translate_curl(text , char (2 ), char (2 ), text ) returns json as $$
49+ create or replace function google_translate ._translate_curl(text , char (2 ), char (2 ), text ) returns text as $$
5050# !/bin/sh
5151curl -- connect-timeout 2 -H "Accept: application/json" "https://www.googleapis.com/language/translate/v2?key=$1&source=$2&target=$3&q=$4" 2>/dev/null | sed 's/\r//g'
5252$$ language plsh;
@@ -58,10 +58,13 @@ declare
5858 q2call_urlencoded text ;
5959 url_len int4;
6060 response json;
61+ response_text text ;
6162 resp_1 json;
6263 res text [];
6364 k int4;
6465 rec record;
66+ err_state text ;
67+ err_detail text ;
6568begin
6669 res := qs; -- by default, return input "as is"
6770 qs2call := array[]::text [];
8891 raise debug ' INPUT: i: %, q: "%", result found in cache: "%"' , rec .i , rec .q , rec .result ;
8992 if rec .result is not null then
9093 res[rec .i ] := rec .result ;
91- elsif (current_setting(' google_translate.begin_at' ) is not null
94+ elsif (current_setting(' google_translate.begin_at' ) is not null
9295 and current_setting(' google_translate.begin_at' )::timestamp > current_timestamp
9396 ) or (current_setting(' google_translate.end_at' ) is not null
9497 and current_setting(' google_translate.end_at' )::timestamp < current_timestamp
@@ -107,14 +110,22 @@ begin
107110 raise debug ' URLENCODED STRING: %' , q2call_urlencoded;
108111
109112 if q2call_urlencoded <> ' ' then
110- -- q2call_urlencoded := replace(q2call_urlencoded, ' ', '+');
111113 url_len := length(q2call_urlencoded);
112114 raise debug ' q2call_urlencoded length=%, total URL length=%' , url_len, (url_len + 115 );
113115 if url_len > 1885 then
114116 raise exception ' Google API' ' s character limit (2K) is exceeded, total URL length=%' , (url_len + 115 );
115117 end if;
116118 raise info ' Calling Google Translate API for source=%, target=%, q=%' , source, target, q2call_urlencoded;
117- select into response google_translate ._translate_curl (api_key, source, target, q2call_urlencoded);
119+ begin
120+ select into response_text google_translate ._translate_curl (api_key, source, target, q2call_urlencoded);
121+ response := response_text::json;
122+ exception
123+ when invalid_text_representation then -- Google returned text, not JSON
124+ get stacked diagnostics err_state = returned_sqlstate, err_detail = pg_exception_detail;
125+ raise exception ' Google Translate API returned text, not JSON (see details)'
126+ using detail = response_text,
127+ hint = ' Google Translate API usually returns text instead of JSON if something is wrong with the request (error 400 "Bad Request").' ;
128+ end;
118129 if response is null then
119130 raise exception ' Google API responded with empty JSON' ;
120131 elsif response- > ' error' - > ' message' is not null then
0 commit comments