Просмотр исходного кода

improve error handling and logging for GitHub API failures in upgrade and install script (#972)

CodinCat 9 месяцев назад
Родитель
Сommit
d6eff3b3a3
2 измененных файлов с 8 добавлено и 2 удалено
  1. 1 1
      install
  2. 7 1
      packages/opencode/src/installation/index.ts

+ 1 - 1
install

@@ -48,7 +48,7 @@ if [ -z "$requested_version" ]; then
     url="https://github.com/sst/opencode/releases/latest/download/$filename"
     specific_version=$(curl -s https://api.github.com/repos/sst/opencode/releases/latest | awk -F'"' '/"tag_name": "/ {gsub(/^v/, "", $4); print $4}')
 
-    if [[ $? -ne 0 ]]; then
+    if [[ $? -ne 0 || -z "$specific_version" ]]; then
         echo "${RED}Failed to fetch version information${NC}"
         exit 1
     fi

+ 7 - 1
packages/opencode/src/installation/index.ts

@@ -140,6 +140,12 @@ export namespace Installation {
   export async function latest() {
     return fetch("https://api.github.com/repos/sst/opencode/releases/latest")
       .then((res) => res.json())
-      .then((data) => data.tag_name.slice(1) as string)
+      .then((data) => {
+        if (typeof data.tag_name !== "string") {
+          log.error("GitHub API error", data)
+          throw new Error("failed to fetch latest version")
+        }
+        return data.tag_name.slice(1) as string
+      })
   }
 }