Skip to content

Commit f923afc

Browse files
fix #29: add error management
1 parent 0768106 commit f923afc

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

Deepl-Translate.alfred5workflow

180 Bytes
Binary file not shown.

Deepl-Translate.alfredworkflow

180 Bytes
Binary file not shown.

deepl.sh

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LANGUAGE_PREFERRED="${DEEPL_PREFERRED:-[\"DE\",\"EN\"]}"
88
KEY="${DEEPL_KEY:-}"
99
PRO="${DEEPL_PRO:-}"
1010
POSTFIX="${DEEPL_POSTFIX:-.}"
11-
VERSION="2.0.1"
11+
VERSION="2.1.0"
1212
PATH="$PATH:/usr/local/bin/"
1313
DIR="$( 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

6060
if [[ $KEY = "" ]] && [[ $query != *"$POSTFIX" ]]; then
6161
printJson "End query with $POSTFIX"
62-
exit 1
62+
exit 2
6363
fi
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'"
100114
else
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 => ({

info5.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ To avoid the error message 'too many requests', please configure your free (or p
611611
<key>variablesdontexport</key>
612612
<array/>
613613
<key>version</key>
614-
<string>2.0.2</string>
614+
<string>2.1.0</string>
615615
<key>webaddress</key>
616616
<string>https://github.com/AlexanderWillner/deepl-alfred-workflow2</string>
617617
</dict>

0 commit comments

Comments
 (0)