Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions gh-md-toc
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,29 @@ gh_toc_md2html() {
fi

# echo $URL 1>&2
# append the HTTP status code as the last line of the output, so API
# errors are detected by status rather than by the rendered markdown
OUTPUT=$(curl -s \
--user-agent "$gh_user_agent" \
--data-binary @"$gh_tmp_file_md" \
-H "Content-Type:text/plain" \
-H "$AUTHORIZATION" \
-w '\n%{http_code}' \
"$URL")
local curl_status=$?
local http_code=${OUTPUT##*$'\n'}
OUTPUT=${OUTPUT%$'\n'*}

rm -f "${gh_file_md}~~"

if [ "$curl_status" != "0" ]; then
echo "XXNetworkErrorXX"
fi
if [ "$(echo "${OUTPUT}" | awk '/API rate limit exceeded/')" != "" ]; then
elif [ "$http_code" = "401" ]; then
echo "XXBadCredentialsXX"
elif [ "$http_code" != "200" ] && [ "$(echo "${OUTPUT}" | awk '/API rate limit exceeded/')" != "" ]; then
echo "XXRateLimitXX"
elif [ "$http_code" != "200" ]; then
echo "XXApiErrorXX ${http_code}"
else
echo "${OUTPUT}"
fi
Expand Down Expand Up @@ -161,13 +169,26 @@ gh_toc(){
echo "Parsing local markdown file requires access to github API"
echo "Please make sure curl is installed and check your network connectivity"
exit 1
fi
if [ "$rawhtml" == "XXRateLimitXX" ]; then
elif [ "$rawhtml" == "XXRateLimitXX" ]; then
echo "Parsing local markdown file requires access to github API"
echo "Error: You exceeded the hourly limit. See: https://developer.github.com/v3/#rate-limiting"
TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
echo "or place GitHub auth token here: ${TOKEN_FILE}"
exit 1
elif [ "$rawhtml" == "XXBadCredentialsXX" ]; then
echo "Parsing local markdown file requires access to github API"
echo "Error: GitHub API rejected the auth token (HTTP 401 Bad credentials)"
if [ -n "$GH_TOC_TOKEN" ]; then
echo "Check the GitHub auth token in the GH_TOC_TOKEN environment variable"
else
TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
echo "Check the GitHub auth token here: ${TOKEN_FILE}"
fi
exit 1
elif [[ "$rawhtml" == "XXApiErrorXX "* ]]; then
echo "Parsing local markdown file requires access to github API"
echo "Error: GitHub API request failed (HTTP ${rawhtml#XXApiErrorXX })"
exit 1
fi
local toc
toc=$(echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent" "$depth" "$numbered")
Expand Down
7 changes: 7 additions & 0 deletions tests/test directory/test_api_error_text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Bad credentials

Docs that mention GitHub API errors must still render.

## API rate limit exceeded

Blabla...
17 changes: 17 additions & 0 deletions tests/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ test_help() {
assert_equal "${lines[3]}" "Please make sure curl is installed and check your network connectivity"
}

@test "Error for local file with invalid GitHub token" {
GH_TOC_TOKEN=invalid run $BATS_TEST_DIRNAME/../gh-md-toc tests/test\ directory/test_plussign.md
assert_fail

assert_equal "${lines[2]}" "Parsing local markdown file requires access to github API"
assert_equal "${lines[3]}" "Error: GitHub API rejected the auth token (HTTP 401 Bad credentials)"
assert_equal "${lines[4]}" "Check the GitHub auth token in the GH_TOC_TOKEN environment variable"
}

@test "TOC for local file mentioning API error messages" {
run $BATS_TEST_DIRNAME/../gh-md-toc tests/test\ directory/test_api_error_text.md
assert_success

assert_equal "${lines[2]}" "* [Bad credentials](#bad-credentials)"
assert_equal "${lines[3]}" " * [API rate limit exceeded](#api-rate-limit-exceeded)"
}

@test "TOC with depth for local file, #25" {
run $BATS_TEST_DIRNAME/../gh-md-toc --depth 2 tests/test\ directory/test_depth.md
assert_success
Expand Down