Browse Source

Optimization, error helper

Volodymyr Tkach 2 years ago
parent
commit
0194082704
4 changed files with 31 additions and 1 deletions
  1. 4 0
      README.md
  2. 8 1
      ctrlc/ctrlc.go
  3. 15 0
      ctrlc/error.go
  4. 4 0
      main.go

+ 4 - 0
README.md

@@ -23,6 +23,10 @@ func main() {
 		// With goroutine inside
 		test := Run()
 
+		// err1 := fmt.Errorf("Startup error 1")
+		// err2 := fmt.Errorf("Startup error 2")
+		// return ctrlc.MakeError(shutdown, ctrlc.AppError(err1), ctrlc.AppError(err2))
+
 		// Http web server
 		mux := http.NewServeMux()
 		mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

+ 8 - 1
ctrlc/ctrlc.go

@@ -101,7 +101,9 @@ func AppWithTimeOut(t time.Duration, f CallbackFunc) {
 	ctx, cancel := context.WithTimeout(context.Background(), t)
 	for _, iface := range *ifaces {
 		if err := iface.Shutdown(ctx); err != nil {
-			errors = true
+			if !errors {
+				errors = true
+			}
 			fmt.Printf(
 				icon_hot(UseColors())+"%s\n",
 				clr(
@@ -180,3 +182,8 @@ func UseColors() bool {
 	}
 	return !IS_WIN_PLATFORM && useColors
 }
+
+func MakeError(shutdown context.CancelFunc, ifaces ...Iface) *[]Iface {
+	shutdown()
+	return &ifaces
+}

+ 15 - 0
ctrlc/error.go

@@ -0,0 +1,15 @@
+package ctrlc
+
+import "context"
+
+type Error struct {
+	error
+}
+
+func AppError(err error) *Error {
+	return &Error{err}
+}
+
+func (c *Error) Shutdown(ctx context.Context) error {
+	return c
+}

+ 4 - 0
main.go

@@ -15,6 +15,10 @@ func main() {
 		// With goroutine inside
 		test := Run()
 
+		// err1 := fmt.Errorf("Startup error 1")
+		// err2 := fmt.Errorf("Startup error 2")
+		// return ctrlc.MakeError(shutdown, ctrlc.AppError(err1), ctrlc.AppError(err2))
+
 		// Http web server
 		mux := http.NewServeMux()
 		mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {