github.com/go-maxhub/gremlins@v1.0.1-0.20231227222204-b03a6a1e3e09/core/execution/execution.go (about)

     1  /*
     2   * Copyright 2022 The Gremlins Authors
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package execution
    18  
    19  // ErrorType is the type of the error that can generate a specific exit status.
    20  type ErrorType int
    21  
    22  // String produces the human readable sentence for the ErrorType.
    23  func (e ErrorType) String() string {
    24  	switch e {
    25  	case EfficacyThreshold:
    26  		return "below efficacy-threshold"
    27  	case MutantCoverageThreshold:
    28  		return "below mutant coverage-threshold"
    29  	}
    30  	panic("this should not happen")
    31  }
    32  
    33  const (
    34  	// EfficacyThreshold is the error type raised when efficacy is below threshold.
    35  	EfficacyThreshold ErrorType = iota
    36  
    37  	// MutantCoverageThreshold is the error type raised when mutant coverage is
    38  	// below threshold.
    39  	MutantCoverageThreshold
    40  )
    41  
    42  var errorMapping = map[ErrorType]int{
    43  	EfficacyThreshold:       10,
    44  	MutantCoverageThreshold: 11,
    45  }
    46  
    47  // ExitError is a special Error that is raised when special conditions require
    48  // Gremlins to exit with a specific errorCode.
    49  // If this error is returned and/or properly wrapped, it will reach the main
    50  // function. In the main, the exitCode will be set as the exit code of the
    51  // execution.
    52  type ExitError struct {
    53  	errorType ErrorType
    54  	exitCode  int
    55  }
    56  
    57  // NewExitErr instantiates a new ExitError.
    58  func NewExitErr(et ErrorType) *ExitError {
    59  	exitCode := errorMapping[et]
    60  
    61  	return &ExitError{exitCode: exitCode, errorType: et}
    62  }
    63  
    64  // Error is the implementation of the Error interface and returns
    65  // the ErrorType human readable message.
    66  func (e *ExitError) Error() string {
    67  	return e.errorType.String()
    68  }
    69  
    70  // ExitCode returns the exit code associated with the specific ErrorType.
    71  func (e *ExitError) ExitCode() int {
    72  	return e.exitCode
    73  }