@@ -46,6 +46,52 @@ The best way to contact the gopls team directly is via the
46
46
gophers slack. Please feel free to ask any questions about your contribution or
47
47
about contributing in general.
48
48
49
+
50
+ ## Error handling
51
+
52
+ It is important for the user experience that, whenever practical,
53
+ minor logic errors in a particular feature don't cause the server to
54
+ crash.
55
+
56
+ The representation of a Go program is complex. The import graph of
57
+ package metadata, the syntax trees of parsed files, and their
58
+ associated type information together form a huge API surface area.
59
+ Even when the input is valid, there are many edge cases to consider,
60
+ and this grows by an order of magnitude when you consider missing
61
+ imports, parse errors, and type errors.
62
+
63
+ What should you do when your logic must handle an error that you
64
+ believe "can't happen"?
65
+
66
+ - If it's possible to return an error, then use the ` bug.Errorf `
67
+ function to return an error to the user, but also record the bug in
68
+ gopls' cache so that it is less likely to be ignored.
69
+
70
+ - If it's safe to proceed, you can call ` bug.Reportf ` to record the
71
+ error and continue as normal.
72
+
73
+ - If there's no way to proceed, call ` bug.Fatalf ` to record the error
74
+ and then stop the program with ` log.Fatalf ` . You can also use
75
+ ` bug.Panicf ` if there's a chance that a recover handler might save
76
+ the situation.
77
+
78
+ - Only if you can prove locally that an error is impossible should you
79
+ call ` log.Fatal ` . If the error may happen for some input, however
80
+ unlikely, then you should use one of the approaches above. Also, if
81
+ the proof of safety depends on invariants broadly distributed across
82
+ the code base, then you should instead use ` bug.Panicf ` .
83
+
84
+ Note also that panicking is preferable to ` log.Fatal ` because it
85
+ allows VS Code's crash reporting to recognize and capture the stack.
86
+
87
+ Bugs reported through ` bug.Errorf ` and friends are retrieved using the
88
+ ` gopls bug ` command, which opens a GitHub Issue template and populates
89
+ it with a summary of each bug and its frequency.
90
+ The text of the bug is rather fastidiously printed to stdout to avoid
91
+ sharing user names and error message strings (which could contain
92
+ project identifiers) with GitHub.
93
+ Users are invited to share it if they are willing.
94
+
49
95
## Testing
50
96
51
97
To run tests for just ` gopls/ ` , run,
0 commit comments