Skip to content

Commit

Permalink
fix(errors): fix error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
alpkeskin committed Jan 5, 2025
1 parent 34dc900 commit be63576
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ jobs:
fail_ci_if_error: true

- name: Run linter
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: latest
14 changes: 12 additions & 2 deletions cmd/rota/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
msgFailedToLoadProxies = "failed to load proxies"
msgWatchingProxyFile = "watching proxy file"
msgMissingProxyFile = "missing proxy file"
msgFailedToCheckProxies = "failed to check proxies"
msgFailedToServeApi = "failed to serve api"
msgFailedToListen = "failed to listen"
msgReceivedSignal = "received signal, shutting down..."
)
Expand Down Expand Up @@ -53,7 +55,11 @@ func main() {

if cfgManager.Check {
proxyChecker := proxy.NewProxyChecker(cfg, proxyServer)
proxyChecker.Check()
err = proxyChecker.Check()
if err != nil {
slog.Error(msgFailedToCheckProxies, "error", err)
os.Exit(1)
}
return
}

Expand Down Expand Up @@ -106,5 +112,9 @@ func runApi(cfg *config.Config) {
}

api := api.NewApi(cfg)
api.Serve()
err := api.Serve()
if err != nil {
slog.Error(msgFailedToServeApi, "error", err)
os.Exit(1)
}
}
8 changes: 7 additions & 1 deletion internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
msgMetricsRequested = "metrics requested"
msgMethodNotAllowed = "method not allowed"
msgFailedToCollectMetrics = "failed to collect metrics"
msgFailedToWriteMetrics = "failed to write metrics"
)

type Api struct {
Expand Down Expand Up @@ -105,7 +106,12 @@ func (a *Api) handleMetrics(w http.ResponseWriter, r *http.Request) {
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(metrics)
err = json.NewEncoder(w).Encode(metrics)
if err != nil {
slog.Error(msgFailedToWriteMetrics, "error", err)
http.Error(w, msgFailedToWriteMetrics, http.StatusInternalServerError)
return
}
}

func collectMetrics() (*metrics, error) {
Expand Down
6 changes: 5 additions & 1 deletion internal/proxy/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
msgFailedToCreateRequest = "failed to create request"
msgDeadProxy = "dead proxy"
msgAliveProxy = "alive proxy"
msgFailedToWriteOutputFile = "failed to write output file"
)

type ProxyChecker struct {
Expand Down Expand Up @@ -92,6 +93,9 @@ func (pl *ProxyChecker) checkProxy(proxy *Proxy, outputFile *os.File) {

slog.Info(msgAliveProxy, "proxy", proxy.Host)
if outputFile != nil {
outputFile.WriteString(proxy.Host + "\n")
_, err = outputFile.WriteString(proxy.Host + "\n")
if err != nil {
slog.Error(msgFailedToWriteOutputFile, "error", err)
}
}
}

0 comments on commit be63576

Please sign in to comment.