Created
January 11, 2024 20:22
-
-
Save jdob/847af852668a4497efac4bdb9bfa1c86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"os" | |
"github.com/hashicorp/go-multierror" | |
"go.uber.org/zap" | |
"go.uber.org/zap/zapcore" | |
) | |
// ===== Defines the custom error | |
type ValidationError struct { | |
userMessage string | |
component string | |
reason string | |
} | |
func (notAnActualError ValidationError) Error() string { | |
return fmt.Sprintf("component %s failed because: %s", notAnActualError.component, notAnActualError.reason) | |
//return notAnActualError.err.Error() | |
} | |
func (notAnActualError ValidationError) Message() string { | |
return notAnActualError.userMessage | |
} | |
// ===== Interface to all component validation methods | |
type validation func() error | |
// ===== Pretend this is real running code | |
func setupLogging() { | |
//logFilename := "eib-build.log" | |
logConfig := zap.NewProductionConfig() | |
logConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel) | |
logConfig.Encoding = "console" | |
logConfig.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder | |
logConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | |
logger := zap.Must(logConfig.Build()) | |
// Set our configured logger to be accessed globally by zap.L() | |
zap.ReplaceGlobals(logger) | |
} | |
func EibDoingStuff() { | |
setupLogging() | |
validationErrors := ValidateDefinition() | |
fmt.Println("====== LOGS") | |
if validationErrors != nil { | |
zap.S().Errorf("shit broke: %s", validationErrors) | |
fmt.Println("====== CONSOLE") | |
unwrapped := errors.Unwrap(validationErrors) | |
fmt.Println(unwrapped.(ValidationError).userMessage) | |
} | |
} | |
func ValidateDefinition() error { | |
validations := []validation{ | |
validateOne, | |
validateKubernetes, | |
} | |
var validationErrors error | |
for _, v := range validations { | |
componentErrors := v() | |
if componentErrors != nil { | |
//validationErrors = append(validationErrors, componentErrors) | |
validationErrors = multierror.Append(validationErrors, componentErrors) | |
} | |
} | |
return validationErrors | |
} | |
func validateOne() error { | |
// No validation errors found | |
return nil | |
} | |
func validateKubernetes() error { | |
//var myErrors []error | |
var result error | |
_, err := os.Stat("main.______go") | |
if err != nil { | |
fileError := ValidationError{ | |
userMessage: "The file main.go must be specified but could not be found", | |
component: "two", | |
reason: fmt.Sprintf("File could not be found: %s", err), | |
} | |
//myErrors = append(myErrors, fileError) | |
result = multierror.Append(result, fileError) | |
} | |
// arch: foobar | |
if 1 != 2 { | |
semanticError := ValidationError{ | |
userMessage: "The user violated the basic principles of mathematics", | |
component: "two", | |
reason: "1 does not equal two", | |
} | |
//myErrors = append(myErrors, semanticError) | |
result = multierror.Append(result, semanticError) | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment