1package entity
 2
 3import (
 4	"fmt"
 5	"strings"
 6)
 7
 8// ErrNotFound is to be returned when an entity, item, element is
 9// not found.
10type ErrNotFound struct {
11	typename string
12}
13
14func NewErrNotFound(typename string) *ErrNotFound {
15	return &ErrNotFound{typename: typename}
16}
17
18func (e ErrNotFound) Error() string {
19	return fmt.Sprintf("%s doesn't exist", e.typename)
20}
21
22func IsErrNotFound(err error) bool {
23	_, ok := err.(*ErrNotFound)
24	return ok
25}
26
27// ErrMultipleMatch is to be returned when more than one entity, item, element
28// is found, where only one was expected.
29type ErrMultipleMatch struct {
30	typename string
31	Matching []Id
32}
33
34func NewErrMultipleMatch(typename string, matching []Id) *ErrMultipleMatch {
35	return &ErrMultipleMatch{typename: typename, Matching: matching}
36}
37
38func (e ErrMultipleMatch) Error() string {
39	matching := make([]string, len(e.Matching))
40
41	for i, match := range e.Matching {
42		matching[i] = match.String()
43	}
44
45	return fmt.Sprintf("Multiple matching %s found:\n%s",
46		e.typename,
47		strings.Join(matching, "\n"))
48}
49
50func IsErrMultipleMatch(err error) bool {
51	_, ok := err.(*ErrMultipleMatch)
52	return ok
53}
54
55// ErrInvalidFormat is to be returned when reading on-disk data with an unexpected
56// format or version.
57type ErrInvalidFormat struct {
58	version  uint
59	expected uint
60}
61
62func NewErrInvalidFormat(version uint, expected uint) *ErrInvalidFormat {
63	return &ErrInvalidFormat{
64		version:  version,
65		expected: expected,
66	}
67}
68
69func NewErrUnknownFormat(expected uint) *ErrInvalidFormat {
70	return &ErrInvalidFormat{
71		version:  0,
72		expected: expected,
73	}
74}
75
76func (e ErrInvalidFormat) Error() string {
77	if e.version == 0 {
78		return fmt.Sprintf("unreadable data, you likely have an outdated repository format, please use https://github.com/git-bug/git-bug-migration to upgrade to format version %v", e.expected)
79	}
80	if e.version < e.expected {
81		return fmt.Sprintf("outdated repository format %v, please use https://github.com/git-bug/git-bug-migration to upgrade to format version %v", e.version, e.expected)
82	}
83	return fmt.Sprintf("your version of git-bug is too old for this repository (format version %v, expected %v), please upgrade to the latest version", e.version, e.expected)
84}