Skip to content

Commit

Permalink
Improve error handling (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzerk authored and tzerk committed Feb 28, 2017
1 parent 870c83b commit 522e388
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
12 changes: 11 additions & 1 deletion lib/coherentui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import (
"github.com/mitchellh/go-ps"
"log"
"os"
"errors"
)

func killCoherentUI() {
func killCoherentUI() (err error) {

defer func() {
if r := recover(); r != nil {
err = errors.New("Unable to kill 'CoherentUI_Host.exe', need admin rights!'")
}
}()

// Find process(es)
chp, err := ps.Processes()
if err != nil {
Expand All @@ -30,4 +38,6 @@ func killCoherentUI() {
proc.Kill()
}
}

return err
}
12 changes: 9 additions & 3 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
"errors"
)

// Telegram and program settings (config.yml)
Expand All @@ -22,7 +23,13 @@ type Config struct {
KillCoherentUI bool
}

func Read_Settings(ex string, err error) Config {
func Read_Settings(ex string) (config Config, err error) {

defer func() {
if r := recover(); r != nil {
err = errors.New("Unable to find config.yml, created a new one.")
}
}()

//// SETTINGS
//--------------------------------------------------------------------------------------------------------------
Expand All @@ -31,7 +38,6 @@ func Read_Settings(ex string, err error) Config {
// This is necessary for dynamic builds in Jetbrains Gogland IDE
//newex = strings.Replace(ex, "Application.exe", "config.yml", -1)

var config Config
source, err := ioutil.ReadFile(newex)

if err != nil {
Expand Down Expand Up @@ -62,5 +68,5 @@ func Read_Settings(ex string, err error) Config {
panic(err)
}

return(config)
return config, err
}
5 changes: 4 additions & 1 deletion lib/watchdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func Watchdog(

// KILL CoherentUI_Host.exe
if config.KillCoherentUI {
killCoherentUI()
err := killCoherentUI()
if err != nil {
label_Status.SetText(err.Error())
}
}

// INFINITE MAIN LOOP
Expand Down
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import (

// Variables
const VERSION = "0.1.5"
var errmsg string

func main() {


// SETTINGS
config := Read_Settings(os.Executable())
exp, _ := os.Executable()
config, err := Read_Settings(exp)
if err != nil {
errmsg = err.Error()
}

//// GUI
//--------------------------------------------------------------------------------------------------------------
Expand All @@ -23,8 +28,8 @@ func main() {

label_Process := ui.NewLabel(" Process: " + config.Process)
label_Status := ui.NewLabel(" Initializing...")
label_PID := ui.NewLabel("-")
label_Connection := ui.NewLabel("-")
label_PID := ui.NewLabel("")
label_Connection := ui.NewLabel("")
label_Update := ui.NewLabel("")

//box := ui.NewVerticalBox()
Expand Down Expand Up @@ -88,7 +93,12 @@ func main() {
return true
})
window.Show()
go Watchdog(config, label_Status, label_PID, label_Connection, label_Update, pb)

if (errmsg) != "" {
label_Status.SetText(" " + errmsg)
} else {
go Watchdog(config, label_Status, label_PID, label_Connection, label_Update, pb)
}
})
if ui != nil {
panic(ui)
Expand Down

0 comments on commit 522e388

Please sign in to comment.