You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+136-97
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,41 @@
1
1
# Error handling proposal
2
2
3
-
**This is not a formal go proposal yet, but the pre-work needed in order to potentially create one.**
3
+
**This should not be considered a formal Go proposal yet, but the pre-work needed in order to create one. When all requirements are in place, this may transition into a proposal.**
4
+
5
+
Contribution guide:
6
+
7
+
- Discuss changes first; either, either on the [Go issue][issue], or by raising an issue at the [GitHub repo][repo].
8
+
- Commits should be atomic; rebase your commits to match this.
9
+
- Examples with third-party dependencies should get it's own go.mod file.
10
+
- Include both working Go 1.24 code (with .go suffix), and a variant using the proposed ? syntax (with .go2 suffix). -
11
+
- Note that only files that are affected by the proposal syntax, needs a .go2 file.
12
+
13
+
Contributions that may be of particular interest for now:
14
+
15
+
- Contributions demonstrating how this change would help improve application code.
@@ -53,6 +80,8 @@ It's not about generics, but the proto-type is using generics for it's implement
53
80
54
81
### Cases
55
82
83
+
Before discussing the proposal, we will demonstrate a few use-cases that could benefit from it. The cases will be relatively simple. Real use-cases may be more complex, and could therefore expect to result in saving more lines.
84
+
56
85
#### Return directly
57
86
58
87
The direct return of an error is a commonly used case for error handling when adding additional context is not necessary.
The collect errors case appear less common in Go for a few reasons. First of all, it's hard to do it as the standard mechanisms for handling it is limited. Secondly, most open source Go code is libraries. However, the use-case for collecting errors is likely common in application code. Especially code that relates to some sort of UI form-validation of user input. Another related example is an API that want to validate client input and communicate all errors at once so that the API client maintainers can more easily do their job.
130
+
The case for collecting errors is likely not common in library code. However, it is likely useful for application code. Possible use-cases include formvalidation or JSON APIs.
- An addition to the Go syntax, using `?()` /`?` to catch errors.
207
230
- Helper functions in the `errors` package.
208
231
209
232
The proposal follows the principal of the now implemented range-over-func proposal in making sure that the solution can be described as valid Go code using the current language syntax. As of the time of writing, this is the syntax of Go 1.24.
210
233
234
+
The `?`/`?()` syntax can be used to _move handling_ of errors from the left of the expression to the right. The default handler (no parenthesis), is to return on error. When parenthesis are provided, errors pass though handlers of format `func(error) error`. If any handler return nill, the code continuous along the happy path. If the final handler returns an error, the function with the `?` syntax returns.
235
+
236
+
It's not yet clear if the `?` syntax should be allowed inside functions that does _not_ return an error. If it's allowed, the suggestion is that the `?` syntax would result in a panic. See options for more details.
237
+
238
+
The standard library changes involve adding handlers for the most common cases for error handling.
239
+
240
+
### Standard library changes
241
+
211
242
The following exposed additions to the standard library `errors` package is suggested:
212
243
213
244
```go
@@ -282,23 +313,24 @@ If the `?` operator receives an error, the error is passed to each handler in or
282
313
283
314
If after all handlers are called, the final return value is an error, then the flow of the current _statement_ is aborted similar to how a panic works. If `?` is used within a function where the final return statement is an error, then this panic is _recovered_ and the error value is populated with that error value and the function returns at once.
284
315
316
+
285
317
### Is this change backward compatible?
286
318
287
319
Yes
288
320
289
-
This work leans on the work done for [%71460][https://github.com/golang/go/discussions/71460], that highlights that the `?` operator is invalid to use in any existing code. Thus it's expected that no existing code will be able to break due to the introduction of the new syntax.
321
+
This work leans on the work done for [%71460][discussion], that highlights that the `?` operator is invalid to use in any existing code. Thus it's expected that no existing code will be able to break due to the introduction of the new syntax.
290
322
291
323
### Orthogonality: How does this change interact or overlap with existing features?
292
324
293
325
_No response_
294
326
295
327
### Would this change make Go easier or harder to learn, and why?
296
328
297
-
Any addition to the Go syntax, including this one, will make it harder to learn go, including this one.
329
+
Any addition to the Go syntax, including this one, will make it harder to learn Go. However, people coming from an exception handling paradigm may find the new syntax less intrusive then the explicit return.
298
330
299
331
### Cost Description
300
332
301
-
The highest cost of this proposal is that there will now be multiple patterns for handling errors.
333
+
The highest cost of this proposal is likely that there will now be multiple patterns for handling errors. There could be discrepancies and disagreement between different projects about which style to use.
302
334
303
335
### Changes to Go ToolChain
304
336
@@ -310,35 +342,32 @@ _No response_
310
342
311
343
### Prototype
312
344
313
-
https://github.com/smyrman/error-handling-proposal includes a working proto-type for the program _flow_ that the new syntax suggests. However there is no transpiler for converting the proposed syntax into valid Go code. In theory one could be written, but I do not have the required time or skills to do so.
314
-
315
-
316
-
## Implementation without a language change
345
+
The proto-type code is found in the [pre-work repo][repo].
317
346
318
-
Following the example of range-over-func, the implementation of the `?` semantics is not magic. A tool can be written to generate go code that rewrites the `?` syntax to valid go 1.24 syntax.
347
+
Following the example of range-over-func, the implementation of the `?` semantics is not magic. A tool could be written to generate go code that rewrites the `?` syntax to valid go 1.24 syntax.
0 commit comments