errorx.go 780 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package errorx
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/pkg/env"
  6. )
  7. var errorFormat = `goctlwt error: %+v
  8. goctlwt env:
  9. %s
  10. %s`
  11. // GoctlError represents a goctlwt error.
  12. type GoctlError struct {
  13. message []string
  14. err error
  15. }
  16. func (e *GoctlError) Error() string {
  17. detail := wrapMessage(e.message...)
  18. return fmt.Sprintf(errorFormat, e.err, env.Print(), detail)
  19. }
  20. // Wrap wraps an error with goctlwt version and message.
  21. func Wrap(err error, message ...string) error {
  22. e, ok := err.(*GoctlError)
  23. if ok {
  24. return e
  25. }
  26. return &GoctlError{
  27. message: message,
  28. err: err,
  29. }
  30. }
  31. func wrapMessage(message ...string) string {
  32. if len(message) == 0 {
  33. return ""
  34. }
  35. return fmt.Sprintf(`message: %s`, strings.Join(message, "\n"))
  36. }