1
0

error.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package migres
  2. import (
  3. "fmt"
  4. "github.com/annybs/go-version"
  5. )
  6. // Migration error.
  7. var (
  8. ErrMigrationFailed = Error{Message: "migration failed at version %q: %s"}
  9. )
  10. // Error reflects an error that occurred during a migration.
  11. type Error struct {
  12. Message string // Error message template.
  13. PreviousError error // Original error encountered during the migration.
  14. Version *version.Version // Version at which the migration error occured.
  15. LastVersion *version.Version // Version
  16. }
  17. // Error retrieves the message of a migration error.
  18. // This does not necessarily include all information about the error, such as the last
  19. func (e *Error) Error() string {
  20. return fmt.Sprintf(e.Message, e.Version, e.PreviousError)
  21. }
  22. // Is determines whether the Error is an instance of the target.
  23. // https://pkg.go.dev/errors#Is
  24. //
  25. // This implementation does not compare versions.
  26. func (e *Error) Is(target error) bool {
  27. if t, ok := target.(*Error); ok {
  28. return t.Message == e.Message
  29. }
  30. return false
  31. }
  32. func failMigration(err error, v, last *version.Version) *Error {
  33. return &Error{
  34. Message: ErrMigrationFailed.Message,
  35. PreviousError: err,
  36. Version: v,
  37. LastVersion: last,
  38. }
  39. }