Debian Codegraph @

README.md

raw
1 Package errgo provides some primitives for error creation and handling.
2
3 It provides primitives for wrapping and annotating errors without exposing
4 implementation details unnecessarily.
5
6 Error dependencies as code fragility
7
8 If we import a package, how much of its exposed API are we entitled
9 to use? Most packages do not explicitly document the set of possible
10 public errors that can be returned from their functions, so if we see
11 that a package returns a particular type or value, can we be blamed for
12 writing code that relies on it?
13
14 I cannot be the only one that has run a function that's returning an error
15 I want to handle, looked at the error's type and added a test against
16 that type. That type isn't necessarily in the package that I'm calling,
17 but may be several layers deeper than that. Once that condition is there
18 in the my code, it may be broken if any of the layers below it fails to
19 preserve or generate the errors values my code is now expecting.
20
21 For example, say I'm developing text-editor integration for my favourite
22 cloud app. When using it to parse the app's configuration data, I find
23 that it returns json.SyntaxError when there's a configuration error. I
24 write code that, when it finds json.SyntaxError, jumps the editor cursor
25 to the place where the error was found (by looking at SyntaxError.Offset).
26
27 Some time later, someone comes along and decides that encoding/json is
28 too slow, and replaces it with xxx/fastjson. Since encoding/json isn't
29 in the public API surface area, this doesn't seem like a breaking change,
30 so the change lands, but in actual fact my code is now broken.
31
32 When code bases get sufficiently large, this makes refactoring code
33 considerably more error-prone, because it is not always
34 clear whether it's OK to change some code or not. The code
35 is fragile.
36
37 Wrapping breaks error dependencies
38
39 A common idiom is to return an error "annotated" with some extra text
40 to give it more context. If we do that, then we protect ourselves
41 from the subtle breakage illustrated above, because callers *cannot*
42 depend on the underlying error any more. If I want to add my feature,
43 I need to notify upstream and tell them that I'd like to be able to
44 find where the config file syntax error is, and they'll need to land a
45 change that makes it explicitly available in the API. This is more work,
46 but it means that the error type is now an explicit part of the API
47 and hence much less likely to be changed in a backwardly incompatible
48 way.
49
50 The problem with fmt.Errorf is that it hides even error causes that
51 we *want* to expose. It's common to have a few well-known error
52 return values
53
54 I find a package
55 that can talk to the app, and I find that when
56
57
58 My code uses
59 the tool's config p
60
61
62 calling a package that uses (internally)
63 encoding/json to parse its configuration format. I'm building
64 an
65
66
67
68 Even if we're only importing
69 a package for internal use, if we're returning its errors,
70 we're exposing ourselves to that kind of
71
72 example, changing to use
73 a different package that's only used internally
74 might end up breaking some other code that happens
75 to be
76 if any of those
77 layers changes to use a different lower layer,
78 or
79
80 Fragility 1: "That code doesn't return that kind of error any more!"
81
82 Fragility 2: "I thought that error meant *this* not *that* as well!"
83
84
85 Some code makes a conditional test on an error. It may compare the
86 error for equality with a value defined in another package (io.EOF), or
87 call a function (os.IsNotExist) or do a dynamic type comparison. There
88 is a dependency made between the code doing the comparison and the code
89 that defines the value, function or type. This dependency is not visible
90 directly in any call graph but bugs and API incompatibilities can easily
91 arise when dependencies change.
92
93 Since errors can propagate back through many layers of stack and layers
94 of abstraction (and are commonly returned exactly as received), a caller
95 might be testing an error returned from a package that an intermediate
96 package considers an implementation details.
97
98 Even though error values and types are not generally well documented
99 (or perhaps *because* they're not), these dependencies arise commonly
100 because, on finding some error that we want to handle, we will run the
101 program or look at the code, then write a test against the type we see.
102
103 This
104
105
106 and try to find something about the error that can be used to
107 distinguish the problem we're seeing, and often that will involve
108 a test against an error␣ value
109
110 I believe that most programmers
111 will try to avoid string comparisons (known to be fragile), but testing
112 against a known type seems more robust.
113
114
115 diagnoses
116 the problem what we're seeing.
117
118 some kind of database
119 package, and as part of that implementation, it uses an
120 encoding package.
121
122 initially uses a file based store.
123
124 It is currently very common to return exactly the error that was
125
126
127 when encountering an error
128
129
130 When code tests an error against a particular type or value
131 to decide what to do
132
133
134 The is informed by the view that
135
136 hiding is important
137
138
139
140 It aims to decrease implicit inter-package dependency