github.com/nycdavid/zeus@v0.0.0-20201208104106-9ba439429e03/go/zerror/zerror.go (about)

     1  package zerror
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"syscall"
     7  
     8  	slog "github.com/burke/zeus/go/shinylog"
     9  )
    10  
    11  var finalOutput []func()
    12  
    13  func Init() {
    14  	finalOutput = make([]func(), 0)
    15  }
    16  
    17  func PrintFinalOutput() {
    18  	for _, cb := range finalOutput {
    19  		cb()
    20  	}
    21  }
    22  
    23  // TODO: this is gross because code is ignored.
    24  func ExitNow(code int, finalOuputCallback func()) {
    25  	finalOutput = append(finalOutput, finalOuputCallback)
    26  	proc, _ := os.FindProcess(os.Getpid())
    27  	proc.Signal(syscall.SIGTERM)
    28  }
    29  
    30  func Error(msg string) {
    31  	ExitNow(1, func() {
    32  		slog.Red(msg)
    33  	})
    34  }
    35  
    36  func ErrorCantConnectToMaster() {
    37  	slog.StdErrorString("Can't connect to master. Run {yellow}zeus start{red} first.\r")
    38  }
    39  
    40  func ErrorConfigCommandCouldntStart(msg, output string) {
    41  	ExitNow(1, func() {
    42  		slog.Red("Failed to initialize application from {yellow}zeus.json{red}.")
    43  		slog.Red("The json file is valid, but the {yellow}command{red} could not be started:\n\x1b[0m" + output)
    44  	})
    45  }
    46  
    47  func ErrorConfigCommandCrashed(output string) {
    48  	ExitNow(1, func() {
    49  		slog.Red("Couldn't boot application. {yellow}command{red} terminated with this output:")
    50  		fmt.Println(output)
    51  	})
    52  }
    53  
    54  // The config file is loaded before any goroutines are launched that require cleanup,
    55  // and our exitNow goroutine has not been spawned yet, so we will just explicitly exit
    56  // in the json-related errors..
    57  func ErrorConfigFileInvalidJson() {
    58  	if slog.Red("The config file {yellow}zeus.json{red} contains invalid JSON and could not be parsed.") {
    59  		os.Exit(1)
    60  	}
    61  }
    62  
    63  func ErrorConfigFileInvalidFormat() {
    64  	if slog.Red("The config file {yellow}zeus.json{red} is not in the correct format.") {
    65  		os.Exit(1)
    66  	}
    67  }
    68  
    69  func ErrorCantCreateListener() {
    70  	ExitNow(1, func() {
    71  		slog.Red("It looks like Zeus is already running. If not, remove {yellow}.zeus.sock{red} and try again.")
    72  	})
    73  }
    74  
    75  func ErrorUnableToAcceptSocketConnection() {
    76  	slog.Red("Unable to accept socket connection.")
    77  }