1
0
mirror of https://github.com/minio/mc.git synced 2025-11-13 12:22:45 +03:00

second update to error messages

This commit is contained in:
Anand Babu (AB) Periasamy
2015-08-22 02:14:00 -07:00
parent 2e1c97f83b
commit b10d2648e8
4 changed files with 45 additions and 50 deletions

View File

@@ -19,6 +19,8 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/minio/mc/internal/github.com/minio/minio/pkg/probe"
) )
// CopyMessage container for file copy messages // CopyMessage container for file copy messages
@@ -34,8 +36,7 @@ func (c CopyMessage) String() string {
return fmt.Sprintf("%s -> %s\n", c.Source, c.Target) return fmt.Sprintf("%s -> %s\n", c.Source, c.Target)
} }
copyMessageBytes, err := json.Marshal(c) copyMessageBytes, err := json.Marshal(c)
if err != nil { fatalIf(probe.NewError(err), "Failed to marshal copy message.")
panic(err)
}
return string(copyMessageBytes) + "\n" return string(copyMessageBytes) + "\n"
} }

View File

@@ -159,14 +159,14 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) {
break break
} }
if cpURLs.Error != nil { if cpURLs.Error != nil {
errorIf(cpURLs.Error, "Failed to prepare copy URLs.") errorIf(cpURLs.Error.Trace(), "Unable to prepare URLs for copying.")
break break
} }
jsonData, err := json.Marshal(cpURLs) jsonData, err := json.Marshal(cpURLs)
if err != nil { if err != nil {
session.Close() session.Close()
console.Fatalf("Unable to marshal URLs to JSON. %s\n", err) fatalIf(probe.NewError(err), "Unable to prepare URLs for copying. Error in JSON marshaling.")
} }
fmt.Fprintln(dataFP, string(jsonData)) fmt.Fprintln(dataFP, string(jsonData))
if !globalQuietFlag && !globalJSONFlag { if !globalQuietFlag && !globalJSONFlag {
@@ -230,7 +230,7 @@ func doCopyCmdSession(session *sessionV2) {
session.Header.LastCopied = cpURLs.SourceContent.Name session.Header.LastCopied = cpURLs.SourceContent.Name
} else { } else {
console.Println() console.Println()
errorIf(cpURLs.Error, fmt.Sprintf("Failed to copy %s. ", cpURLs.SourceContent.Name)) errorIf(cpURLs.Error.Trace(), fmt.Sprintf("Failed to copy %s.", cpURLs.SourceContent.Name))
} }
case <-trapCh: // Receive interrupt notification. case <-trapCh: // Receive interrupt notification.
session.Close() session.Close()
@@ -274,22 +274,22 @@ func mainCopy(ctx *cli.Context) {
session := newSessionV2() session := newSessionV2()
var err error var e error
session.Header.CommandType = "cp" session.Header.CommandType = "cp"
session.Header.RootPath, err = os.Getwd() session.Header.RootPath, e = os.Getwd()
if err != nil { if e != nil {
session.Close() session.Close()
session.Delete() session.Delete()
console.Fatalf("Unable to get current working folder. %s\n", err) fatalIf(probe.NewError(e), "Unable to get current working folder.")
} }
// extract URLs. // extract URLs.
var perr *probe.Error var err *probe.Error
session.Header.CommandArgs, perr = args2URLs(ctx.Args()) session.Header.CommandArgs, err = args2URLs(ctx.Args())
if perr != nil { if err != nil {
session.Close() session.Close()
session.Delete() session.Delete()
console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), perr) fatalIf(err.Trace(), "One or more unknown URL types passed.")
} }
doCopyCmdSession(session) doCopyCmdSession(session)

View File

