github.com/Comcast/plax@v0.8.32/dsl/broken.go (about)

     1  /*
     2   * Copyright 2021 Comcast Cable Communications Management, LLC
     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   * SPDX-License-Identifier: Apache-2.0
    17   */
    18  
    19  package dsl
    20  
    21  import "fmt"
    22  
    23  // Broken is an error that represents a test that is broken (and not
    24  // simpling failing).
    25  type Broken struct {
    26  	// ToDo: Consider using an interface.
    27  
    28  	Err error
    29  }
    30  
    31  // Brokenf makes a nice Broken for you.
    32  func Brokenf(format string, args ...interface{}) *Broken {
    33  	return NewBroken(fmt.Errorf(format, args...))
    34  }
    35  
    36  // NewBroken does exactly what you'd expect.
    37  func NewBroken(err error) *Broken {
    38  	return &Broken{
    39  		Err: err,
    40  	}
    41  }
    42  
    43  // IsBroken reports whether the given error is a *Broken.  If it is,
    44  // returns it.
    45  //
    46  // This function knows about Errors.
    47  func IsBroken(err error) (*Broken, bool) {
    48  	if err == nil {
    49  		return nil, false
    50  	}
    51  
    52  	if errs, is := err.(*Errors); is {
    53  		return errs.IsBroken()
    54  	}
    55  
    56  	b, is := err.(*Broken)
    57  	return b, is
    58  }
    59  
    60  // Error makes *Broken into an error.
    61  func (b *Broken) Error() string {
    62  	return fmt.Sprintf("Broken: %s", b.Err)
    63  }
    64  
    65  func (b *Broken) String() string {
    66  	return b.Error()
    67  }
    68  
    69  // Failure is an error that does not represent something that's
    70  // broken.
    71  type Failure struct {
    72  	Err error
    73  }
    74  
    75  func (f *Failure) Error() string {
    76  	return string("failure: " + f.Err.Error())
    77  }
    78  
    79  func IsFailure(x interface{}) (*Failure, bool) {
    80  	f, is := x.(*Failure)
    81  	return f, is
    82  }
    83  
    84  func Failuref(format string, args ...interface{}) *Failure {
    85  	return &Failure{
    86  		Err: fmt.Errorf(format, args...),
    87  	}
    88  }