{"push_id":994296462,"size":1,"distinct_size":1,"ref":"refs/heads/remove_static_error_declarations","head":"23ac1689ee65fa46c1f5395bd143faa6dbbf11ee","before":"6a9e9496efe82c5ca7d24385b4f753dd72575bf9","commits":[{"sha":"23ac1689ee65fa46c1f5395bd143faa6dbbf11ee","author":{"email":"b9b0c8e7f90ecf337f6797c4d2e0f1dd0dbc0324@gmail.com","name":"David Calavera"},"message":"Remove static errors from errors package.\n\nMoving all strings to the errors package wasn't a good idea after all.\n\nOur custom implementation of Go errors predates everything that's nice\nand good about working with errors in Go. Take as an example what we\nhave to do to get an error message:\n\n```go\nfunc GetErrorMessage(err error) string {\n\tswitch err.(type) {\n\tcase errcode.Error:\n\t\te, _ := err.(errcode.Error)\n\t\treturn e.Message\n\n\tcase errcode.ErrorCode:\n\t\tec, _ := err.(errcode.ErrorCode)\n\t\treturn ec.Message()\n\n\tdefault:\n\t\treturn err.Error()\n\t}\n}\n```\n\nThis goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.\n\nOur custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.\n\nMoreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:\n\n```go\n\tswitch err.(type) {\n\tcase errcode.ErrorCode:\n\t\tdaError, _ := err.(errcode.ErrorCode)\n\t\tstatusCode = daError.Descriptor().HTTPStatusCode\n\t\terrMsg = daError.Message()\n\n\tcase errcode.Error:\n\t\t// For reference, if you're looking for a particular error\n\t\t// then you can do something like :\n\t\t//   import ( derr \"github.com/docker/docker/errors\" )\n\t\t//   if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }\n\n\t\tdaError, _ := err.(errcode.Error)\n\t\tstatusCode = daError.ErrorCode().Descriptor().HTTPStatusCode\n\t\terrMsg = daError.Message\n\n\tdefault:\n\t\t// This part of will be removed once we've\n\t\t// converted everything over to use the errcode package\n\n\t\t// FIXME: this is brittle and should not be necessary.\n\t\t// If we need to differentiate between different possible error types,\n\t\t// we should create appropriate error types with clearly defined meaning\n\t\terrStr := strings.ToLower(err.Error())\n\t\tfor keyword, status := range map[string]int{\n\t\t\t\"not found\":             http.StatusNotFound,\n\t\t\t\"no such\":               http.StatusNotFound,\n\t\t\t\"bad parameter\":         http.StatusBadRequest,\n\t\t\t\"conflict\":              http.StatusConflict,\n\t\t\t\"impossible\":            http.StatusNotAcceptable,\n\t\t\t\"wrong login/password\":  http.StatusUnauthorized,\n\t\t\t\"hasn't been activated\": http.StatusForbidden,\n\t\t} {\n\t\t\tif strings.Contains(errStr, keyword) {\n\t\t\t\tstatusCode = status\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t}\n```\n\nYou can notice two things in that code:\n\n1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.\n2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.\n\nThis change removes all our status errors from the errors package and puts them back in their specific contexts.\nIT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.\nIt uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:\n\n```go\ntype errorWithStatus interface {\n\tHTTPErrorStatusCode() int\n}\n```\n\nThis interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.\n\nI included helper functions to generate errors that use custom status code in `errors/errors.go`.\n\nBy doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.\n\nPlease, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors\n\nSigned-off-by: David Calavera <david.calavera@gmail.com>","distinct":true,"url":"https://api.github.com/repos/calavera/docker/commits/23ac1689ee65fa46c1f5395bd143faa6dbbf11ee"}]}