Skip to content

Commit

Permalink
Improve cURL error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
silkeh committed Mar 15, 2018
1 parent 5059ef2 commit 66c10e6
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pdns_api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ request() {
method="$1"
url="$2"
data="$3"
error=false

# Do the request
res="$(curl -sS --request "${method}" --header "${headers}" --data "${data}" "${url}")"
# Perform the request
# This is wrappend in an if to avoid the exit on error
if ! res="$(curl -sSfL --stderr - --request "${method}" --header "${headers}" --data "${data}" "${url}")"; then
error=true
fi

# Debug output
debug "Method: ${method}"
Expand All @@ -139,7 +143,7 @@ request() {
debug "Response: ${res}"

# Abort on failed request
if [[ "${res}" = *"error"* ]] || [[ "${res}" = "Not Found" ]]; then
if [[ "${res}" = *"error"* ]] || [[ "${error}" = true ]]; then
error "API error: ${res}"
exit 1
fi
Expand Down

0 comments on commit 66c10e6

Please sign in to comment.