Skip to content

Commit

Permalink
Log errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed Apr 26, 2017
1 parent fe2edd3 commit b3ba667
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions lib/http_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
# Use option -F or -C to alter the default behavior of the function.
#
# Option -F forces the return of a fresh resource, that is, if the
# server responds with 304, the function returns with a nonzero return
# code.
# server responds with 304, the function quietly returns with a nonzero
# return code.
#
# Option -C causes the function to go directly to cache. No GET request
# is issued. (This option is useful in offline mode.) If the resource
# is not cached, the function returns with a nonzero return code.
# is not cached, the function quietly returns with a nonzero return code.
#
# The output of the curl command-line tool is stored in the following
# temporary files:
Expand Down Expand Up @@ -220,24 +220,29 @@ conditional_get () {
)
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: $FUNCNAME failed to hash the location URL (exit code: $exit_code)" >&2
print_log_message -E "$FUNCNAME failed to hash the location URL (exit code: $exit_code)" "$log_file"
return 4
fi

cached_header_file="$cache_dir/${hash}_headers"
cached_content_file="$cache_dir/${hash}_content"

if $verbose_mode; then
print_log_message -D "$FUNCNAME using cached header file: ${cached_header_file}" "$log_file"
print_log_message -D "$FUNCNAME using cached content file: ${cached_content_file}" "$log_file"
print_log_message -D "$FUNCNAME using cached header file: $cached_header_file" "$log_file"
print_log_message -D "$FUNCNAME using cached content file: $cached_content_file" "$log_file"
fi

# check if the resource is cached
if [ -f "$cached_header_file" ] && [ -f "$cached_content_file" ]; then
if $cache_only_mode; then
print_log_message "$FUNCNAME reading cached content file: ${cached_content_file}" "$log_file"
print_log_message "$FUNCNAME reading cached content file: $cached_content_file" "$log_file"
/bin/cat "$cached_content_file"
return
exit_code=$?
if [ $exit_code -ne 0 ]; then
print_log_message -E "$FUNCNAME unable to cat output ($exit_code)" "$log_file"
return 3
fi
return 0
fi

conditional_get_mode=true
Expand Down Expand Up @@ -299,7 +304,7 @@ conditional_get () {
header_value=$( get_header_value "$cached_header_file" 'ETag' )
return_code=$?
if [ $return_code -ne 0 ]; then
echo "ERROR: $FUNCNAME: get_header_value (return code: $return_code)" >&2
print_log_message -E "$FUNCNAME: get_header_value (return code: $return_code)" "$log_file"
return 6
fi
if [ -n "$header_value" ]; then
Expand All @@ -309,7 +314,7 @@ conditional_get () {
header_value=$( get_header_value "$cached_header_file" 'Last-Modified' )
return_code=$?
if [ $return_code -ne 0 ]; then
echo "ERROR: $FUNCNAME: get_header_value (return code: $return_code)" >&2
print_log_message -E "$FUNCNAME: get_header_value (return code: $return_code)" "$log_file"
return 6
fi
if [ -n "$header_value" ]; then
Expand All @@ -325,7 +330,7 @@ conditional_get () {
eval $cmd
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: $FUNCNAME: curl failed (exit code: $exit_code)" >&2
print_log_message -E "$FUNCNAME: curl failed (exit code: $exit_code)" "$log_file"
return 5
fi

Expand All @@ -340,14 +345,14 @@ conditional_get () {

# sanity check
if [ ! -f "$tmp_header_file" ]; then
echo "ERROR: $FUNCNAME unable to find header file $tmp_header_file" >&2
print_log_message -E "$FUNCNAME unable to find header file $tmp_header_file" "$log_file"
return 3
fi

response_code=$( get_response_code "$tmp_header_file" )
return_code=$?
if [ $return_code -ne 0 ]; then
echo "ERROR: $FUNCNAME: get_response_code failed (return code: $return_code)" >&2
print_log_message -E "$FUNCNAME: get_response_code failed (return code: $return_code)" "$log_file"
return 7
fi
print_log_message "$FUNCNAME received response code: $response_code" "$log_file"
Expand All @@ -358,7 +363,7 @@ conditional_get () {
declared_content_length=$( get_header_value "$tmp_header_file" 'Content-Length' )
return_code=$?
if [ $return_code -ne 0 ]; then
echo "ERROR: $FUNCNAME: get_header_value failed (return code: $return_code)" >&2
print_log_message -E "$FUNCNAME: get_header_value failed (return code: $return_code)" "$log_file"
return 6
fi
actual_content_length=$( /bin/cat "$tmp_content_file" \
Expand All @@ -367,11 +372,11 @@ conditional_get () {
)
if [ -n "$declared_content_length" ]; then
if [ "$declared_content_length" != "$actual_content_length" ]; then
echo "ERROR: $FUNCNAME failed content length check" >&2
print_log_message -E "$FUNCNAME failed content length check" "$log_file"
return 3
fi
else
echo "WARNING: Content-Length response header missing" >&2
print_log_message -W "$FUNCNAME: Content-Length response header missing" "$log_file"
fi

if $verbose_mode; then
Expand All @@ -388,15 +393,15 @@ conditional_get () {
exit_code=$?
if [ $exit_code -ne 0 ]; then
/bin/rm -f "$cached_header_file" "$cached_content_file" >&2
echo "ERROR: $FUNCNAME failed copy to file $cached_header_file (exit code: $exit_code)" >&2
print_log_message -E "$FUNCNAME failed copy to file $cached_header_file (exit code: $exit_code)" "$log_file"
return 8
fi
print_log_message "$FUNCNAME writing cached content file: ${cached_content_file}" "$log_file"
/bin/cp -f "$tmp_content_file" "$cached_content_file" >&2
exit_code=$?
if [ $exit_code -ne 0 ]; then
/bin/rm -f "$cached_header_file" "$cached_content_file" >&2
echo "ERROR: $FUNCNAME failed copy to file $cached_content_file (exit code: $exit_code)" >&2
print_log_message -E "$FUNCNAME failed copy to file $cached_content_file (exit code: $exit_code)" "$log_file"
return 8
fi

Expand All @@ -409,7 +414,7 @@ conditional_get () {

$verbose_mode && print_log_message -D "$FUNCNAME downloaded 0 bytes (cache is up-to-date)" "$log_file"
else
echo "ERROR: $FUNCNAME failed with HTTP response code $response_code" >&2
print_log_message -E "$FUNCNAME failed with HTTP response code $response_code" "$log_file"
return 9
fi

Expand All @@ -421,7 +426,12 @@ conditional_get () {

print_log_message "$FUNCNAME reading cached content file: ${cached_content_file}" "$log_file"
/bin/cat "$cached_content_file"
return
exit_code=$?
if [ $exit_code -ne 0 ]; then
print_log_message -E "$FUNCNAME unable to cat output ($exit_code)" "$log_file"
return 3
fi
return 0
}

#######################################################################
Expand Down

0 comments on commit b3ba667

Please sign in to comment.