Improve error text for unauthorized add commands #6377

Merged
merged 3 commits into from Oct 11, 2016

Conversation

Projects
None yet
4 participants
Contributor

macgreagoir commented Oct 4, 2016

The following commands now output more user-friendly responses,
mentioning the use of juju grant, when the API returns an
"unauthorized access" error.

  • add-machine
  • add-model
  • add-relation
  • add-space
  • add-storage
  • add-subnet
  • add-unit
  • add-user

Fixes lp:1612046

QA steps:

  • Add both a new local user and a new Juju user
    • juju add-user foo and juju grant foo read default
    • sudo adduser foo and sudo su - foo
  • As the new local user foo,
    • juju register <as output by juju register>
    • juju switch <controller>:admin@local/default
  • Now that you are logged into Juju as the Juju user foo, test each of the list commands. When each errors for lack of permissions, you should see an error message mentioning the use of juju grant.
cmd/juju/common/errors.go
+ command = "complete this operation"
+ }
+ permMsg := fmt.Sprintf(permMsg, command)
+ return errors.Errorf("%v\n\n%s\n%s\n", err, permMsg, grantMsg)
@howbazaar

howbazaar Oct 5, 2016

Owner

You should probably wrap the error to keep the stack.
Also, should you check that err is not nil?

newErr := errors.Errorf("%v\n\n%s\n%s\n", err, permMsg, grantMsg)
return errors.Wrap(err, newErr)

So I talked this over with @howbazaar some more. I think this might be a better way of doing things.

Rather than trying to shove a message we want printed out onto stderr in an error type, let's instead create a function that takes in io.Writer (potentially ansiterm.Writer) and writes out both the friendly message and the underlying error to the user.

Then return errors.Wrap(err, cmd.ErrSilent) so that the command returns nothing further.

cmd/juju/common/errors.go
+ stdout.Write([]byte(
+ fmt.Sprintf("\n%s\n%s\n\n", permMsg, grantMsg)),
+ )
+ return err
@macgreagoir

macgreagoir Oct 6, 2016

Contributor

Returning errors.Wrap(err, cmd.ErrSilent) here shows a potentially confusing error , unless the user redirects stderr [to /dev/null]. It seemed appropriate to just return the err, with the friendlier message written to stdout. I stand to be corrected :-)

@macgreagoir

macgreagoir Oct 7, 2016

Contributor

...and I've reworked it slightly again, leaving the caller to return err, and letting the new function just print the nicer message.

macgreagoir added some commits Oct 4, 2016

Improve error text for unauthorized add commands
The following commands now output more user-friendly responses,
mentioning the use of `juju grant`, when the API returns an
"unauthorized access" error.

 * add-machine
 * add-model
 * add-relation
 * add-space
 * add-storage
 * add-subnet
 * add-unit
 * add-user

Fixes lp:1612046
Print PermissionsError message to stdout
Changes the cmd/juju/common PermissionsError() function to print the
user-friendly message to stdout, while still returning the "permission
denied" error.
Print PermissionsMessage to stderr
Do not needlessly handle the err argument in PermissionsMessage (renamed
from PermissionsError). Instead only use PermssionsMessage to write the
user-friendly message to stderr, leaving the caller to return the err.

kat-co approved these changes Oct 11, 2016

LGTM!

+ if command == "" {
+ command = "complete this operation"
+ }
+ fmt.Fprintf(writer, "\n%s\n%s\n\n", fmt.Sprintf(perm, command), grant)
@kat-co

kat-co Oct 11, 2016

Contributor

Nitpick that shouldn't block landing:

This reads a little weird. I think it would be more common to see this as a series of Fprintfs.

Contributor

macgreagoir commented Oct 11, 2016

$$merge$$

Contributor

jujubot commented Oct 11, 2016

Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju

@jujubot jujubot merged commit 60da3f0 into juju:master Oct 11, 2016

@macgreagoir macgreagoir deleted the macgreagoir:unauth-err branch Oct 11, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment