// RestoreError makes a best effort at converting the given error // back into an error originally converted by ServerError(). If the // error could not be converted then false is returned. func RestoreError(err error) (error, bool) { err = errors.Cause(err) if apiErr, ok := err.(*params.Error); !ok { return err, false } else if apiErr == nil { return nil, true } if params.ErrCode(err) == "" { return err, false } msg := err.Error() if singleton, ok := singletonError(err); ok { return singleton, true } // TODO(ericsnow) Support the other error types handled by ServerError(). switch { case params.IsCodeUnauthorized(err): return errors.NewUnauthorized(nil, msg), true case params.IsCodeNotFound(err): // TODO(ericsnow) UnknownModelError should be handled here too. // ...by parsing msg? return errors.NewNotFound(nil, msg), true case params.IsCodeAlreadyExists(err): return errors.NewAlreadyExists(nil, msg), true case params.IsCodeNotAssigned(err): return errors.NewNotAssigned(nil, msg), true case params.IsCodeHasAssignedUnits(err): // TODO(ericsnow) Handle state.HasAssignedUnitsError here. // ...by parsing msg? return err, false case params.IsCodeNoAddressSet(err): // TODO(ericsnow) Handle isNoAddressSetError here. // ...by parsing msg? return err, false case params.IsCodeNotProvisioned(err): return errors.NewNotProvisioned(nil, msg), true case params.IsCodeUpgradeInProgress(err): // TODO(ericsnow) Handle state.UpgradeInProgressError here. // ...by parsing msg? return err, false case params.IsCodeMachineHasAttachedStorage(err): // TODO(ericsnow) Handle state.HasAttachmentsError here. // ...by parsing msg? return err, false case params.IsCodeNotSupported(err): return errors.NewNotSupported(nil, msg), true case params.IsBadRequest(err): return errors.NewBadRequest(nil, msg), true case params.IsMethodNotAllowed(err): return errors.NewMethodNotAllowed(nil, msg), true case params.ErrCode(err) == params.CodeDischargeRequired: // TODO(ericsnow) Handle DischargeRequiredError here. return err, false default: return err, false } }
// RegisterCleanupHandler identifies the handler to use a given // cleanup kind. func RegisterCleanupHandler(kindStr string, handler CleanupHandler) error { kind := cleanupKind(kindStr) if _, ok := cleanupHandlers[kind]; ok { return errors.NewAlreadyExists(nil, fmt.Sprintf("cleanup handler for %q already registered", kindStr)) } cleanupHandlers[kind] = handler return nil }
func registerAPIEndpoint(pattern string, spec apihttp.HandlerSpec) error { if _, ok := endpointRegistry[pattern]; ok { return errors.NewAlreadyExists(nil, fmt.Sprintf("endpoint %q already registered", pattern)) } endpointRegistry[pattern] = spec endpointRegistryOrder = append(endpointRegistryOrder, pattern) return nil }
// RegisterLatestCharmHandler adds the factory func for the identified // handler to the handler registry. func RegisterLatestCharmHandler(name string, newHandler newHandlerFunc) error { if _, ok := registeredHandlers[name]; ok { msg := fmt.Sprintf(`"latest charm" handler %q already registered`, name) return errors.NewAlreadyExists(nil, msg) } registeredHandlers[name] = newHandler return nil }
func (staged StagedResource) stage() error { // TODO(ericsnow) Ensure that the service is still there? buildTxn := func(attempt int) ([]txn.Op, error) { var ops []txn.Op switch attempt { case 0: ops = newStagedResourceOps(staged.stored) case 1: ops = newEnsureStagedSameOps(staged.stored) default: return nil, errors.NewAlreadyExists(nil, "already staged") } return ops, nil } if err := staged.base.Run(buildTxn); err != nil { return errors.Trace(err) } return nil }
func (staged StagedResource) stage() error { buildTxn := func(attempt int) ([]txn.Op, error) { var ops []txn.Op switch attempt { case 0: ops = newInsertStagedResourceOps(staged.stored) case 1: ops = newEnsureStagedResourceSameOps(staged.stored) default: return nil, errors.NewAlreadyExists(nil, "already staged") } if staged.stored.PendingID == "" { // Only non-pending resources must have an existing service. ops = append(ops, staged.base.ServiceExistsOps(staged.stored.ServiceID)...) } return ops, nil } if err := staged.base.Run(buildTxn); err != nil { return errors.Trace(err) } return nil }