thread-safe typed errors

This commit is contained in:
Anand Babu (AB) Periasamy
2015-08-24 12:55:11 -07:00
parent 3d69860b5e
commit 831db52dcf
12 changed files with 51 additions and 45 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ func mainAccess(ctx *cli.Context) {
config := mustGetMcConfig()
acl := bucketACL(ctx.Args().First())
if !acl.isValidBucketACL() {
fatalIf(errDummy.Trace(),
fatalIf(errDummy().Trace(),
"Unrecognized permission ‘"+acl.String()+"’. Allowed values are [private, public, readonly].")
}
for _, arg := range ctx.Args().Tail() {
+6 -6
View File
@@ -95,7 +95,7 @@ func mainConfig(ctx *cli.Context) {
arg := ctx.Args().First()
tailArgs := ctx.Args().Tail()
if len(tailArgs) > 2 {
fatalIf(errDummy.Trace(), "Incorrect number of arguments to config command. Please read ‘mc config help’")
fatalIf(errDummy().Trace(), "Incorrect number of arguments to config command. Please read ‘mc config help’")
}
switch arg {
@@ -129,7 +129,7 @@ func mainConfig(ctx *cli.Context) {
// addAlias - add new aliases
func addAlias(alias, url string) {
if alias == "" || url == "" {
fatalIf(errDummy.Trace(), "Alias or URL cannot be empty.")
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
}
conf := newConfigV2()
config, err := quick.New(conf)
@@ -140,18 +140,18 @@ func addAlias(alias, url string) {
url = strings.TrimSuffix(url, "/")
if !strings.HasPrefix(url, "http") {
fatalIf(errDummy.Trace(), fmt.Sprintf("Invalid alias URL ‘%s’. Valid examples are: http://s3.amazonaws.com, https://yourbucket.example.com.", url))
fatalIf(errDummy().Trace(), fmt.Sprintf("Invalid alias URL ‘%s’. Valid examples are: http://s3.amazonaws.com, https://yourbucket.example.com.", url))
}
if isAliasReserved(alias) {
fatalIf(errDummy.Trace(), fmt.Sprintf("Cannot use a reserved name ‘%s’ as an alias. Following are reserved names: [help, private, readonly, public, authenticated].", alias))
fatalIf(errDummy().Trace(), fmt.Sprintf("Cannot use a reserved name ‘%s’ as an alias. Following are reserved names: [help, private, readonly, public, authenticated].", alias))
}
if !isValidAliasName(alias) {
fatalIf(errDummy.Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias))
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias))
}
// convert interface{} back to its original struct
newConf := config.Data().(*configV2)
if oldURL, ok := newConf.Aliases[alias]; ok {
fatalIf(errDummy.Trace(), fmt.Sprintf("Alias ‘%s’ already exists for ‘%s’.", alias, oldURL))
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ already exists for ‘%s’.", alias, oldURL))
}
newConf.Aliases[alias] = url
newConfig, err := quick.New(newConf)
+2 -2
View File
@@ -107,7 +107,7 @@ func mustGetMcConfigPath() string {
// getMcConfig - reads configuration file and returns config
func getMcConfig() (*configV2, *probe.Error) {
if !isMcConfigExists() {
return nil, errInvalidArgument.Trace()
return nil, errInvalidArgument().Trace()
}
configFile, err := getMcConfigPath()
@@ -158,7 +158,7 @@ func isMcConfigExists() bool {
// writeConfig - write configuration file
func writeConfig(config quick.Config) *probe.Error {
if config == nil {
return errInvalidArgument.Trace()
return errInvalidArgument().Trace()
}
err := createMcConfigDir()
if err != nil {
+19 -19
View File
@@ -71,7 +71,7 @@ func checkCopySyntax(ctx *cli.Context) {
/****** Generic rules *******/
// Recursive URLs are not allowed in target.
if isURLRecursive(tgtURL) {
fatalIf(errDummy.Trace(), fmt.Sprintf("Recursive option is not supported for target ‘%s’ argument.", tgtURL))
fatalIf(errDummy().Trace(), fmt.Sprintf("Recursive option is not supported for target ‘%s’ argument.", tgtURL))
}
// scope locally
{
@@ -81,7 +81,7 @@ func checkCopySyntax(ctx *cli.Context) {
}
if url.Host != "" {
if url.Path == string(url.Separator) {
fatalIf(errInvalidArgument.Trace(), fmt.Sprintf("Target ‘%s’ does not contain bucket name.", tgtURL))
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Target ‘%s’ does not contain bucket name.", tgtURL))
}
}
}
@@ -95,14 +95,14 @@ func checkCopySyntax(ctx *cli.Context) {
case copyURLsTypeD: // File | Folder... -> Folder.
checkCopySyntaxTypeD(srcURLs, tgtURL)
default:
fatalIf(errInvalidArgument.Trace(), "Invalid arguments to copy command.")
fatalIf(errInvalidArgument().Trace(), "Invalid arguments to copy command.")
}
}
// checkCopySyntaxTypeA verifies if the source and target are valid file arguments.
func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) {
if len(srcURLs) != 1 {
fatalIf(errInvalidArgument.Trace(), "Invalid number of source arguments to copy command.")
fatalIf(errInvalidArgument().Trace(), "Invalid number of source arguments to copy command.")
}
srcURL := srcURLs[0]
_, srcContent, err := url2Stat(srcURL)
@@ -110,34 +110,34 @@ func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) {
fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.")
if srcContent.Type.IsDir() {
fatalIf(errInvalidArgument.Trace(), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL))
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL))
}
if !srcContent.Type.IsRegular() {
fatalIf(errInvalidArgument.Trace(), "Source ‘"+srcURL+"’ is not a file.")
fatalIf(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a file.")
}
}
// checkCopySyntaxTypeB verifies if the source is a valid file and target is a valid dir.
func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) {
if len(srcURLs) != 1 {
fatalIf(errInvalidArgument.Trace(), "Invalid number of source arguments to copy command.")
fatalIf(errInvalidArgument().Trace(), "Invalid number of source arguments to copy command.")
}
srcURL := srcURLs[0]
_, srcContent, err := url2Stat(srcURL)
fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.")
if srcContent.Type.IsDir() {
fatalIf(errInvalidArgument.Trace(), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL))
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL))
}
if !srcContent.Type.IsRegular() {
fatalIf(errInvalidArgument.Trace(), "Source ‘"+srcURL+"’ is not a file.")
fatalIf(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a file.")
}
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?.
if err == nil {
if !tgtContent.Type.IsDir() {
fatalIf(errInvalidArgument.Trace(), "Target ‘"+tgtURL+"’ is not a folder.")
fatalIf(errInvalidArgument().Trace(), "Target ‘"+tgtURL+"’ is not a folder.")
}
}
}
@@ -145,7 +145,7 @@ func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) {
// checkCopySyntaxTypeC verifies if the source is a valid recursive dir and target is a valid dir.
func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) {
if len(srcURLs) != 1 {
fatalIf(errInvalidArgument.Trace(), "Invalid number of source arguments to copy command.")
fatalIf(errInvalidArgument().Trace(), "Invalid number of source arguments to copy command.")
}
srcURL := srcURLs[0]
srcURL = stripRecursiveURL(srcURL)
@@ -153,13 +153,13 @@ func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) {
fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.")
if srcContent.Type.IsRegular() { // Ellipses is supported only for folders.
fatalIf(errInvalidArgument.Trace(), "Source ‘"+srcURL+"’ is not a folder.")
fatalIf(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a folder.")
}
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?.
if err == nil {
if !tgtContent.Type.IsDir() {
fatalIf(errInvalidArgument.Trace(), "Target ‘"+tgtURL+"’ is not a folder.")
fatalIf(errInvalidArgument().Trace(), "Target ‘"+tgtURL+"’ is not a folder.")
}
}
}
@@ -173,17 +173,17 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) {
fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.")
if !srcContent.Type.IsDir() { // Ellipses is supported only for folders.
fatalIf(errInvalidArgument.Trace(), "Source ‘"+srcURL+"’ is not a folder.")
fatalIf(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a folder.")
}
} else { // Regular URL.
_, srcContent, err := url2Stat(srcURL)
fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.")
if srcContent.Type.IsDir() {
fatalIf(errInvalidArgument.Trace(), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL))
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL))
}
if !srcContent.Type.IsRegular() {
fatalIf(errInvalidArgument.Trace(), "Source ‘"+srcURL+"’ is not a file.")
fatalIf(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a file.")
}
}
}
@@ -191,7 +191,7 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) {
// Target exist?.
if err == nil {
if !tgtContent.Type.IsDir() {
fatalIf(errInvalidArgument.Trace(), "Target ‘"+tgtURL+"’ is not a folder.")
fatalIf(errInvalidArgument().Trace(), "Target ‘"+tgtURL+"’ is not a folder.")
}
}
}
@@ -352,7 +352,7 @@ func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan copyURLs
if sourceURLs == nil {
// Source list is empty.
copyURLsCh <- copyURLs{Error: errSourceListEmpty.Trace()}
copyURLsCh <- copyURLs{Error: errSourceListEmpty().Trace()}
return
}
@@ -390,7 +390,7 @@ func prepareCopyURLs(sourceURLs []string, targetURL string) <-chan copyURLs {
copyURLsCh <- cURLs
}
default:
copyURLsCh <- copyURLs{Error: errInvalidArgument.Trace()}
copyURLsCh <- copyURLs{Error: errInvalidArgument().Trace()}
}
}(sourceURLs, targetURL, copyURLsCh)
+1 -1
View File
@@ -77,7 +77,7 @@ func mainDiff(ctx *cli.Context) {
fatalIf(err.Trace(secondArg), "Unable to parse second argument ‘"+secondArg+"’.")
if isURLRecursive(secondURL) {
fatalIf(errInvalidArgument.Trace(), "Second argument ‘"+secondURL+"’ cannot be recursive.")
fatalIf(errInvalidArgument().Trace(), "Second argument ‘"+secondURL+"’ cannot be recursive.")
}
newFirstURL := stripRecursiveURL(firstURL)
for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) {
+1 -1
View File
@@ -64,7 +64,7 @@ func (d DiffMessage) String() string {
case "size":
msg = console.Colorize("DiffMessage", "‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffSize", " - differ in size.")
default:
fatalIf(errDummy.Trace(), "Unhandled difference between ‘"+d.FirstURL+"’ and ‘"+d.SecondURL+"’.")
fatalIf(errDummy().Trace(), "Unhandled difference between ‘"+d.FirstURL+"’ and ‘"+d.SecondURL+"’.")
}
return msg
}
+1 -1
View File
@@ -149,7 +149,7 @@ VERSION:
{{end}}
`
app.CommandNotFound = func(ctx *cli.Context, command string) {
fatalIf(errDummy.Trace(), fmt.Sprintf("Command ‘%s’ not found.", command))
fatalIf(errDummy().Trace(), fmt.Sprintf("Command ‘%s’ not found.", command))
}
return app
}
+6 -6
View File
@@ -83,7 +83,7 @@ func checkMirrorSyntax(ctx *cli.Context) {
/****** Generic rules *******/
// Source cannot be a folder (except when recursive)
if !isURLRecursive(srcURL) {
fatalIf(errInvalidArgument.Trace(), fmt.Sprintf("Source ‘%s’ is not recursive. Use ‘%s...’ as argument to mirror recursively.", srcURL, srcURL))
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Source ‘%s’ is not recursive. Use ‘%s...’ as argument to mirror recursively.", srcURL, srcURL))
}
// Recursive source URL.
newSrcURL := stripRecursiveURL(srcURL)
@@ -91,17 +91,17 @@ func checkMirrorSyntax(ctx *cli.Context) {
fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+newSrcURL+"’.")
if srcContent.Type.IsRegular() { // Ellipses is supported only for folders.
fatalIf(errInvalidArgument.Trace(), "Source ‘"+srcURL+"’ is not a folder.")
fatalIf(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a folder.")
}
if len(tgtURLs) == 0 && tgtURLs == nil {
fatalIf(errInvalidArgument.Trace(), "Invalid number of target arguments to mirror command.")
fatalIf(errInvalidArgument().Trace(), "Invalid number of target arguments to mirror command.")
}
for _, tgtURL := range tgtURLs {
// Recursive URLs are not allowed in target.
if isURLRecursive(tgtURL) {
fatalIf(errDummy.Trace(), fmt.Sprintf("Recursive option is not supported for target ‘%s’ argument.", tgtURL))
fatalIf(errDummy().Trace(), fmt.Sprintf("Recursive option is not supported for target ‘%s’ argument.", tgtURL))
}
url, e := client.Parse(tgtURL)
@@ -109,14 +109,14 @@ func checkMirrorSyntax(ctx *cli.Context) {
if url.Host != "" {
if url.Path == string(url.Separator) {
fatalIf(errInvalidArgument.Trace(), fmt.Sprintf("Target ‘%s’ does not contain bucket name.", tgtURL))
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Target ‘%s’ does not contain bucket name.", tgtURL))
}
}
_, content, err := url2Stat(tgtURL)
fatalIf(err.Trace(tgtURL), "Unable to stat target ‘"+tgtURL+"’.")
if !content.Type.IsDir() {
fatalIf(errInvalidArgument.Trace(), "Target ‘"+tgtURL+"’ is not a folder.")
fatalIf(errInvalidArgument().Trace(), "Target ‘"+tgtURL+"’ is not a folder.")
}
}
}
+2 -2
View File
@@ -93,7 +93,7 @@ func clearSession(sid string) {
}
if !isSession(sid) {
fatalIf(errDummy.Trace(), "Session ‘"+sid+"’ not found.")
fatalIf(errDummy().Trace(), "Session ‘"+sid+"’ not found.")
}
session, err := loadSessionV2(sid)
@@ -144,7 +144,7 @@ func mainSession(ctx *cli.Context) {
sid := strings.TrimSpace(ctx.Args().Tail().First())
if !isSession(sid) {
fatalIf(errDummy.Trace(), "Session ‘"+sid+"’ not found.")
fatalIf(errDummy().Trace(), "Session ‘"+sid+"’ not found.")
}
s, err := loadSessionV2(sid)
+2 -2
View File
@@ -209,7 +209,7 @@ func (s *sessionV2) Delete() *probe.Error {
// loadSession - reads session file if exists and re-initiates internal variables
func loadSessionV2(sid string) (*sessionV2, *probe.Error) {
if !isSessionDirExists() {
return nil, errInvalidArgument.Trace()
return nil, errInvalidArgument().Trace()
}
sessionFile, err := getSessionFile(sid)
if err != nil {
@@ -255,7 +255,7 @@ func isCopiedFactory(lastCopied string) func(string) bool {
copied := true // closure
return func(sourceURL string) bool {
if sourceURL == "" {
fatalIf(errInvalidArgument.Trace(), "Empty source argument passed.")
fatalIf(errInvalidArgument().Trace(), "Empty source argument passed.")
}
if lastCopied == "" {
return false
+9 -3
View File
@@ -23,9 +23,15 @@ import (
)
var (
errDummy = probe.NewError(errors.New("")).Untrace()
errInvalidArgument = probe.NewError(errors.New("Invalid arguments provided, cannot proceed.")).Untrace()
errSourceListEmpty = probe.NewError(errors.New("Source argument list is empty.")).Untrace()
errDummy = func() *probe.Error {
return probe.NewError(errors.New("")).Untrace()
}
errInvalidArgument = func() *probe.Error {
return probe.NewError(errors.New("Invalid arguments provided, cannot proceed.")).Untrace()
}
errSourceListEmpty = func() *probe.Error {
return probe.NewError(errors.New("Source argument list is empty.")).Untrace()
}
)
type eInvalidGlobURL struct {
+1 -1
View File
@@ -91,7 +91,7 @@ func checkGolangRuntimeVersion() {
v1 := newVersion(getNormalizedGolangVersion())
v2 := newVersion(minGolangVersion)
if v1.LessThan(v2) {
errorIf(errDummy.Trace(),
errorIf(errDummy().Trace(),
"Old Golang runtime version ‘"+v1.String()+"’ detected., ‘mc’ requires minimum go1.5 or later.")
}
}