improve shx upload error handling
ci / smoke (push) Successful in 55s

This commit is contained in:
2026-06-04 22:10:55 +00:00
parent b2ef6d8e99
commit 17bd6622eb
4 changed files with 353 additions and 44 deletions
+65 -5
View File
@@ -24,6 +24,44 @@ Notes:
EOF
}
have_jq() {
command -v jq >/dev/null 2>&1
}
print_server_message() {
local response="$1"
local message=""
[[ -n "$response" ]] || return 0
if have_jq; then
message="$(
printf '%s\n' "$response" |
jq -r 'if type=="object" then .error // .message // .reason // empty else empty end' 2>/dev/null ||
true
)"
fi
if [[ -n "$message" ]]; then
echo "server: $message" >&2
else
echo "server response: $response" >&2
fi
}
response_has_files() {
local response="$1"
if have_jq; then
printf '%s\n' "$response" | jq -e '.files | arrays and length > 0' >/dev/null 2>&1
return $?
fi
[[ "$response" == *'"files"'* ]] || return 1
[[ "$response" =~ \"files\"[[:space:]]*:[[:space:]]*\[[[:space:]]*\] ]] && return 1
return 0
}
want_json=0
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
usage
@@ -60,25 +98,47 @@ fi
upload_one() {
local file="$1"
local body_file http_code response
if [[ ! -f "$file" ]]; then
echo "error: file not found: $file" >&2
return 1
fi
local response
response="$(curl -fsS \
body_file="$(mktemp)"
if ! http_code="$(curl -sS \
-o "$body_file" \
-w '%{http_code}' \
-H "authorization: $SHX_TOKEN" \
"$SHX_URL/api/upload" \
-F "file=@$file")"
-F "file=@$file")"; then
rm -f "$body_file"
echo "error: upload request failed for $file" >&2
return 1
fi
response="$(cat "$body_file")"
rm -f "$body_file"
if [[ ! "$http_code" =~ ^2[0-9][0-9]$ ]]; then
echo "error: upload failed for $file (HTTP $http_code)" >&2
print_server_message "$response"
return 1
fi
if ! response_has_files "$response"; then
echo "error: upload response missing a non-empty .files array for $file" >&2
print_server_message "$response"
return 1
fi
if [[ $want_json -eq 1 ]]; then
printf '%s\n' "$response"
else
if ! command -v jq >/dev/null 2>&1; then
if ! have_jq; then
echo "error: jq is required unless --json is used" >&2
return 1
fi
printf '%s\n' "$response" | jq -r 'if .files then .files[] else . end'
printf '%s\n' "$response" | jq -r '.files[]'
fi
}