@@ -17,13 +17,14 @@
package main package main
import ( import (
"errors"
"fmt"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/minio/mc/internal/github.com/minio/cli" "github.com/minio/mc/internal/github.com/minio/cli"
"github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe"
"github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/client"
"github.com/minio/mc/pkg/console"
) )
type copyURLs struct { type copyURLs struct {
@@ -63,9 +64,7 @@ func checkCopySyntax(ctx *cli.Context) {
} }
// extract URLs. // extract URLs.
URLs, err := args2URLs(ctx.Args()) URLs, err := args2URLs(ctx.Args())
if err != nil { fatalIf(err.Trace(ctx.Args()...), fmt.Sprintf("One or more unknown URL types passed."))
console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), err.Trace())
}
srcURLs := URLs[:len(URLs)-1] srcURLs := URLs[:len(URLs)-1]
tgtURL := URLs[len(URLs)-1] tgtURL := URLs[len(URLs)-1]
@@ -73,17 +72,17 @@ func checkCopySyntax(ctx *cli.Context) {
/****** Generic rules *******/ /****** Generic rules *******/
// Recursive URLs are not allowed in target. // Recursive URLs are not allowed in target.
if isURLRecursive(tgtURL) { if isURLRecursive(tgtURL) {
console.Fatalf("Recursive option is not supported for target %s argument. %s\n", tgtURL, probe.NewError(errInvalidArgument)) fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Recursive option is not supported for target %s argument.", tgtURL))
} }
// scope locally // scope locally
{ {
url, err := client.Parse(tgtURL) url, err := client.Parse(tgtURL)
if err != nil { if err != nil {
console.Fatalf("Unable to parse target %s argument. %s\n", tgtURL, probe.NewError(err)) fatalIf(probe.NewError(err), fmt.Sprintf("Unable to parse target %s argument.", tgtURL))
} }
if url.Host != "" { if url.Host != "" {
if url.Path == string(url.Separator) { if url.Path == string(url.Separator) {
console.Fatalf("Bucket creation detected for %s, cloud storage URL's should use mc mb to create buckets\n", tgtURL) fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Bucket creation detected for %s, cloud storage URL's should use mc mb to create buckets.", tgtURL))
} }
} }
} }
@@ -97,48 +96,49 @@ func checkCopySyntax(ctx *cli.Context) {
case copyURLsTypeD: // File | Folder... -> Folder. case copyURLsTypeD: // File | Folder... -> Folder.
checkCopySyntaxTypeD(srcURLs, tgtURL) checkCopySyntaxTypeD(srcURLs, tgtURL)
default: default:
console.Fatalln("Invalid arguments. Unable to determine how to copy. Please report this issue at https://github.com/minio/mc/issues") fatalIf(probe.NewError(errInvalidArgument), "Invalid arguments to copy command.")
} }
} }
// checkCopySyntaxTypeA verifies if the source and target are valid file arguments. // checkCopySyntaxTypeA verifies if the source and target are valid file arguments.
func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) { func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) {
if len(srcURLs) != 1 { if len(srcURLs) != 1 {
console.Fatalf("Invalid number of source arguments to copy command. %s\n", probe.NewError(errInvalidArgument)) fatalIf(probe.NewError(errInvalidArgument), "Invalid number of source arguments to copy command.")
} }
srcURL := srcURLs[0] srcURL := srcURLs[0]
_, srcContent, err := url2Stat(srcURL) _, srcContent, err := url2Stat(srcURL)
fatalIf(err.Trace(), "Unable to stat source URL")
fatalIf(err.Trace(srcURL), "Unable to stat source "+srcURL+".")
if srcContent.Type.IsDir() { if srcContent.Type.IsDir() {
console.Fatalf("Source %s is a folder. Use %s... argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument) fatalIf(probe.NewError(errInvalidArgument), fmt.Sprintf("Source %s is a folder. Use %s... argument to copy this folder and its contents recursively.", srcURL, srcURL))
} }
if !srcContent.Type.IsRegular() { if !srcContent.Type.IsRegular() {
fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL}), "Invalid source type") fatalIf(probe.NewError(errInvalidArgument), "Source "+srcURL+" is not a file.")
} }
} }
// checkCopySyntaxTypeB verifies if the source is a valid file and target is a valid dir. // checkCopySyntaxTypeB verifies if the source is a valid file and target is a valid dir.
func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) { func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) {
if len(srcURLs) != 1 { if len(srcURLs) != 1 {
console.Fatalf("Invalid number of source arguments to copy command. %s\n", errInvalidArgument) fatalIf(probe.NewError(errInvalidArgument), "Invalid number of source arguments to copy command.")
} }
srcURL := srcURLs[0] srcURL := srcURLs[0]
_, srcContent, err := url2Stat(srcURL) _, srcContent, err := url2Stat(srcURL)
fatalIf(err.Trace(), "Unable to stat source URL") fatalIf(err.Trace(srcURL), "Unable to stat source "+srcURL+".")
if srcContent.Type.IsDir() { if srcContent.Type.IsDir() {
console.Fatalf("Source %s is a folder. Use %s... argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument) fatalIf(probe.NewError(errInvalidArgument), fmt.Sprintf("Source %s is a folder. Use %s... argument to copy this folder and its contents recursively.", srcURL, srcURL))
} }
if !srcContent.Type.IsRegular() { if !srcContent.Type.IsRegular() {
fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL}), "Invalid source type") fatalIf(probe.NewError(errInvalidArgument), "Source "+srcURL+" is not a file.")
} }
_, tgtContent, err := url2Stat(tgtURL) _, tgtContent, err := url2Stat(tgtURL)
// Target exist?. // Target exist?.
if err == nil { if err == nil {
if !tgtContent.Type.IsDir() { if !tgtContent.Type.IsDir() {
fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Invalid target type") fatalIf(probe.NewError(errInvalidArgument), "Target "+tgtURL+" is not a folder.")
} }
} }
} }
@@ -146,21 +146,21 @@ func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) {
// checkCopySyntaxTypeC verifies if the source is a valid recursive dir and target is a valid dir. // checkCopySyntaxTypeC verifies if the source is a valid recursive dir and target is a valid dir.
func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) { func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) {
if len(srcURLs) != 1 { if len(srcURLs) != 1 {
console.Fatalf("Invalid number of source arguments to copy command. %s\n", errInvalidArgument) fatalIf(probe.NewError(errInvalidArgument), "Invalid number of source arguments to copy command.")
} }
srcURL := srcURLs[0] srcURL := srcURLs[0]
srcURL = stripRecursiveURL(srcURL) srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL) _, srcContent, err := url2Stat(srcURL)
fatalIf(err.Trace(), "Unable to stat source URL") fatalIf(err.Trace(srcURL), "Unable to stat source "+srcURL+".")
if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. if srcContent.Type.IsRegular() { // Ellipses is supported only for folders.
fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL}), "Invalid source type") fatalIf(probe.NewError(errInvalidArgument), "Source "+srcURL+" is not a folder.")
} }
_, tgtContent, err := url2Stat(tgtURL) _, tgtContent, err := url2Stat(tgtURL)
// Target exist?. // Target exist?.
if err == nil { if err == nil {
if !tgtContent.Type.IsDir() { if !tgtContent.Type.IsDir() {
fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Invalid target type") fatalIf(probe.NewError(errInvalidArgument), "Target "+tgtURL+" is not a folder.")
} }
} }
} }
@@ -171,20 +171,20 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) {
if isURLRecursive(srcURL) { if isURLRecursive(srcURL) {
srcURL = stripRecursiveURL(srcURL) srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL) _, srcContent, err := url2Stat(srcURL)
fatalIf(err.Trace(), "Unable to stat source URL") fatalIf(err.Trace(srcURL), "Unable to stat source "+srcURL+".")
if !srcContent.Type.IsDir() { // Ellipses is supported only for folders. if !srcContent.Type.IsDir() { // Ellipses is supported only for folders.
fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL}), "Invalid source type") fatalIf(probe.NewError(errInvalidArgument), "Source "+srcURL+" is not a folder.")
} }
} else { // Regular URL. } else { // Regular URL.
_, srcContent, err := url2Stat(srcURL) _, srcContent, err := url2Stat(srcURL)
fatalIf(err.Trace(), "Unable to stat source URL") fatalIf(err.Trace(srcURL), "Unable to stat source "+srcURL+".")
if srcContent.Type.IsDir() { if srcContent.Type.IsDir() {
console.Fatalf("Source %s is a folder. Use %s... argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument) fatalIf(probe.NewError(errInvalidArgument), fmt.Sprintf("Source %s is a folder. Use %s... argument to copy this folder and its contents recursively.", srcURL, srcURL))
} }
if !srcContent.Type.IsRegular() { if !srcContent.Type.IsRegular() {
fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL}), "Unable to read from source url") fatalIf(probe.NewError(errInvalidArgument), "Source "+srcURL+" is not a file.")
} }
} }
} }
@@ -192,7 +192,7 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) {
// Target exist?. // Target exist?.
if err == nil { if err == nil {
if !tgtContent.Type.IsDir() { if !tgtContent.Type.IsDir() {
fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Unable to write to target url") fatalIf(probe.NewError(errInvalidArgument), "Target "+tgtURL+" is not a folder.")
} }
} }
} }
@@ -233,7 +233,7 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) copyURLs {
_, sourceContent, err := url2Stat(sourceURL) _, sourceContent, err := url2Stat(sourceURL)
if err != nil { if err != nil {
// Source does not exist or insufficient privileges. // Source does not exist or insufficient privileges.
return copyURLs{Error: err.Trace()} return copyURLs{Error: err.Trace(sourceURL)}
} }
if !sourceContent.Type.IsRegular() { if !sourceContent.Type.IsRegular() {
// Source is not a regular file // Source is not a regular file
@@ -250,7 +250,7 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) copyURLs {
_, sourceContent, err := url2Stat(sourceURL) _, sourceContent, err := url2Stat(sourceURL)
if err != nil { if err != nil {
// Source does not exist or insufficient privileges. // Source does not exist or insufficient privileges.
return copyURLs{Error: err.Trace()} return copyURLs{Error: err.Trace(sourceURL)}
} }
if !sourceContent.Type.IsRegular() { if !sourceContent.Type.IsRegular() {
// Source is not a regular file. // Source is not a regular file.
@@ -290,7 +290,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs {
sourceClient, sourceContent, err := url2Stat(sourceURL) sourceClient, sourceContent, err := url2Stat(sourceURL)
if err != nil { if err != nil {
// Source does not exist or insufficient privileges. // Source does not exist or insufficient privileges.
copyURLsCh <- copyURLs{Error: err.Trace()} copyURLsCh <- copyURLs{Error: err.Trace(sourceURL)}
return return
} }

View File

@@ -91,10 +91,4 @@ func (e errSourceIsNotDir) Error() string {
return "Source " + e.URL + " is not a folder." return "Source " + e.URL + " is not a folder."
} }
type errSourceIsNotFile errTargetIsNotDir
func (e errSourceIsNotFile) Error() string {
return "Source " + e.URL + " is not a file."
}
var errSourceListEmpty = errors.New("Source list is empty.") var errSourceListEmpty = errors.New("Source list is empty.")