Skip to content

Commit 14ec3c0

Browse files
committed
gopls/doc/contributing.md: document error handling strategies
Fixes golang/go#60969 Change-Id: Ia19cf735523b8d0022ae00d79db6a6f95d064f6e Reviewed-on: https://go-review.googlesource.com/c/tools/+/506938 Run-TryBot: Alan Donovan <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent c495364 commit 14ec3c0

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

gopls/doc/contributing.md

+46
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,52 @@ The best way to contact the gopls team directly is via the
4646
gophers slack. Please feel free to ask any questions about your contribution or
4747
about contributing in general.
4848

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+
4995
## Testing
5096

5197
To run tests for just `gopls/`, run,

0 commit comments

Comments
 (0)