@@ -8,7 +8,7 @@ LANGUAGE_PREFERRED="${DEEPL_PREFERRED:-[\"DE\",\"EN\"]}"
88KEY=" ${DEEPL_KEY:- } "
99PRO=" ${DEEPL_PRO:- } "
1010POSTFIX=" ${DEEPL_POSTFIX:- .} "
11- VERSION=" 2.0.1 "
11+ VERSION=" 2.1.0 "
1212PATH=" $PATH :/usr/local/bin/"
1313DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " > /dev/null 2>&1 && pwd ) "
1414# ##############################################################################
@@ -59,7 +59,7 @@ query="$(echo "$query" | iconv -f utf-8-mac -t utf-8 | xargs)"
5959
6060if [[ $KEY = " " ]] && [[ $query != * " $POSTFIX " ]]; then
6161 printJson " End query with $POSTFIX "
62- exit 1
62+ exit 2
6363fi
6464# ##############################################################################
6565
@@ -88,7 +88,21 @@ if [ -n "$KEY" ]; then
8888 fi
8989 echo >&2 " curl -s -X POST '$url ' -H 'Authorization: DeepL-Auth-Key $KEY ' -d 'text=$query ' -d 'target_lang=${LANGUAGE:- EN} '"
9090 result=$( curl -s -X POST " $url " -H " Authorization: DeepL-Auth-Key $KEY " -d " text=$query " -d " target_lang=${LANGUAGE:- EN} " )
91- echo >&2 " $result "
91+ ret=$?
92+ if [[ " x$ret " != " x0" ]] || [[ " $result " == " " ]]; then
93+ echo >&2 " $ret : $result "
94+ http_code=$( curl -s -X POST " $url " -H " Authorization: DeepL-Auth-Key $KEY " -d " text=$query " -d " target_lang=${LANGUAGE:- EN} " -w %{http_code} -o /dev/null)
95+ if [[ $http_code -eq 403 ]]; then
96+ printJson " Error: Invalid API key"
97+ exit 3
98+ fi
99+ if [[ $ret -eq 6 ]]; then
100+ printJson " Error: DNS resolution failed - no Internet connection?"
101+ exit 4
102+ fi
103+ printJson " Error Code $ret - HTTP Code $http_code "
104+ exit 5
105+ fi
92106 osascript -l JavaScript -e ' function run(argv) {
93107 const translations = JSON.parse(argv[0])["translations"].map(item => ({
94108 title: item["text"],
@@ -99,13 +113,22 @@ if [ -n "$KEY" ]; then
99113 }' " $result " || echo >&2 " ERROR w/ key: result '$result ', query '$query '"
100114else
101115 echo >&2 " curl -s 'https://www2.deepl.com/jsonrpc' '${HEADER[@]} ' --data-binary $'$data '"
102- result=$( curl -s ' https://www2.deepl.com/jsonrpc' \
103- " ${HEADER[@]} " \
104- --data-binary $" $data " )
105- echo >&2 " $result "
116+ result=$( curl -s ' https://www2.deepl.com/jsonrpc' " ${HEADER[@]} " --data-binary $" $data " )
117+ ret=$?
118+ if [[ " x$ret " != " x0" ]] || [[ " $result " == " " ]]; then
119+ echo >&2 " $ret : $result "
120+ http_code=$( curl -s ' https://www2.deepl.com/jsonrpc' " ${HEADER[@]} " --data-binary $" $data " -w %{http_code} -o /dev/null)
121+ if [[ $ret -eq 6 ]]; then
122+ printJson " Error: DNS resolution failed - no Internet connection?"
123+ exit 6
124+ fi
125+ printJson " Error Code $ret - HTTP Code $http_code "
126+ exit 7
127+ fi
106128 if [[ $result == * ' "error":{"code":' * ]]; then
107129 message=" $( osascript -l JavaScript -e ' function run(argv) { return JSON.parse(argv[0])["error"]["message"] }' ) "
108130 printJson " Error: $message "
131+ exit 8
109132 else
110133 osascript -l JavaScript -e ' function run(argv) {
111134 const translations = JSON.parse(argv[0])["result"]["translations"][0]["beams"].map(item => ({
0 commit comments