-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
338 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package libcmd | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// parsing: generic parser error | ||
type parserError struct { | ||
arg string | ||
err error | ||
} | ||
|
||
func (e parserError) Error() string { | ||
if e.arg != "" { | ||
return fmt.Sprintf("error parsing argument '%s': %v", e.arg, e.err) | ||
} | ||
|
||
return fmt.Sprintf("parsing error: %v", e.err) | ||
} | ||
|
||
// parsing: unknown argument | ||
type unknownArgErr struct { | ||
arg string | ||
} | ||
|
||
func (e unknownArgErr) Error() string { | ||
return fmt.Sprintf("unknown argument: %s", e.arg) | ||
} | ||
|
||
// parsing: no value for argument | ||
type noValueErr struct { | ||
arg string | ||
} | ||
|
||
func (e noValueErr) Error() string { | ||
return fmt.Sprintf("no value for argument: %s", e.arg) | ||
} | ||
|
||
// parsing: conversion error | ||
type conversionErr struct { | ||
value interface{} | ||
typeName string | ||
} | ||
|
||
func (e conversionErr) Error() string { | ||
return fmt.Sprintf("'%v' is not a valid %s value", e.value, e.typeName) | ||
} | ||
|
||
// parsing: unsupported type | ||
type unsupportedErr struct { | ||
value interface{} | ||
typeName string | ||
} | ||
|
||
func (e unsupportedErr) Error() string { | ||
return fmt.Sprintf("unsupported type '%s' for value '%s'", e.typeName, e.value) | ||
} | ||
|
||
// parsing: wrong number of operands | ||
type operandRequiredErr struct { | ||
required int | ||
got int | ||
exact bool | ||
} | ||
|
||
func (e operandRequiredErr) Error() string { | ||
if e.exact { | ||
return fmt.Sprintf("wrong number of operands, exactly %d required (got %d)", e.required, e.got) | ||
} | ||
return fmt.Sprintf("wrong number of operands, at least %d required (got %d)", e.required, e.got) | ||
} | ||
|
||
// IsParserErr returns true is the error is an error | ||
// generated by the parsing process itself. | ||
func IsParserErr(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
|
||
switch err.(type) { | ||
case parserError: | ||
return true | ||
|
||
case unknownArgErr: | ||
return true | ||
|
||
case noValueErr: | ||
return true | ||
|
||
case conversionErr: | ||
return true | ||
|
||
case operandRequiredErr: | ||
return true | ||
|
||
default: | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package libcmd_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ibraimgm/libcmd" | ||
) | ||
|
||
func TestIsParserErr(t *testing.T) { | ||
tests := []struct { | ||
err error | ||
expected bool | ||
}{ | ||
{err: nil, expected: false}, | ||
{err: errors.New("my error"), expected: false}, | ||
} | ||
|
||
for i, test := range tests { | ||
actual := libcmd.IsParserErr(test.err) | ||
|
||
if actual != test.expected { | ||
t.Errorf("Case %d, expected '%v', but comparison returned '%v'", i, test.expected, actual) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.