Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Morishiri committed Oct 21, 2020
1 parent b6f069c commit 689f3c2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 5 additions & 1 deletion internal/app/loophole/loophole.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,24 @@ 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")
}
}
}
loadingSuccess(loader)

sshConfigHTTPS := &ssh.ClientConfig{
User: fmt.Sprintf("%s_https", siteSpecs.SiteID),
User: fmt.Sprintf(siteSpecs.SiteID),
Auth: []ssh.AuthMethod{
*publicKeyAuthMethod,
},
Expand Down
24 changes: 16 additions & 8 deletions internal/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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
}

0 comments on commit 689f3c2

Please sign in to comment.