csctl/internal/custom.go

32 lines
1.2 KiB
Go

package internal
import (
"errors"
"fmt"
"github.com/kataras/golog"
"os"
"os/user"
"time"
)
func Error(err error, msg string) error {
currentUser, _ := user.Current()
path := fmt.Sprintf("%s/.csctl.err.log", currentUser.HomeDir)
file, errf := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if errf != nil {
golog.Error("An error has occurred while handling another error. Please alert a technician immediately.")
fmt.Printf("==ERROR==\nOccurred at: %s || Message: %s\n\n%s\n\n\n", time.Now().String(), "nil", errf.Error())
panic(errf)
}
defer file.Close()
contents := fmt.Sprintf("==ERROR==\nOccurred at: %s || Message: %s\n\n%s\n\n\n", time.Now().String(), msg, err.Error())
_, errf = file.WriteString(contents)
if errf != nil {
golog.Error("An error has occurred while handling another error. Please alert a technician immediately.")
fmt.Printf("==ERROR==\nOccurred at: %s || Message: %s\n\n%s\n\n\n", time.Now().String(), "nil", errf.Error())
panic(errf)
}
golog.Fatal("An error has occurred while processing the executable. A log file has been created in %s, please alert a Technician and provide the contents of this file if needed.\n", path)
return errors.New(fmt.Sprintf("%s\n\nError Details: %s", msg, err))
}