/Godeps/_workspace/src/github.com/iris-contrib/errors/error.go

https://github.com/beyondblog/k8s-web-terminal · Go · 72 lines · 48 code · 13 blank · 11 comment · 6 complexity · 99385c44dc173ae863a99a5146a56ca2 MD5 · raw file

  1. package errors
  2. import (
  3. "fmt"
  4. "runtime"
  5. "github.com/beyondblog/k8s-web-terminal/Godeps/_workspace/src/github.com/kataras/iris/logger"
  6. )
  7. // Error holds the error
  8. type Error struct {
  9. message string
  10. }
  11. // Error returns the message of the actual error
  12. func (e *Error) Error() string {
  13. return e.message
  14. }
  15. // Format returns a formatted new error based on the arguments
  16. func (e *Error) Format(args ...interface{}) error {
  17. return fmt.Errorf(e.message, args...)
  18. }
  19. // With does the same thing as Format but it receives an error type which if it's nil it returns a nil error
  20. func (e *Error) With(err error) error {
  21. if err == nil {
  22. return nil
  23. }
  24. return e.Format(err.Error())
  25. }
  26. // Return returns the actual error as it is
  27. func (e *Error) Return() error {
  28. return e.Format()
  29. }
  30. // Panic output the message and after panics
  31. func (e *Error) Panic() {
  32. if e == nil {
  33. return
  34. }
  35. _, fn, line, _ := runtime.Caller(1)
  36. errMsg := e.message
  37. errMsg = "\nCaller was: " + fmt.Sprintf("%s:%d", fn, line)
  38. panic(errMsg)
  39. }
  40. // Panicf output the formatted message and after panics
  41. func (e *Error) Panicf(args ...interface{}) {
  42. if e == nil {
  43. return
  44. }
  45. _, fn, line, _ := runtime.Caller(1)
  46. errMsg := e.Format(args...).Error()
  47. errMsg = "\nCaller was: " + fmt.Sprintf("%s:%d", fn, line)
  48. panic(errMsg)
  49. }
  50. //
  51. // New creates and returns an Error with a message
  52. func New(errMsg string) *Error {
  53. // return &Error{fmt.Errorf("\n" + logger.Prefix + "Error: " + errMsg)}
  54. return &Error{message: "\n" + logger.Prefix + " Error: " + errMsg}
  55. }
  56. // Printf prints to the logger a specific error with optionally arguments
  57. func Printf(logger *logger.Logger, err error, args ...interface{}) {
  58. logger.Printf(err.Error(), args...)
  59. }