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