本文整理汇总了Golang中github.com/spacemonkeygo/errors.ErrorClass.String方法的典型用法代码示例。如果您正苦于以下问题:Golang ErrorClass.String方法的具体用法?Golang ErrorClass.String怎么用?Golang ErrorClass.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/spacemonkeygo/errors.ErrorClass
的用法示例。
在下文中一共展示了ErrorClass.String方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ShouldBeErrorClass
/*
'actual' should be an `*errors.Error`; 'expected' should be an `*errors.ErrorClass`;
we'll check that the error is under the umbrella of the error class.
*/
func ShouldBeErrorClass(actual interface{}, expected ...interface{}) string {
err, ok := actual.(error)
if !ok {
return fmt.Sprintf("You must provide an `error` as the first argument to this assertion; got `%T`", actual)
}
var class *errors.ErrorClass
switch len(expected) {
case 0:
return "You must provide a spacemonkey `ErrorClass` as the expectation parameter to this assertion."
case 1:
cls, ok := expected[0].(*errors.ErrorClass)
if !ok {
return "You must provide a spacemonkey `ErrorClass` as the expectation parameter to this assertion."
}
class = cls
default:
return "You must provide one parameter as an expectation to this assertion."
}
// checking if this is nil is surprisingly complicated due to https://golang.org/doc/faq#nil_error
if reflect.ValueOf(err).IsNil() {
return fmt.Sprintf("Expected error to be of class %q but it was nil!", class.String())
}
spaceClass := errors.GetClass(err)
if spaceClass.Is(class) {
return ""
}
return fmt.Sprintf("Expected error to be of class %q but it had %q instead! (Full message: %s)", class.String(), spaceClass.String(), err.Error())
}
示例2: ShouldPanicWith
/*
'actual' should be a `func()`; 'expected' should be an `*errors.ErrorClass`;
we'll run the function, and check that it panics, and that the error is under the umbrella of the error class.
*/
func ShouldPanicWith(actual interface{}, expected ...interface{}) string {
fn, ok := actual.(func())
if !ok {
return fmt.Sprintf("You must provide a `func()` as the first argument to this assertion; got `%T`", actual)
}
var errClass *errors.ErrorClass
switch len(expected) {
case 0:
return "You must provide a spacemonkey `ErrorClass` as the expectation parameter to this assertion."
case 1:
cls, ok := expected[0].(*errors.ErrorClass)
if !ok {
return "You must provide a spacemonkey `ErrorClass` as the expectation parameter to this assertion."
}
errClass = cls
default:
return "You must provide one parameter as an expectation to this assertion."
}
var caught error
try.Do(
fn,
).CatchAll(func(err error) {
caught = err
}).Done()
if caught == nil {
return fmt.Sprintf("Expected error to be of class %q but no error was raised!", errClass.String())
}
spaceClass := errors.GetClass(caught)
if spaceClass.Is(errClass) {
return ""
}
return fmt.Sprintf("Expected error to be of class %q but it had %q instead! (Full message: %s)", errClass.String(), spaceClass.String(), caught.Error())
}