mirror of
https://github.com/openziti/zrok.git
synced 2025-06-21 10:17:51 +02:00
error handling dry (#789)
This commit is contained in:
parent
662aa59065
commit
35fc32b5f8
@ -79,13 +79,7 @@ func newAccessPrivateCommand() *accessPrivateCommand {
|
|||||||
func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
|
func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
|
||||||
root, err := environment.LoadRoot()
|
root, err := environment.LoadRoot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("error loading environment", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !root.IsEnabled() {
|
if !root.IsEnabled() {
|
||||||
@ -115,10 +109,7 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
|
|||||||
|
|
||||||
zrok, err := root.Client()
|
zrok, err := root.Client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !panicInstead {
|
cmd.error(err)
|
||||||
tui.Error("unable to create zrok client", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
||||||
@ -129,36 +120,17 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
|
|||||||
}
|
}
|
||||||
accessResp, err := zrok.Share.Access(req, auth)
|
accessResp, err := zrok.Share.Access(req, auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("unable to access", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bindAddress := cmd.bindAddress
|
bindAddress := cmd.bindAddress
|
||||||
if cmd.autoMode {
|
if cmd.autoMode {
|
||||||
if accessResp.Payload.BackendMode == "udpTunnel" {
|
if accessResp.Payload.BackendMode == "udpTunnel" {
|
||||||
cmd.destroy(accessResp.Payload.FrontendToken, root.Environment().ZitiIdentity, shrToken, zrok, auth)
|
cmd.error(errors.New("auto-addressing is not compatible with the 'udpTunnel' backend mode"))
|
||||||
if cmd.subordinate {
|
|
||||||
subordinateError(errors.New("auto-addressing is not compatible with the 'udpTunnel' backend mode"))
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("auto-addressing is not compatible with the 'udpTunnel' backend mode", nil)
|
|
||||||
}
|
|
||||||
panic(errors.New("auto-addressing is not compatible with the 'udpTunnel' backend mode"))
|
|
||||||
}
|
}
|
||||||
autoAddress, err := util.AutoListenerAddress("tcp", cmd.autoAddress, cmd.autoStartPort, cmd.autoEndPort)
|
autoAddress, err := util.AutoListenerAddress("tcp", cmd.autoAddress, cmd.autoStartPort, cmd.autoEndPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("unable to automatically find a listener address: %v", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
bindAddress = autoAddress
|
bindAddress = autoAddress
|
||||||
}
|
}
|
||||||
@ -173,13 +145,7 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
|
|||||||
|
|
||||||
endpointUrl, err := url.Parse(protocol + bindAddress)
|
endpointUrl, err := url.Parse(protocol + bindAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("invalid endpoint address", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
requests := make(chan *endpoints.Request, 1024)
|
requests := make(chan *endpoints.Request, 1024)
|
||||||
@ -192,23 +158,11 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
|
|||||||
RequestsChan: requests,
|
RequestsChan: requests,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("unable to create private access", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
if err := fe.Run(); err != nil {
|
if err := fe.Run(); err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("error starting access", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -221,23 +175,11 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
|
|||||||
IdleTime: time.Minute,
|
IdleTime: time.Minute,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("unable to create private frontend", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
if err := fe.Run(); err != nil {
|
if err := fe.Run(); err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("error starting frontend", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -249,23 +191,11 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
|
|||||||
RequestsChan: requests,
|
RequestsChan: requests,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("unable to create private access", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
if err := fe.Run(); err != nil {
|
if err := fe.Run(); err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("error starting access", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -279,23 +209,11 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
|
|||||||
RequestsChan: requests,
|
RequestsChan: requests,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("unable to create private access", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
if err := fe.Run(); err != nil {
|
if err := fe.Run(); err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("error starting access", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -307,22 +225,11 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
|
|||||||
cfg.RequestsChan = requests
|
cfg.RequestsChan = requests
|
||||||
fe, err := proxy.NewFrontend(cfg)
|
fe, err := proxy.NewFrontend(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("unable to create private frontend", err)
|
|
||||||
}
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
if err := fe.Run(); err != nil {
|
if err := fe.Run(); err != nil {
|
||||||
if cmd.subordinate {
|
cmd.error(err)
|
||||||
subordinateError(err)
|
|
||||||
}
|
|
||||||
if !panicInstead {
|
|
||||||
tui.Error("unable to run frontend", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@ -395,6 +302,16 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cmd *accessPrivateCommand) error(err error) {
|
||||||
|
if cmd.subordinate {
|
||||||
|
subordinateError(err)
|
||||||
|
}
|
||||||
|
if !panicInstead {
|
||||||
|
tui.Error("unable to create private access", err)
|
||||||
|
}
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
func (cmd *accessPrivateCommand) destroy(frontendName, envZId, shrToken string, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
func (cmd *accessPrivateCommand) destroy(frontendName, envZId, shrToken string, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
||||||
logrus.Infof("shutting down '%v'", shrToken)
|
logrus.Infof("shutting down '%v'", shrToken)
|
||||||
req := share.NewUnaccessParams()
|
req := share.NewUnaccessParams()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user