From b3ba667d7848bb224c1ccdb6221723b053c68110 Mon Sep 17 00:00:00 2001 From: Tom Scavo Date: Wed, 26 Apr 2017 13:57:17 -0400 Subject: [PATCH] Log errors and warnings --- lib/http_tools.sh | 50 ++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/lib/http_tools.sh b/lib/http_tools.sh index 7575ea1..17ec285 100755 --- a/lib/http_tools.sh +++ b/lib/http_tools.sh @@ -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: @@ -220,7 +220,7 @@ 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 @@ -228,16 +228,21 @@ conditional_get () { 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 @@ -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 @@ -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 @@ -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 @@ -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" @@ -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" \ @@ -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 @@ -388,7 +393,7 @@ 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" @@ -396,7 +401,7 @@ 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_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 @@ -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 @@ -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 } #######################################################################