PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/error.go

https://github.com/szaydel/go-flags
Go | 95 lines | 47 code | 24 blank | 24 comment | 1 complexity | fee9df84c37f46325ee7a3bb0979b74f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package flags
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. // ErrorType represents the type of error.
  7. type ErrorType uint
  8. const (
  9. // ErrUnknown indicates a generic error.
  10. ErrUnknown ErrorType = iota
  11. // ErrExpectedArgument indicates that an argument was expected.
  12. ErrExpectedArgument
  13. // ErrUnknownFlag indicates an unknown flag.
  14. ErrUnknownFlag
  15. // ErrUnknownGroup indicates an unknown group.
  16. ErrUnknownGroup
  17. // ErrMarshal indicates a marshalling error while converting values.
  18. ErrMarshal
  19. // ErrHelp indicates that the built-in help was shown (the error
  20. // contains the help message).
  21. ErrHelp
  22. // ErrNoArgumentForBool indicates that an argument was given for a
  23. // boolean flag (which don't not take any arguments).
  24. ErrNoArgumentForBool
  25. // ErrRequired indicates that a required flag was not provided.
  26. ErrRequired
  27. // ErrShortNameTooLong indicates that a short flag name was specified,
  28. // longer than one character.
  29. ErrShortNameTooLong
  30. // ErrDuplicatedFlag indicates that a short or long flag has been
  31. // defined more than once
  32. ErrDuplicatedFlag
  33. // ErrTag indicates an error while parsing flag tags.
  34. ErrTag
  35. // ErrCommandRequired indicates that a command was required but not
  36. // specified
  37. ErrCommandRequired
  38. // ErrUnknownCommand indicates that an unknown command was specified.
  39. ErrUnknownCommand
  40. )
  41. func (e ErrorType) String() string {
  42. return reflect.TypeOf(e).Name()
  43. }
  44. // Error represents a parser error. The error returned from Parse is of this
  45. // type. The error contains both a Type and Message.
  46. type Error struct {
  47. // The type of error
  48. Type ErrorType
  49. // The error message
  50. Message string
  51. }
  52. // Error returns the error's message
  53. func (e *Error) Error() string {
  54. return e.Message
  55. }
  56. func newError(tp ErrorType, message string) *Error {
  57. return &Error{
  58. Type: tp,
  59. Message: message,
  60. }
  61. }
  62. func newErrorf(tp ErrorType, format string, args ...interface{}) *Error {
  63. return newError(tp, fmt.Sprintf(format, args...))
  64. }
  65. func wrapError(err error) *Error {
  66. ret, ok := err.(*Error)
  67. if !ok {
  68. return newError(ErrUnknown, err.Error())
  69. }
  70. return ret
  71. }