-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix coding bugs: Replace panic with proper error handling #2296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -128,7 +128,8 @@ func main() { | |||||||
|
||||||||
listener, err := net.Listen("tcp", net.JoinHostPort(LOCALHOST, "0")) | ||||||||
if err != nil { | ||||||||
panic(err) | ||||||||
logger.Error("Failed to create TCP listener", zap.Error(err)) | ||||||||
return | ||||||||
} | ||||||||
routers := []string{ | ||||||||
"/v1/sys", | ||||||||
|
@@ -155,8 +156,8 @@ func main() { | |||||||
Target: "http://" + listener.Addr().String(), | ||||||||
}) | ||||||||
if err != nil { | ||||||||
fmt.Println("err", err) | ||||||||
panic(err) | ||||||||
logger.Error("Failed to create route", zap.Error(err), zap.String("path", apiPath)) | ||||||||
continue | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
@@ -169,7 +170,7 @@ func main() { | |||||||
if response != nil && response.StatusCode() != http.StatusOK { | ||||||||
logger.Error("error when trying to register one or more event types - some event type will not be discoverable", zap.String("status", response.Status()), zap.String("body", string(response.Body))) | ||||||||
} | ||||||||
if response.StatusCode() == http.StatusOK { | ||||||||
if response != nil && response.StatusCode() == http.StatusOK { | ||||||||
break | ||||||||
} | ||||||||
time.Sleep(time.Second) | ||||||||
|
@@ -224,6 +225,7 @@ func main() { | |||||||
// defer service.MyService.Storage().UnmountAllStorage() | ||||||||
err = s.Serve(listener) // not using http.serve() to fix G114: Use of net/http serve function that has no support for setting timeouts (see https://github.com/securego/gosec) | ||||||||
if err != nil { | ||||||||
panic(err) | ||||||||
logger.Error("Server failed to start", zap.Error(err)) | ||||||||
return | ||||||||
Comment on lines
+228
to
+229
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return is redundant
Suggested change
|
||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,7 +95,7 @@ func Post(url string, data []byte, contentType string, head map[string]string) ( | |
req.Header.Add(k, v) | ||
} | ||
if err != nil { | ||
panic(err) | ||
return "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this "proper error handling" Error should be bubbled up, so that whomever did the Empty content might be ok in some cases |
||
} | ||
|
||
client := &http.Client{Timeout: 5 * time.Second} | ||
|
@@ -120,14 +120,14 @@ func ZeroTierGet(url string, head map[string]string) (content string, code int) | |
req.Header.Add(k, v) | ||
} | ||
if err != nil { | ||
panic(err) | ||
return "", 0 | ||
} | ||
|
||
client := &http.Client{Timeout: 20 * time.Second} | ||
resp, error := client.Do(req) | ||
|
||
if error != nil { | ||
panic(error) | ||
return "", 0 | ||
} | ||
defer resp.Body.Close() | ||
code = resp.StatusCode | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why continue loading the server? This hides server loading issues, and it takes a while for whomever ran this service to know something is off (as the process didn't exit)