From 2576d53c35dd6f99ae592eeaff140e77ac47f9d0 Mon Sep 17 00:00:00 2001 From: ditatompel Date: Wed, 19 Jun 2024 18:46:12 +0700 Subject: [PATCH] feat: Only return exit code `1` for specific err Only return with status code `1` if error type is `errProber` which one of this following const: errNoEndpoint, errNoTorSocks, errNoAPIKey, and errInvalidCredentials. --- cmd/client/probe.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/cmd/client/probe.go b/cmd/client/probe.go index c6fbb63..3d83241 100644 --- a/cmd/client/probe.go +++ b/cmd/client/probe.go @@ -22,8 +22,10 @@ import ( const RPCUserAgent = "ditatombot/0.0.1 (Monero RPC Monitoring; https://github.com/ditatompel/xmr-remote-nodes)" const ( - errEnvNoEndpoint = errProber("please set SERVER_ENDPOINT in .env") - errEnvNoTorSocks = errProber("please set TOR_SOCKS in .env") + errNoEndpoint = errProber("no SERVER_ENDPOINT was provided") + errNoTorSocks = errProber("no TOR_SOCKS was provided") + errNoAPIKey = errProber("no API_KEY was provided") + errInvalidCredentials = errProber("invalid API_KEY credentials") ) type errProber string @@ -63,8 +65,13 @@ var ProbeCmd = &cobra.Command{ } if err := prober.Run(); err != nil { - slog.Error(fmt.Sprintf("[PROBE] %s", err.Error())) - os.Exit(1) + switch err.(type) { + case errProber: + slog.Error(fmt.Sprintf("[PROBE] %s", err.Error())) + os.Exit(1) + default: + slog.Warn(fmt.Sprintf("[PROBE] %s", err.Error())) + } } }, } @@ -99,11 +106,15 @@ func (p *proberClient) Run() error { // checks if all required environment variables are set func (p *proberClient) validateConfig() error { if p.endpoint == "" { - return errEnvNoEndpoint + return errNoEndpoint + } + if p.apiKey == "" { + return errNoAPIKey } if p.acceptTor && p.torSOCKS == "" { - return errEnvNoTorSocks + return errNoTorSocks } + return nil } @@ -133,7 +144,12 @@ func (p *proberClient) fetchJob() (monero.Node, error) { } defer resp.Body.Close() - if resp.StatusCode != 200 { + switch resp.StatusCode { + case 200: + break + case 401: + return node, errInvalidCredentials + default: return node, fmt.Errorf("status code: %d", resp.StatusCode) }