From 689f3c240df858033c0c1796997253769d4a7ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Sowa?= Date: Wed, 21 Oct 2020 13:21:42 +0200 Subject: [PATCH] Better error handling --- go.mod | 1 + go.sum | 1 + internal/app/loophole/loophole.go | 6 +++++- internal/pkg/client/client.go | 24 ++++++++++++++++-------- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index a31b833..14857b2 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.14 require ( github.com/briandowns/spinner v1.11.1 + github.com/coreos/etcd v3.3.10+incompatible github.com/go-acme/lego/v3 v3.7.0 github.com/kyokomi/emoji v2.2.4+incompatible github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381 diff --git a/go.sum b/go.sum index 9d37a8d..fa71a72 100644 --- a/go.sum +++ b/go.sum @@ -67,6 +67,7 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/cloudflare-go v0.10.2/go.mod h1:qhVI5MKwBGhdNU89ZRz2plgYutcJ5PCekLxXn56w6SY= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= +github.com/coreos/etcd v3.3.10+incompatible h1:jFneRYjIvLMLhDLCzuTuU4rSJUjRplcJQ7pD7MnhC04= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= diff --git a/internal/app/loophole/loophole.go b/internal/app/loophole/loophole.go index 20a5a58..ea34a97 100644 --- a/internal/app/loophole/loophole.go +++ b/internal/app/loophole/loophole.go @@ -185,12 +185,16 @@ func generateListener(config lm.Config, publicKeyAuthMethod *ssh.AuthMethod, pub log.Fatal().Err(err).Msg("Failed to register site, try logging in again") } } else if siteSpecs.ResultCode == 403 { + loadingFailure(loader) log.Fatal().Err(err).Msg("You don't have required permissions to establish tunnel with given parameters") } else if siteSpecs.ResultCode == 409 { + loadingFailure(loader) log.Fatal().Err(err).Msg("The given hostname is already taken by different used") } else if siteSpecs.ResultCode == 600 || siteSpecs.ResultCode == 601 { + loadingFailure(loader) log.Fatal().Err(err).Msg("Looks like you're not logged in") } else { + loadingFailure(loader) log.Fatal().Err(err).Msg("Something unexpected happened, please let developers know") } } @@ -198,7 +202,7 @@ func generateListener(config lm.Config, publicKeyAuthMethod *ssh.AuthMethod, pub loadingSuccess(loader) sshConfigHTTPS := &ssh.ClientConfig{ - User: fmt.Sprintf("%s_https", siteSpecs.SiteID), + User: fmt.Sprintf(siteSpecs.SiteID), Auth: []ssh.AuthMethod{ *publicKeyAuthMethod, }, diff --git a/internal/pkg/client/client.go b/internal/pkg/client/client.go index 0ca6c53..d01df3b 100644 --- a/internal/pkg/client/client.go +++ b/internal/pkg/client/client.go @@ -17,6 +17,15 @@ type SiteSpecification struct { ResultCode int } +type SuccessResponse struct { + SiteId string `json:"siteId"` +} +type ErrorResponse struct { + StatusCode string `json:"statusCode"` + Message string `json:"message"` + Error string `json:"error"` +} + func RegisterSite(apiURL string, publicKey ssh.PublicKey, siteID string) (SiteSpecification, error) { publicKeyString := publicKey.Type() + " " + base64.StdEncoding.EncodeToString(publicKey.Marshal()) @@ -52,23 +61,22 @@ func RegisterSite(apiURL string, publicKey ssh.PublicKey, siteID string) (SiteSp if err != nil { return SiteSpecification{"", 0}, err } + defer resp.Body.Close() if resp.StatusCode != 200 && resp.StatusCode != 201 { - return SiteSpecification{"", resp.StatusCode}, fmt.Errorf("Site registration request ended with %d status", resp.StatusCode) + errorResponse := ErrorResponse{} + json.NewDecoder(resp.Body).Decode(&errorResponse) + + return SiteSpecification{"", resp.StatusCode}, fmt.Errorf("%s", errorResponse.Message) } - var result map[string]interface{} + result := SuccessResponse{} json.NewDecoder(resp.Body).Decode(&result) if el := log.Debug(); el.Enabled() { fmt.Println() el.Interface("result", result).Msg("Response") } - defer resp.Body.Close() - siteID, ok := result["siteId"].(string) - if !ok { - return SiteSpecification{"", 400}, fmt.Errorf("Error converting siteId to string") - } - return SiteSpecification{siteID, resp.StatusCode}, nil + return SiteSpecification{result.SiteId, resp.StatusCode}, nil }