3 ms·
This idea can also be explored in the Go programming language. Go has an error type, not exceptions, but error checking famously can be rather verbose. Two cas
by cratermoon 4y ago
This idea can also be explored in the Go programming language. Go has an error type, not exceptions, but error checking famously can be rather verbose.
Two cases to consider come to mind. First, the common pattern
result, err := SomeFunc()
if err != nil {
return err
}
Here the code is just passing along the error to the caller, unchanged.
Second, signaling errors ab initio
result := // some calculation or behavior
if result != expected
return fmt.Errorf("An error happened. Expected %v, go %v", expected, result)
In the first case, the discussions around whether or not to throw custom exceptions applies analogously: should you wrap the error or not?
The second case, I argue, is always wrong. The error is "stringly typed", and can be examined and read by a person, but that's it. The correct way is to define an error type meaningful for the context. Errors in Go are type implementing the error interface
type error interface {
Error() string
}
therefore, an error should be a type relevant or the context. A useful starting point looks something like
type DomainError struct {
Code DomainErrorCode
Message string
Details []DomainType
}
func (d DomainError) Error() string {
return Message
}
then code can look like
// some work
if result != expected {
return DomainError {
Status: AnErrorCode
Message: fmt.Sprintf("Error %v. Expected %v, go %v", AnErrorCode expected, result)
Details: []DomainType{ expected, result }
}
}
And the caller gets back a type providing useful information.
- kitd 4y agoIME, returning a "stringly-typed" error is only wrong if you don't provide enough context in the returned error message. You can describe what you were doing as an error message and pass it back up the stack, each layer adding their own context, ie what they were trying to do to what. At the top you (should) get a pretty complete picture of what went wrong and why. I have found this reasonable and proportionate for most scenarios.
- cratermoon 4y agoThat's all well and good if all you want to do it propagate the error up the call stack until some code prints it, but that's not really handling the error, any more than if err != nil { return error } is. If the calling code needs to behave differently depending on the details of the error, the worst way to do that would be examine the message string. A robust system is going to respond very differently if, for example, the error is one that indicates retrying is worthwhile vs an error indicating that no amount of retrying will ever succeed.
- tsimionescu 4y agoThe whole DomainError thing only makes sense if you expect someone to handle this error (and by handle, I mean doing something other than logging and aborting some operation), or if you are writing a very generic library. Otherwise, it is wasted time and extra complexity that makes the code harder to read.
- maria2 4y agoIt depends on how much context is needed. Imagine you have a Go library that parses JavaScript. Parsing can return an error, and it's very helpful to know information like the line and column number. So you might have an error like this: type ParseError struct { Line int Col int Message string } The user of your library isn't expected to handle this error explicitly, but when they print the output, they can see exactly where the issue was.
- tsimionescu 4y agoDo you expect any piece of code to touch err.Line and err.Col? I definitely don't - I would expect the line and col to be included in the message of a basic string error.
- cratermoon 4y agoYes, that's true, just as it is with exceptions in the original article. However, if you are writing the code and it's callers, if you don't want to handle the error, why even have it? Just log the results there and move on.
- gwd 4y ago> However, if you are writing the code and it's callers, if you don't want to handle the error, why even have it? Just log the results there and move on. Well at very least you need the caller to know that the operation didn't succeed. And if the failure is deep in the call stack, you may not have enough information to log a meaningful error. Returning errors and wrapping them with a description as you pass them up the stack means you can have a single meaningful log message at the "top level" (wherever the buck stops).