Example #1
0
func (*functionSuite) TestWrap(c *gc.C) {
	first := errors.New("first") //err wrapFirst
	detailed := errors.New("detailed")
	err := errors.Wrap(first, detailed) //err wrapTest
	c.Assert(err.Error(), gc.Equals, "detailed")
	c.Assert(errors.Cause(err), gc.Equals, detailed)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["wrapFirst"].String())
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["wrapTest"].String())
}
Example #2
0
func checkErr(c *gc.C, err, cause error, msg string, details string) {
	c.Assert(err, gc.NotNil)
	c.Assert(err.Error(), gc.Equals, msg)
	c.Assert(errors.Cause(err), gc.Equals, cause)
	expectedDetails := replaceLocations(details)
	c.Assert(errors.Details(err), gc.Equals, expectedDetails)
}
Example #3
0
func (*functionSuite) TestWrapOfNil(c *gc.C) {
	detailed := errors.New("detailed")
	err := errors.Wrap(nil, detailed) //err nilWrapTest
	c.Assert(err.Error(), gc.Equals, "detailed")
	c.Assert(errors.Cause(err), gc.Equals, detailed)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["nilWrapTest"].String())
}
Example #4
0
File: errors.go Project: bac/juju
// ServerError returns an error suitable for returning to an API
// client, with an error code suitable for various kinds of errors
// generated in packages outside the API.
func ServerError(err error) *params.Error {
	if err == nil {
		return nil
	}
	logger.Tracef("server RPC error %v", errors.Details(err))
	msg := err.Error()
	// Skip past annotations when looking for the code.
	err = errors.Cause(err)
	code, ok := singletonCode(err)
	var info *params.ErrorInfo
	switch {
	case ok:
	case errors.IsUnauthorized(err):
		code = params.CodeUnauthorized
	case errors.IsNotFound(err):
		code = params.CodeNotFound
	case errors.IsUserNotFound(err):
		code = params.CodeUserNotFound
	case errors.IsAlreadyExists(err):
		code = params.CodeAlreadyExists
	case errors.IsNotAssigned(err):
		code = params.CodeNotAssigned
	case state.IsHasAssignedUnitsError(err):
		code = params.CodeHasAssignedUnits
	case state.IsHasHostedModelsError(err):
		code = params.CodeHasHostedModels
	case isNoAddressSetError(err):
		code = params.CodeNoAddressSet
	case errors.IsNotProvisioned(err):
		code = params.CodeNotProvisioned
	case IsUpgradeInProgressError(err):
		code = params.CodeUpgradeInProgress
	case state.IsHasAttachmentsError(err):
		code = params.CodeMachineHasAttachedStorage
	case isUnknownModelError(err):
		code = params.CodeModelNotFound
	case errors.IsNotSupported(err):
		code = params.CodeNotSupported
	case errors.IsBadRequest(err):
		code = params.CodeBadRequest
	case errors.IsMethodNotAllowed(err):
		code = params.CodeMethodNotAllowed
	default:
		if err, ok := err.(*DischargeRequiredError); ok {
			code = params.CodeDischargeRequired
			info = &params.ErrorInfo{
				Macaroon: err.Macaroon,
				// One macaroon fits all.
				MacaroonPath: "/",
			}
			break
		}
		code = params.ErrCode(err)
	}
	return &params.Error{
		Message: msg,
		Code:    code,
		Info:    info,
	}
}
Example #5
0
// sendError sends a JSON-encoded error response.
func (h *logSinkHandler) sendError(w io.Writer, req *http.Request, err error) {
	if err != nil {
		logger.Errorf("returning error from %s %s: %s", req.Method, req.URL.Path, errors.Details(err))
	}
	sendJSON(w, &params.ErrorResult{
		Error: common.ServerError(err),
	})
}
Example #6
0
func (*functionSuite) TestMaskf(c *gc.C) {
	first := errors.New("first")
	err := errors.Maskf(first, "masked %d", 42) //err maskfTest
	c.Assert(err.Error(), gc.Equals, "masked 42: first")
	c.Assert(errors.Cause(err), gc.Equals, err)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["maskfTest"].String())

	c.Assert(errors.Maskf(nil, "mask"), gc.IsNil)
}
Example #7
0
func (*functionSuite) TestTrace(c *gc.C) {
	first := errors.New("first")
	err := errors.Trace(first) //err traceTest
	c.Assert(err.Error(), gc.Equals, "first")
	c.Assert(errors.Cause(err), gc.Equals, first)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["traceTest"].String())

	c.Assert(errors.Trace(nil), gc.IsNil)
}
func (*errorsSuite) TestNewErr(c *gc.C) {
	if runtime.Compiler == "gccgo" {
		c.Skip("gccgo can't determine the location")
	}
	err := newEmbed("testing %d", 42) //err embedErr
	c.Assert(err.Error(), gc.Equals, "testing 42")
	c.Assert(errors.Cause(err), gc.Equals, err)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["embedErr"].String())
}
Example #9
0
func (*functionSuite) TestAnnotatef(c *gc.C) {
	first := errors.New("first")
	err := errors.Annotatef(first, "annotation %d", 2) //err annotatefTest
	c.Assert(err.Error(), gc.Equals, "annotation 2: first")
	c.Assert(errors.Cause(err), gc.Equals, first)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["annotatefTest"].String())

	c.Assert(errors.Annotatef(nil, "annotate"), gc.IsNil)
}
Example #10
0
// sendError sends a JSON-encoded error response.
// Note the difference from the error response sent by
// the sendError function - the error is encoded in the
// Error field as a string, not an Error object.
func (h *charmsHandler) sendError(w http.ResponseWriter, req *http.Request, err error) {
	logger.Errorf("returning error from %s %s: %s", req.Method, req.URL, errors.Details(err))
	perr, status := common.ServerErrorAndStatus(err)
	sendStatusAndJSON(w, status, &params.CharmsResponse{
		Error:     perr.Message,
		ErrorCode: perr.Code,
		ErrorInfo: perr.Info,
	})
}
func (*errorsSuite) TestNewErrWithCause(c *gc.C) {
	if runtime.Compiler == "gccgo" {
		c.Skip("gccgo can't determine the location")
	}
	causeErr := fmt.Errorf("external error")
	err := newEmbedWithCause(causeErr, "testing %d", 43) //err embedCause
	c.Assert(err.Error(), gc.Equals, "testing 43: external error")
	c.Assert(errors.Cause(err), gc.Equals, causeErr)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["embedCause"].String())
}
Example #12
0
func main() {
	err := fmt.Errorf("%s", "error msg here")
	err2 := fmt.Errorf("err2: %s", err)
	fmt.Println(err2)
	err = errors.Trace(err)
	//err = errors.Annotatef(err, "error has been traces and annotated")

	fmt.Println(err)
	fmt.Println("details:", errors.Details(err))
	fmt.Println("stack trace:", errors.ErrorStack(err))
}
Example #13
0
func (s *functionSuite) TestDetails(c *gc.C) {
	if runtime.Compiler == "gccgo" {
		c.Skip("gccgo can't determine the location")
	}
	c.Assert(errors.Details(nil), gc.Equals, "[]")

	otherErr := fmt.Errorf("other")
	checkDetails(c, otherErr, "[{other}]")

	err0 := newEmbed("foo") //err TestStack#0
	checkDetails(c, err0, "[{$TestStack#0$: foo}]")

	err1 := errors.Annotate(err0, "bar") //err TestStack#1
	checkDetails(c, err1, "[{$TestStack#1$: bar} {$TestStack#0$: foo}]")

	err2 := errors.Trace(err1) //err TestStack#2
	checkDetails(c, err2, "[{$TestStack#2$: } {$TestStack#1$: bar} {$TestStack#0$: foo}]")
}
Example #14
0
func (*functionSuite) TestDeferredAnnotatef(c *gc.C) {
	// NOTE: this test fails with gccgo
	if runtime.Compiler == "gccgo" {
		c.Skip("gccgo can't determine the location")
	}
	first := errors.New("first")
	test := func() (err error) {
		defer errors.DeferredAnnotatef(&err, "deferred %s", "annotate")
		return first
	} //err deferredAnnotate
	err := test()
	c.Assert(err.Error(), gc.Equals, "deferred annotate: first")
	c.Assert(errors.Cause(err), gc.Equals, first)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["deferredAnnotate"].String())

	err = nil
	errors.DeferredAnnotatef(&err, "deferred %s", "annotate")
	c.Assert(err, gc.IsNil)
}
// Activate makes the staged resource the active resource.
func (staged StagedResource) Activate() error {
	buildTxn := func(attempt int) ([]txn.Op, error) {
		// This is an "upsert".
		var ops []txn.Op
		switch attempt {
		case 0:
			ops = newInsertResourceOps(staged.stored)
		case 1:
			ops = newUpdateResourceOps(staged.stored)
		default:
			return nil, errors.New("setting the resource failed")
		}
		if staged.stored.PendingID == "" {
			// Only non-pending resources must have an existing service.
			ops = append(ops, staged.base.ServiceExistsOps(staged.stored.ServiceID)...)
		}
		// No matter what, we always remove any staging.
		ops = append(ops, newRemoveStagedResourceOps(staged.id)...)

		// If we are changing the bytes for a resource, we increment the
		// CharmModifiedVersion on the service, since resources are integral to
		// the high level "version" of the charm.
		if staged.stored.PendingID == "" {
			hasNewBytes, err := staged.hasNewBytes()
			if err != nil {
				logger.Errorf("can't read existing resource during activate: %v", errors.Details(err))
				return nil, errors.Trace(err)
			}
			if hasNewBytes {
				incOps := staged.base.IncCharmModifiedVersionOps(staged.stored.ServiceID)
				ops = append(ops, incOps...)
			}
		}
		logger.Debugf("activate ops: %#v", ops)
		return ops, nil
	}
	if err := staged.base.Run(buildTxn); err != nil {
		return errors.Trace(err)
	}
	return nil
}
Example #16
0
func checkDetails(c *gc.C, err error, details string) {
	c.Assert(err, gc.NotNil)
	expectedDetails := replaceLocations(details)
	c.Assert(errors.Details(err), gc.Equals, expectedDetails)
}
Example #17
0
func (s *RegisterSuite) TestControllerUUIDExists(c *gc.C) {
	// Controller has the UUID from s.testRegister to mimic a user with
	// this controller already registered (regardless of its name).
	err := s.store.AddController("controller-name", jujuclient.ControllerDetails{
		ControllerUUID: mockControllerUUID,
		CACert:         testing.CACert,
	})

	s.listModels = func(_ jujuclient.ClientStore, controllerName, userName string) ([]base.UserModel, error) {
		return []base.UserModel{{
			Name:  "model-name",
			Owner: "bob",
			UUID:  mockControllerUUID,
		}}, nil
	}

	registrationData := s.encodeRegistrationData(c, jujuclient.RegistrationInfo{
		User:           "******",
		SecretKey:      mockSecretKey,
		ControllerName: "controller-name",
	})

	srv := s.mockServer(c)
	s.httpHandler = srv

	prompter := cmdtesting.NewSeqPrompter(c, "»", `
Enter a new password: »hunter2

Confirm password: »hunter2

Initial password successfully set for bob.
`[1:])
	err = s.run(c, prompter, registrationData)
	c.Assert(err, gc.ErrorMatches, `controller is already registered as "controller-name"`, gc.Commentf("details: %v", errors.Details(err)))
	prompter.CheckDone()
}
Example #18
0
func (*functionSuite) TestErrorf(c *gc.C) {
	err := errors.Errorf("testing %d", 42) //err errorfTest
	c.Assert(err.Error(), gc.Equals, "testing 42")
	c.Assert(errors.Cause(err), gc.Equals, err)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["errorfTest"].String())
}
Example #19
0
func (*functionSuite) TestNew(c *gc.C) {
	err := errors.New("testing") //err newTest
	c.Assert(err.Error(), gc.Equals, "testing")
	c.Assert(errors.Cause(err), gc.Equals, err)
	c.Assert(errors.Details(err), jc.Contains, tagToLocation["newTest"].String())
}