-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: stdlib cleanup and simplification
- Loading branch information
Showing
3 changed files
with
92 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package stdlib | ||
|
||
import "fmt" | ||
|
||
type ArgumentError struct { | ||
name string | ||
wrapped error | ||
} | ||
|
||
func NewArgumentError(name string, wrapped error) ArgumentError { | ||
return ArgumentError{name: name, wrapped: wrapped} | ||
} | ||
|
||
func (e ArgumentError) Error() string { | ||
return fmt.Sprintf("%s(): argument error: %s", e.name, e.wrapped) | ||
} | ||
|
||
func (e ArgumentError) Unwrap() error { | ||
return e.wrapped | ||
} | ||
|
||
type InvalidNumberOfArgumentsError struct { | ||
Expected int | ||
Actual int | ||
Message string | ||
} | ||
|
||
func NewInvalidNumberOfArgumentsError(name, message string, expected, actual int) error { | ||
return NewArgumentError(name, InvalidNumberOfArgumentsError{Message: message, Actual: actual, Expected: expected}) | ||
} | ||
|
||
func (e InvalidNumberOfArgumentsError) Error() string { | ||
if e.Message == "" { | ||
return fmt.Sprintf("accepts exactly %d argument, %d provided", e.Expected, e.Actual) | ||
} | ||
|
||
return fmt.Sprintf(e.Message, e.Expected, e.Actual) | ||
} | ||
|
||
type InvalidArgumentTypeError struct { | ||
Message string | ||
} | ||
|
||
func (e InvalidArgumentTypeError) Error() string { | ||
return e.Message | ||
} | ||
|
||
func NewInvalidArgumentTypeError(name, message string) error { | ||
return NewArgumentError(name, InvalidArgumentTypeError{Message: message}) | ||
} |
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