Last active
August 21, 2019 17:04
-
-
Save scheler/19fa9cd307b3d293ffc2e8660937392f to your computer and use it in GitHub Desktop.
Distinguishing curl client and server errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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