github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/log/errors.go (about)

     1  package log
     2  
     3  // ErrorCategory defines the category of a pipeline error
     4  type ErrorCategory int
     5  
     6  // Error categories which allow categorizing failures
     7  const (
     8  	ErrorUndefined ErrorCategory = iota
     9  	ErrorBuild
    10  	ErrorCompliance
    11  	ErrorConfiguration
    12  	ErrorCustom
    13  	ErrorInfrastructure
    14  	ErrorService
    15  	ErrorTest
    16  )
    17  
    18  var errorCategory ErrorCategory = ErrorUndefined
    19  var fatalError []byte
    20  
    21  func (e ErrorCategory) String() string {
    22  	return [...]string{
    23  		"undefined",
    24  		"build",
    25  		"compliance",
    26  		"config",
    27  		"custom",
    28  		"infrastructure",
    29  		"service",
    30  		"test",
    31  	}[e]
    32  }
    33  
    34  // ErrorCategoryByString returns the error category based on the category text
    35  func ErrorCategoryByString(category string) ErrorCategory {
    36  	switch category {
    37  	case "build":
    38  		return ErrorBuild
    39  	case "compliance":
    40  		return ErrorCompliance
    41  	case "config":
    42  		return ErrorConfiguration
    43  	case "custom":
    44  		return ErrorCustom
    45  	case "infrastructure":
    46  		return ErrorInfrastructure
    47  	case "service":
    48  		return ErrorService
    49  	case "test":
    50  		return ErrorTest
    51  	}
    52  	return ErrorUndefined
    53  }
    54  
    55  // SetErrorCategory sets the error category
    56  // This can be used later by calling log.GetErrorCategory()
    57  // In addition it will be used when exiting the program with
    58  // log.FatalError(err, message)
    59  func SetErrorCategory(category ErrorCategory) {
    60  	errorCategory = category
    61  }
    62  
    63  // GetErrorCategory retrieves the error category which is currently known to the execution of a step
    64  func GetErrorCategory() ErrorCategory {
    65  	return errorCategory
    66  }
    67  
    68  // SetFatalErrorDetail sets the fatal error to be stored
    69  func SetFatalErrorDetail(error []byte) {
    70  	fatalError = error
    71  }
    72  
    73  // GetFatalErrorDetail retrieves the error which is currently known to the execution of a step
    74  func GetFatalErrorDetail() []byte {
    75  	return fatalError
    76  }