/vendor/github.com/kataras/go-errors/errors.go

https://github.com/kataras/go-template · Go · 102 lines · 64 code · 15 blank · 23 comment · 4 complexity · a2f9172d5bc0907374e601ac8fa57979 MD5 · raw file

  1. // Package errors helps you to write and design your own pre-defined errors, useful when you have a known list of errors
  2. package errors
  3. import (
  4. "fmt"
  5. "runtime"
  6. )
  7. const (
  8. // Version current version number
  9. Version = "0.0.3"
  10. )
  11. var (
  12. // Prefix the error prefix, applies to each error's message
  13. // defaults to Error:_
  14. Prefix = "Error: "
  15. // NewLine adds a new line to the end of each error's message
  16. // defaults to true
  17. NewLine = true
  18. )
  19. // Error holds the error message, this message never really changes
  20. type Error struct {
  21. message string
  22. appended bool
  23. }
  24. // New creates and returns an Error with a pre-defined user output message
  25. // all methods below that doesn't accept a pointer receiver because actualy they are not changing the original message
  26. func New(errMsg string) *Error {
  27. if NewLine {
  28. errMsg += "\n"
  29. }
  30. return &Error{message: Prefix + errMsg}
  31. }
  32. // String returns the error message
  33. func (e Error) String() string {
  34. return e.message
  35. }
  36. // Error returns the message of the actual error
  37. // implements the error
  38. func (e Error) Error() string {
  39. return e.String()
  40. }
  41. // Format returns a formatted new error based on the arguments
  42. // it does NOT change the original error's message
  43. func (e Error) Format(a ...interface{}) Error {
  44. e.message = fmt.Sprintf(e.message, a...)
  45. return e
  46. }
  47. // Append adds a message to the predefined error message and returns a new error
  48. // it does NOT change the original error's message
  49. func (e Error) Append(format string, a ...interface{}) Error {
  50. // eCp := *e
  51. if NewLine {
  52. format += "\n"
  53. }
  54. e.message += fmt.Sprintf(format, a...)
  55. e.appended = true
  56. return e
  57. }
  58. // AppendErr adds an error's message to the predefined error message and returns a new error
  59. // it does NOT change the original error's message
  60. func (e Error) AppendErr(err error) Error {
  61. return e.Append(err.Error())
  62. }
  63. // IsAppended returns true if the Error instance is created using original's Error.Append/AppendErr func
  64. func (e Error) IsAppended() bool {
  65. return e.appended
  66. }
  67. // With does the same thing as Format but it receives an error type which if it's nil it returns a nil error
  68. func (e Error) With(err error) error {
  69. if err == nil {
  70. return nil
  71. }
  72. return e.Format(err.Error())
  73. }
  74. // Panic output the message and after panics
  75. func (e Error) Panic() {
  76. _, fn, line, _ := runtime.Caller(1)
  77. errMsg := e.message
  78. errMsg = "\nCaller was: " + fmt.Sprintf("%s:%d", fn, line)
  79. panic(errMsg)
  80. }
  81. // Panicf output the formatted message and after panics
  82. func (e Error) Panicf(args ...interface{}) {
  83. _, fn, line, _ := runtime.Caller(1)
  84. errMsg := e.Format(args...).Error()
  85. errMsg = "\nCaller was: " + fmt.Sprintf("%s:%d", fn, line)
  86. panic(errMsg)
  87. }