Skip to content

Instantly share code, notes, and snippets.

@scheler
Last active August 21, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scheler/19fa9cd307b3d293ffc2e8660937392f to your computer and use it in GitHub Desktop.
Save scheler/19fa9cd307b3d293ffc2e8660937392f to your computer and use it in GitHub Desktop.
Distinguishing curl client and server errors
function curl_client_error() {
if [ "$2" == "4" ]; then
echo $1: Error resolving proxy
elif [ "$2" == "6" ]; then
echo $1: Error resolving host
elif [ "$2" == "7" ]; then
echo $1: Error connecting to host
elif [ "$2" == "28" ]; then
echo $1: Server response timed out.
fi
}
function curl_server_error() {
if [ $2 -ge 400 ]; then
echo $1: Error [$2] from server
fi
}
curl --connect-timeout 3 --max-time 2 httpstat.us/200?sleep=5000 2> /dev/null 2>&1
curl_client_error "Client" $?
# Client: Server response timed out.
response=$(curl --connect-timeout 3 --max-time 2 --write-out %{http_code} --silent --output /dev/null https://httpstat.us/502)
curl_server_error "Server" $response
#Server: Error [502] from server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment