Skip to content

Commit 16f1bcd

Browse files
authored
Fix not 200 http response code (#3)
1 parent 21152cc commit 16f1bcd

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func chooseServiceInteractive() string {
7070
fmt.Print("Enter number: ")
7171
_, err := fmt.Scanln(&choice)
7272
if err != nil || choice < 1 || choice > len(availableServices) {
73-
fmt.Println(" Invalid choice. Defaulting to 'google'")
73+
fmt.Println("⚠️ Invalid choice. Defaulting to 'google'")
7474
return "google"
7575
}
7676
return availableServices[choice-1]
@@ -81,23 +81,27 @@ func fetchData(client *http.Client, isp string) (float64, error) {
8181

8282
req, err := http.NewRequest("GET", url, nil)
8383
if err != nil {
84-
return 0, fmt.Errorf(" request creation error: %v", err)
84+
return 0, fmt.Errorf("⚠️ request creation error: %v", err)
8585
}
8686

8787
resp, err := client.Do(req)
8888
if err != nil {
89-
return 0, fmt.Errorf(" request error: %v", err)
89+
return 0, fmt.Errorf("⚠️ request error: %v", err)
9090
}
9191
defer resp.Body.Close()
9292

93+
if resp.StatusCode != http.StatusOK {
94+
return 0, fmt.Errorf("⚠️ unexpected status code: %d", resp.StatusCode)
95+
}
96+
9397
body, err := io.ReadAll(resp.Body)
9498
if err != nil {
95-
return 0, fmt.Errorf(" error reading response: %v", err)
99+
return 0, fmt.Errorf("⚠️ error reading response: %v", err)
96100
}
97101

98102
var data map[string][]float64
99103
if err := json.Unmarshal(body, &data); err != nil {
100-
return 0, fmt.Errorf(" JSON parse error: %v", err)
104+
return 0, fmt.Errorf("⚠️ JSON parse error: %v", err)
101105
}
102106

103107
values, ok := data[serviceIndicator]
@@ -116,15 +120,15 @@ func checkStatus(isp string, value float64) {
116120
if errorCounts[isp] >= 3 && !erroredISPs[isp] {
117121
err := beeep.Notify("🔴 Internet Outage", fmt.Sprintf("%s unreachable from %s", serviceIndicator, isp), "./icon.png")
118122
if err != nil {
119-
fmt.Printf("[%s] Notification error: %v\n", isp, err)
123+
fmt.Printf("[%s] ⚠️ Notification error: %v\n", isp, err)
120124
}
121125
erroredISPs[isp] = true
122126
}
123127
} else {
124128
if erroredISPs[isp] {
125129
err := beeep.Notify("🟢 Internet Restored", fmt.Sprintf("%s is reachable again from %s", serviceIndicator, isp), "./icon.png")
126130
if err != nil {
127-
fmt.Printf("[%s] Notification error: %v\n", isp, err)
131+
fmt.Printf("[%s] ⚠️ Notification error: %v\n", isp, err)
128132
}
129133
fmt.Printf("[%s] 🟢 %s is reachable again\n", isp, serviceIndicator)
130134
}
@@ -153,7 +157,7 @@ func main() {
153157
if *serviceFlag > 0 && *serviceFlag <= len(availableServices) {
154158
serviceIndicator = availableServices[*serviceFlag-1]
155159
} else if *serviceFlag != 0 {
156-
fmt.Println(" Invalid service number. Use --help to see available options.")
160+
fmt.Println("⚠️ Invalid service number. Use --help to see available options.")
157161
os.Exit(1)
158162
} else {
159163
serviceIndicator = chooseServiceInteractive()

0 commit comments

Comments
 (0)