示例#1
0
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)
}
示例#2
0
文件: common.go 项目: xh3b4sd/anna
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
}
示例#3
0
文件: error.go 项目: xh3b4sd/anna
// IsUnsupportedType asserts json.UnsupportedTypeError.
func IsUnsupportedType(err error) bool {
	if _, ok := errgo.Cause(err).(*json.UnsupportedTypeError); ok {
		return true
	}

	return false
}
示例#4
0
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))
	}
}
示例#5
0
文件: error.go 项目: xh3b4sd/anna
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
}
示例#6
0
// 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
}
示例#7
0
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)
}
示例#8
0
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)
	}
}
示例#9
0
文件: storage.go 项目: pulcy/robin
// 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
}
示例#10
0
文件: errors.go 项目: pulcy/rest-kit
// 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
}
示例#11
0
文件: storage.go 项目: pulcy/robin
// 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
}
示例#12
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidCLGTreeID asserts invalidCLGTreeIDError.
func IsInvalidCLGTreeID(err error) bool {
	return errgo.Cause(err) == invalidCLGTreeIDError
}
示例#13
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidStorageFlag asserts invalidStorageFlagError.
func IsInvalidStorageFlag(err error) bool {
	return errgo.Cause(err) == invalidStorageFlagError
}
示例#14
0
文件: error.go 项目: pulcy/robin
func IsNotMaster(err error) bool {
	return errgo.Cause(err) == NotMasterError
}
示例#15
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidPosition asserts invalidPositionError.
func IsInvalidPosition(err error) bool {
	return errgo.Cause(err) == invalidPositionError
}
示例#16
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidNetworkPayload asserts invalidNetworkPayloadError.
func IsInvalidNetworkPayload(err error) bool {
	return errgo.Cause(err) == invalidNetworkPayloadError
}
示例#17
0
文件: error.go 项目: xh3b4sd/anna
// IsWorkerCanceled asserts workerCanceledError.
func IsWorkerCanceled(err error) bool {
	return errgo.Cause(err) == workerCanceledError
}
示例#18
0
文件: error.go 项目: xh3b4sd/anna
// IsCannotParse asserts cannotParseError.
func IsCannotParse(err error) bool {
	return errgo.Cause(err) == cannotParseError
}
示例#19
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidInformationID asserts invalidInformationIDError.
func IsInvalidInformationID(err error) bool {
	return errgo.Cause(err) == invalidInformationIDError
}
示例#20
0
文件: error.go 项目: giantswarm/yochu
// IsJobDependency returns true if the given err is a JobDependencyError.
func IsJobDependency(err error) bool {
	return errgo.Cause(err) == JobDependencyError
}
示例#21
0
// IsInvalidFeatureKey asserts invalidFeatureKeyError.
func IsInvalidFeatureKey(err error) bool {
	return errgo.Cause(err) == invalidFeatureKeyError
}
示例#22
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidInterface asserts invalidInterfaceError.
func IsInvalidInterface(err error) bool {
	return errgo.Cause(err) == invalidInterfaceError
}
示例#23
0
文件: error.go 项目: xh3b4sd/anna
// IsCLGNotFound asserts clgNotFoundError.
func IsCLGNotFound(err error) bool {
	return errgo.Cause(err) == clgNotFoundError
}
示例#24
0
文件: error.go 项目: xh3b4sd/anna
// IsNoSuchFileOrDirectoryError asserts noSuchFileOrDirectoryError.
func IsNoSuchFileOrDirectoryError(err error) bool {
	return errgo.Cause(err) == noSuchFileOrDirectoryError
}
示例#25
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidAPIResponse asserts invalidAPIResponseError.
func IsInvalidAPIResponse(err error) bool {
	return errgo.Cause(err) == invalidAPIResponseError
}
示例#26
0
文件: error.go 项目: pulcy/j2
func IsNotFound(err error) bool {
	return errgo.Cause(err) == NotFoundError
}
示例#27
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidConfig asserts invalidConfigError.
func IsInvalidConfig(err error) bool {
	return errgo.Cause(err) == invalidConfigError
}
示例#28
0
文件: error.go 项目: xh3b4sd/anna
// IsDuplicatedMember asserts duplicatedMemberError.
func IsDuplicatedMember(err error) bool {
	return errgo.Cause(err) == duplicatedMemberError
}
示例#29
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidBehaviourID asserts invalidBehaviourIDError.
func IsInvalidBehaviourID(err error) bool {
	return errgo.Cause(err) == invalidBehaviourIDError
}
示例#30
0
文件: error.go 项目: xh3b4sd/anna
// IsInvalidCLGName asserts invalidCLGNameError.
func IsInvalidCLGName(err error) bool {
	return errgo.Cause(err) == invalidCLGNameError
}