err.go

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