func (*errorsSuite) TestCause(c *gc.C) { c.Assert(errgo.Cause(someErr), gc.Equals, someErr) causeErr := errgo.New("cause error") underlyingErr := errgo.New("underlying error") //err TestCause#1 err := errgo.WithCausef(underlyingErr, causeErr, "foo %d", 99) //err TestCause#2 c.Assert(errgo.Cause(err), gc.Equals, causeErr) checkErr(c, err, underlyingErr, "foo 99: underlying error", "[{$TestCause#2$: foo 99} {$TestCause#1$: underlying error}]", causeErr) err = &embed{err.(*errgo.Err)} c.Assert(errgo.Cause(err), gc.Equals, causeErr) }
func (a *annactl) GetSessionID() (string, error) { a.Log.WithTags(spec.Tags{C: nil, L: "D", O: a, V: 13}, "call GetSession") // Read session ID. raw, err := a.Service().FS().ReadFile(SessionFilePath) if _, ok := errgo.Cause(err).(*os.PathError); ok { // Session file does not exist. Go ahead to create one. } else if err != nil { return "", maskAny(err) } if len(raw) != 0 { return string(raw), nil } // Create session ID. newSessionID, err := a.Service().ID().New() if err != nil { return "", maskAny(err) } err = a.Service().FS().WriteFile(SessionFilePath, []byte(newSessionID), os.FileMode(0644)) if err != nil { return "", maskAny(err) } return string(newSessionID), nil }
// IsUnsupportedType asserts json.UnsupportedTypeError. func IsUnsupportedType(err error) bool { if _, ok := errgo.Cause(err).(*json.UnsupportedTypeError); ok { return true } return false }
func TestCause(t *testing.T) { if cause := errgo.Cause(someErr); cause != someErr { t.Fatalf("expected %q kind; got %#v", someErr, cause) } causeErr := errgo.New("cause error") underlyingErr := errgo.New("underlying error") //err TestCause#1 err := errgo.WithCausef(underlyingErr, causeErr, "foo %d", 99) //err TestCause#2 if errgo.Cause(err) != causeErr { t.Fatalf("expected %q; got %#v", causeErr, errgo.Cause(err)) } checkErr(t, err, underlyingErr, "foo 99: underlying error", "[{$TestCause#2$: foo 99} {$TestCause#1$: underlying error}]", causeErr) err = &embed{err.(*errgo.Err)} if errgo.Cause(err) != causeErr { t.Fatalf("expected %q; got %#v", causeErr, errgo.Cause(err)) } }
func maskAnyf(err error, f string, v ...interface{}) error { if err == nil { return nil } f = fmt.Sprintf("%s: %s", err.Error(), f) newErr := errgo.WithCausef(nil, errgo.Cause(err), f, v...) newErr.(*errgo.Err).SetLocation(1) return newErr }
// IsParseFloatSyntax asserts strconv.ErrSyntax. func IsParseFloatSyntax(err error) bool { cause := errgo.Cause(err) if e, ok := cause.(*strconv.NumError); ok { if e.Err == strconv.ErrSyntax { return true } } return false }
func checkErr(c *gc.C, err, underlying error, msg string, details string, cause error) { c.Assert(err, gc.NotNil) c.Assert(err.Error(), gc.Equals, msg) if err, ok := err.(errgo.Wrapper); ok { c.Assert(err.Underlying(), gc.Equals, underlying) } else { c.Assert(underlying, gc.IsNil) } c.Assert(errgo.Cause(err), gc.Equals, cause) wantDetails := replaceLocations(details) c.Assert(errgo.Details(err), gc.Equals, wantDetails) }
func checkErr(t *testing.T, err, underlying error, msg string, details string, cause error) { if err == nil { t.Fatalf("err is nil; want %q", msg) } if err.Error() != msg { t.Fatalf("unexpected message: want %q; got %q", msg, err.Error()) } if err, ok := err.(errgo.Wrapper); ok { if err.Underlying() != underlying { t.Fatalf("unexpected underlying error: want %q; got %v", underlying, err.Underlying()) } } else if underlying != nil { t.Fatalf("no underlying error found; want %q", underlying) } if errgo.Cause(err) != cause { t.Fatalf("unexpected cause: want %#v; got %#v", cause, errgo.Cause(err)) } wantDetails := replaceLocations(details) if gotDetails := errgo.Details(err); gotDetails != wantDetails { t.Fatalf("unexpected details: want %q; got %q", wantDetails, gotDetails) } }
// getRegistration reads the registration resource for the registration path. // If no such file exists, nil is returned. func (s *acmeService) getRegistration() (*acme.RegistrationResource, error) { raw, err := ioutil.ReadFile(s.RegistrationPath) if err != nil { if os.IsNotExist(errgo.Cause(err)) { return nil, nil } return nil, maskAny(err) } res := &acme.RegistrationResource{} if err := json.Unmarshal(raw, res); err != nil { return nil, maskAny(err) } return res, nil }
// NewErrorResponseFromError creates an ErrorResponse from the given error. // This ErrorResponse can be sent directly to an HttpResponseWriter. func NewErrorResponseFromError(err error) ErrorResponse { var er *ErrorResponse if erX, ok := err.(*ErrorResponse); ok { er = erX } else if erX, ok := errgo.Cause(err).(*ErrorResponse); ok { er = erX msg := err.Error() if msg != "" { er.TheError.Message = msg } } else { er = &ErrorResponse{} er.TheError.Message = err.Error() er.TheError.Code = -1 } return *er }
// getPrivateKey loads the private key from the private key path. // If there is no such file, a new private key is generated. func (s *acmeService) getPrivateKey() (*rsa.PrivateKey, error) { key, err := loadRSAPrivateKey(s.PrivateKeyPath) if err == nil { return key, nil } else if !os.IsNotExist(errgo.Cause(err)) { return nil, maskAny(err) } // private key not found, generate one key, err = rsa.GenerateKey(rand.Reader, s.KeyBits) if err != nil { return nil, maskAny(err) } if err := saveRSAPrivateKey(key, s.PrivateKeyPath); err != nil { return nil, maskAny(err) } return key, nil }
// IsInvalidCLGTreeID asserts invalidCLGTreeIDError. func IsInvalidCLGTreeID(err error) bool { return errgo.Cause(err) == invalidCLGTreeIDError }
// IsInvalidStorageFlag asserts invalidStorageFlagError. func IsInvalidStorageFlag(err error) bool { return errgo.Cause(err) == invalidStorageFlagError }
func IsNotMaster(err error) bool { return errgo.Cause(err) == NotMasterError }
// IsInvalidPosition asserts invalidPositionError. func IsInvalidPosition(err error) bool { return errgo.Cause(err) == invalidPositionError }
// IsInvalidNetworkPayload asserts invalidNetworkPayloadError. func IsInvalidNetworkPayload(err error) bool { return errgo.Cause(err) == invalidNetworkPayloadError }
// IsWorkerCanceled asserts workerCanceledError. func IsWorkerCanceled(err error) bool { return errgo.Cause(err) == workerCanceledError }
// IsCannotParse asserts cannotParseError. func IsCannotParse(err error) bool { return errgo.Cause(err) == cannotParseError }
// IsInvalidInformationID asserts invalidInformationIDError. func IsInvalidInformationID(err error) bool { return errgo.Cause(err) == invalidInformationIDError }
// IsJobDependency returns true if the given err is a JobDependencyError. func IsJobDependency(err error) bool { return errgo.Cause(err) == JobDependencyError }
// IsInvalidFeatureKey asserts invalidFeatureKeyError. func IsInvalidFeatureKey(err error) bool { return errgo.Cause(err) == invalidFeatureKeyError }
// IsInvalidInterface asserts invalidInterfaceError. func IsInvalidInterface(err error) bool { return errgo.Cause(err) == invalidInterfaceError }
// IsCLGNotFound asserts clgNotFoundError. func IsCLGNotFound(err error) bool { return errgo.Cause(err) == clgNotFoundError }
// IsNoSuchFileOrDirectoryError asserts noSuchFileOrDirectoryError. func IsNoSuchFileOrDirectoryError(err error) bool { return errgo.Cause(err) == noSuchFileOrDirectoryError }
// IsInvalidAPIResponse asserts invalidAPIResponseError. func IsInvalidAPIResponse(err error) bool { return errgo.Cause(err) == invalidAPIResponseError }
func IsNotFound(err error) bool { return errgo.Cause(err) == NotFoundError }
// IsInvalidConfig asserts invalidConfigError. func IsInvalidConfig(err error) bool { return errgo.Cause(err) == invalidConfigError }
// IsDuplicatedMember asserts duplicatedMemberError. func IsDuplicatedMember(err error) bool { return errgo.Cause(err) == duplicatedMemberError }
// IsInvalidBehaviourID asserts invalidBehaviourIDError. func IsInvalidBehaviourID(err error) bool { return errgo.Cause(err) == invalidBehaviourIDError }
// IsInvalidCLGName asserts invalidCLGNameError. func IsInvalidCLGName(err error) bool { return errgo.Cause(err) == invalidCLGNameError }