Simplify and enhance error strings
This commit is contained in:
+1
-2
@@ -70,8 +70,7 @@ func runAccessCmd(ctx *cli.Context) {
|
||||
targetURL, err := getExpandedURL(arg, config.Aliases)
|
||||
fatalIf(err)
|
||||
|
||||
err = doUpdateAccessCmd(targetURL, acl)
|
||||
fatalIf(err)
|
||||
fatalIf(doUpdateAccessCmd(targetURL, acl))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-2
@@ -27,8 +27,7 @@ import (
|
||||
// validAliasURL: use net/url.Parse to validate
|
||||
var validAliasName = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9-]+$")
|
||||
|
||||
// Check if it is an aliased URL
|
||||
func isValidAliasName(aliasName string) bool {
|
||||
func isAliasReserved(aliasName string) bool {
|
||||
// help is reserved argument
|
||||
switch aliasName {
|
||||
case "help":
|
||||
@@ -40,8 +39,14 @@ func isValidAliasName(aliasName string) bool {
|
||||
case "public":
|
||||
fallthrough
|
||||
case "authenticated":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Check if it is an aliased URL
|
||||
func isValidAliasName(aliasName string) bool {
|
||||
return validAliasName.MatchString(aliasName)
|
||||
}
|
||||
|
||||
|
||||
+7
-7
@@ -70,19 +70,19 @@ func (s *CmdTestSuite) SetUpSuite(c *C) {
|
||||
perr = createSessionDir()
|
||||
c.Assert(perr, IsNil)
|
||||
|
||||
_, perr = doConfig("invalid", nil)
|
||||
perr = doConfig("invalid", nil)
|
||||
c.Assert(perr, Not(IsNil))
|
||||
|
||||
_, perr = doConfig("alias", []string{"test", "https://test.io"})
|
||||
perr = doConfig("alias", []string{"test", "https://test.io"})
|
||||
c.Assert(perr, IsNil)
|
||||
|
||||
_, perr = doConfig("alias", []string{"test", "https://test.io"})
|
||||
perr = doConfig("alias", []string{"test", "https://test.io"})
|
||||
c.Assert(perr, Not(IsNil))
|
||||
|
||||
_, perr = doConfig("alias", []string{"test", "htt://test.io"})
|
||||
perr = doConfig("alias", []string{"test", "htt://test.io"})
|
||||
c.Assert(perr, Not(IsNil))
|
||||
|
||||
_, perr = doConfig("alias", []string{"readonly", "https://new.test.io"})
|
||||
perr = doConfig("alias", []string{"readonly", "https://new.test.io"})
|
||||
c.Assert(perr, Not(IsNil))
|
||||
|
||||
app = registerApp()
|
||||
@@ -261,8 +261,8 @@ func (s *CmdTestSuite) TestIsvalidAliasName(c *C) {
|
||||
c.Check(isValidAliasName("h0SFD2k2#Fdsa"), Equals, false)
|
||||
c.Check(isValidAliasName("0dslka-4"), Equals, false)
|
||||
c.Check(isValidAliasName("-fdslka"), Equals, false)
|
||||
c.Check(isValidAliasName("help"), Equals, false)
|
||||
c.Check(isValidAliasName("private"), Equals, false) // reserved names
|
||||
c.Check(isAliasReserved("help"), Equals, true)
|
||||
c.Check(isAliasReserved("private"), Equals, true) // reserved names
|
||||
}
|
||||
|
||||
func (s *CmdTestSuite) TestEmptyExpansions(c *C) {
|
||||
|
||||
+11
-12
@@ -35,20 +35,19 @@ func isTargetURLDir(targetURL string) bool {
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
{
|
||||
_, targetContent, err := url2Stat(targetURL)
|
||||
if err != nil {
|
||||
if targetURLParse.Path == string(targetURLParse.Separator) && targetURLParse.Scheme != "" {
|
||||
return false
|
||||
}
|
||||
if strings.HasSuffix(targetURLParse.Path, string(targetURLParse.Separator)) {
|
||||
return true
|
||||
}
|
||||
|
||||
_, targetContent, perr := url2Stat(targetURL)
|
||||
if perr != nil {
|
||||
if targetURLParse.Path == string(targetURLParse.Separator) && targetURLParse.Scheme != "" {
|
||||
return false
|
||||
}
|
||||
if !targetContent.Type.IsDir() { // Target is a dir.
|
||||
return false
|
||||
if strings.HasSuffix(targetURLParse.Path, string(targetURLParse.Separator)) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
if !targetContent.Type.IsDir() { // Target is a dir.
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -166,7 +165,7 @@ func getNewClient(urlStr string, auth hostConfig) (client.Client, *probe.Error)
|
||||
case client.Filesystem:
|
||||
return fs.New(urlStr)
|
||||
}
|
||||
return nil, probe.NewError(errInvalidURL{URL: urlStr})
|
||||
return nil, probe.NewError(errInitClient{url: urlStr})
|
||||
}
|
||||
|
||||
// url2Stat - Returns client, config and its stat Content from the URL
|
||||
|
||||
+14
-34
@@ -66,9 +66,12 @@ func runConfigCmd(ctx *cli.Context) {
|
||||
if len(tailArgs) > 2 {
|
||||
console.Fatalf("Incorrect number of arguments, please read \"mc config help\". %s", errInvalidArgument{})
|
||||
}
|
||||
msg, err := doConfig(arg, tailArgs)
|
||||
configPath, err := getMcConfigPath()
|
||||
fatalIf(err)
|
||||
console.Infoln(msg)
|
||||
|
||||
fatalIf(doConfig(arg, tailArgs))
|
||||
// upon success
|
||||
console.Infoln("Alias written successfully to [" + configPath + "].")
|
||||
}
|
||||
|
||||
// saveConfig writes configuration data in json format to config file.
|
||||
@@ -79,41 +82,15 @@ func saveConfig(arg string, aliases []string) *probe.Error {
|
||||
if err != nil {
|
||||
return err.Trace()
|
||||
}
|
||||
return writeConfig(config)
|
||||
return writeConfig(config).Trace()
|
||||
default:
|
||||
return probe.NewError(errInvalidArgument{})
|
||||
}
|
||||
}
|
||||
|
||||
// doConfig is the handler for "mc config" sub-command.
|
||||
func doConfig(arg string, aliases []string) (string, *probe.Error) {
|
||||
configPath, err := getMcConfigPath()
|
||||
if err != nil {
|
||||
return "Unable to determine config file path.", err.Trace()
|
||||
}
|
||||
err = saveConfig(arg, aliases)
|
||||
if err != nil {
|
||||
switch err.ToError().(type) {
|
||||
case errConfigExists:
|
||||
return "Configuration file [" + configPath + "]", err.Trace()
|
||||
case errInvalidArgument:
|
||||
return "Incorrect usage, please use \"mc config help\" ", err.Trace()
|
||||
case errAliasExists:
|
||||
return "Alias name: [" + aliases[0] + "]", err.Trace()
|
||||
case errInvalidAliasName:
|
||||
return "Alias [" + aliases[0] + "] is reserved word or invalid", err.Trace()
|
||||
|
||||
case errInvalidURL:
|
||||
return "Alias [" + aliases[1] + "] is invalid URL", err.Trace()
|
||||
default:
|
||||
// unexpected error
|
||||
return "Unable to modify config file [" + configPath + "].", err.Trace()
|
||||
}
|
||||
}
|
||||
if arg == "alias" {
|
||||
return "Alias written to [" + configPath + "].", nil
|
||||
}
|
||||
return "", probe.NewError(errUnexpected{})
|
||||
func doConfig(arg string, aliases []string) *probe.Error {
|
||||
return saveConfig(arg, aliases).Trace()
|
||||
}
|
||||
|
||||
// addAlias - add new aliases
|
||||
@@ -134,18 +111,21 @@ func addAlias(aliases []string) (quick.Config, *probe.Error) {
|
||||
aliasName := aliases[0]
|
||||
url := strings.TrimSuffix(aliases[1], "/")
|
||||
if strings.HasPrefix(aliasName, "http") {
|
||||
return nil, probe.NewError(errInvalidAliasName{name: aliasName})
|
||||
return nil, probe.NewError(errInvalidAliasName{alias: aliasName})
|
||||
}
|
||||
if !strings.HasPrefix(url, "http") {
|
||||
return nil, probe.NewError(errInvalidURL{URL: url})
|
||||
}
|
||||
if isAliasReserved(aliasName) {
|
||||
return nil, probe.NewError(errReservedAliasName{alias: aliasName})
|
||||
}
|
||||
if !isValidAliasName(aliasName) {
|
||||
return nil, probe.NewError(errInvalidAliasName{name: aliasName})
|
||||
return nil, probe.NewError(errInvalidAliasName{alias: aliasName})
|
||||
}
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV1)
|
||||
if _, ok := newConf.Aliases[aliasName]; ok {
|
||||
return nil, probe.NewError(errAliasExists{})
|
||||
return nil, probe.NewError(errAliasExists{alias: aliasName})
|
||||
}
|
||||
newConf.Aliases[aliasName] = url
|
||||
newConfig, err := quick.New(newConf)
|
||||
|
||||
@@ -204,12 +204,10 @@ func migrateConfigV1ToV101() {
|
||||
conf.Hosts["*.s3*.amazonaws.com"] = s3HostConf
|
||||
}
|
||||
|
||||
{
|
||||
newConfig, err := quick.New(conf)
|
||||
fatalIf(err)
|
||||
err = newConfig.Save(mustGetMcConfigPath())
|
||||
fatalIf(err)
|
||||
}
|
||||
newConfig, perr := quick.New(conf)
|
||||
fatalIf(perr)
|
||||
perr = newConfig.Save(mustGetMcConfigPath())
|
||||
fatalIf(perr)
|
||||
|
||||
console.Infof("Successfully migrated %s from version: %s to version: %s\n", mustGetMcConfigPath(), mcPreviousConfigVersion, mcCurrentConfigVersion)
|
||||
}
|
||||
|
||||
@@ -221,6 +221,7 @@ func doCopyCmdSession(session *sessionV2) {
|
||||
session.Header.LastCopied = cpURLs.SourceContent.Name
|
||||
} else {
|
||||
console.Println()
|
||||
console.Printf("Failed to copy ‘%s’, ", cpURLs.SourceContent.Name)
|
||||
errorIf(cpURLs.Error)
|
||||
}
|
||||
case <-trapCh: // Receive interrupt notification.
|
||||
|
||||
+1
-1
@@ -57,5 +57,5 @@ func getHostConfig(URL string) (hostConfig, *probe.Error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return hostConfig{}, probe.NewError(errNoMatchingHost{})
|
||||
return hostConfig{}, probe.NewError(errNoMatchingHost{url: URL})
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ func doList(clnt client.Client, recursive bool) *probe.Error {
|
||||
errorIf(contentCh.Err)
|
||||
continue
|
||||
}
|
||||
err = contentCh.Err
|
||||
err = contentCh.Err.Trace()
|
||||
break
|
||||
}
|
||||
console.Print(parseContent(contentCh.Content))
|
||||
|
||||
@@ -221,6 +221,7 @@ func doMirrorCmdSession(session *sessionV2) {
|
||||
session.Header.LastCopied = sURLs.SourceContent.Name
|
||||
} else {
|
||||
console.Println()
|
||||
console.Printf("Failed to mirror ‘%s’, ", sURLs.SourceContent.Name)
|
||||
errorIf(sURLs.Error)
|
||||
}
|
||||
case <-trapCh: // Receive interrupt notification.
|
||||
|
||||
+11
-14
@@ -170,7 +170,6 @@ func loadSessionV2(sid string) (*sessionV2, *probe.Error) {
|
||||
return nil, probe.NewError(errInvalidArgument{})
|
||||
}
|
||||
sessionFile := getSessionFile(sid)
|
||||
|
||||
if _, err := os.Stat(sessionFile); err != nil {
|
||||
return nil, probe.NewError(err)
|
||||
}
|
||||
@@ -179,26 +178,24 @@ func loadSessionV2(sid string) (*sessionV2, *probe.Error) {
|
||||
s.Header = &sessionV2Header{}
|
||||
s.SessionID = sid
|
||||
s.Header.Version = "1.1.0"
|
||||
qs, err := quick.New(s.Header)
|
||||
if err != nil {
|
||||
return nil, err.Trace()
|
||||
qs, perr := quick.New(s.Header)
|
||||
if perr != nil {
|
||||
return nil, perr.Trace()
|
||||
}
|
||||
err = qs.Load(sessionFile)
|
||||
if err != nil {
|
||||
return nil, err.Trace()
|
||||
perr = qs.Load(sessionFile)
|
||||
if perr != nil {
|
||||
return nil, perr.Trace()
|
||||
}
|
||||
|
||||
s.mutex = new(sync.Mutex)
|
||||
s.Header = qs.Data().(*sessionV2Header)
|
||||
|
||||
{
|
||||
var err error
|
||||
s.DataFP, err = os.Open(getSessionDataFile(s.SessionID))
|
||||
if err != nil {
|
||||
console.Fatalf("Unable to open session data file \""+getSessionDataFile(s.SessionID)+"\". %s\n", err)
|
||||
}
|
||||
var err error
|
||||
s.DataFP, err = os.Open(getSessionDataFile(s.SessionID))
|
||||
if err != nil {
|
||||
// fail proactively here
|
||||
console.Fatalf("Unable to open session data file \""+getSessionDataFile(s.SessionID)+"\". %s\n", err)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
|
||||
+29
-21
@@ -35,7 +35,7 @@ type errInvalidACL struct {
|
||||
}
|
||||
|
||||
func (e errInvalidACL) Error() string {
|
||||
return "Invalid ACL Type ‘" + e.acl + "’."
|
||||
return "Invalid ACL Type ‘" + e.acl + "’. Valid types are [private, public, readonly]."
|
||||
}
|
||||
|
||||
type errNotConfigured struct{}
|
||||
@@ -49,13 +49,13 @@ type errNotAnObject struct {
|
||||
}
|
||||
|
||||
func (e errNotAnObject) Error() string {
|
||||
return "Not an object " + e.url
|
||||
return "URL: ‘" + e.url + "’ not an object"
|
||||
}
|
||||
|
||||
type errInvalidArgument struct{}
|
||||
|
||||
func (e errInvalidArgument) Error() string {
|
||||
return "Invalid argument."
|
||||
return "Incorrect usage, please use \"mc config help\""
|
||||
}
|
||||
|
||||
type errUnsupportedScheme struct {
|
||||
@@ -64,7 +64,7 @@ type errUnsupportedScheme struct {
|
||||
}
|
||||
|
||||
func (e errUnsupportedScheme) Error() string {
|
||||
return "Unsuppported URL scheme: " + e.scheme
|
||||
return "Unsuppported URL scheme: " + e.scheme + " URL: ‘" + e.url + "’"
|
||||
}
|
||||
|
||||
type errInvalidGlobURL struct {
|
||||
@@ -76,37 +76,45 @@ func (e errInvalidGlobURL) Error() string {
|
||||
return "Error reading glob URL " + e.glob + " while comparing with " + e.request
|
||||
}
|
||||
|
||||
type errReservedAliasName struct {
|
||||
alias string
|
||||
}
|
||||
|
||||
func (e errReservedAliasName) Error() string {
|
||||
return "Alias name ‘" + e.alias + "’ is a reserved word, reserved words are [help, private, readonly, public, authenticated]"
|
||||
}
|
||||
|
||||
type errInvalidAliasName struct {
|
||||
name string
|
||||
alias string
|
||||
}
|
||||
|
||||
func (e errInvalidAliasName) Error() string {
|
||||
return "Not a valid alias name: " + e.name + " valid examples are: Area51, Grand-Nagus.."
|
||||
return "Alias name ‘" + e.alias + "’ is invalid, valid examples are: Area51, Grand-Nagus.."
|
||||
}
|
||||
|
||||
type errInvalidAuth struct{}
|
||||
|
||||
func (e errInvalidAuth) Error() string {
|
||||
return "Invalid auth keys"
|
||||
type errNoMatchingHost struct {
|
||||
url string
|
||||
}
|
||||
|
||||
type errNoMatchingHost struct{}
|
||||
|
||||
func (e errNoMatchingHost) Error() string {
|
||||
return "No matching host found."
|
||||
}
|
||||
|
||||
type errConfigExists struct{}
|
||||
|
||||
func (e errConfigExists) Error() string {
|
||||
return "Already exists."
|
||||
return "No matching host found for the given url ‘" + e.url + "’"
|
||||
}
|
||||
|
||||
// errAliasExists - alias exists
|
||||
type errAliasExists struct{}
|
||||
type errAliasExists struct {
|
||||
alias string
|
||||
}
|
||||
|
||||
func (e errAliasExists) Error() string {
|
||||
return "Already exists."
|
||||
return "Alias name: ‘" + e.alias + "’ already exists."
|
||||
}
|
||||
|
||||
type errInitClient struct {
|
||||
url string
|
||||
}
|
||||
|
||||
func (e errInitClient) Error() string {
|
||||
return "Unable to initialize client for url ‘" + e.url + "’"
|
||||
}
|
||||
|
||||
type errInvalidURL struct {
|
||||
|
||||
Reference in New Issue
Block a user