2015-11-12 14:55:17 -05:00
|
|
|
package container
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
import (
|
2013-03-12 08:36:37 -04:00
|
|
|
"fmt"
|
2013-04-09 12:09:54 -04:00
|
|
|
"sync"
|
2013-01-28 17:30:05 -05:00
|
|
|
"time"
|
2014-05-12 19:40:19 -04:00
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-07-15 14:21:19 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-12-16 12:26:49 -05:00
|
|
|
"github.com/docker/go-units"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// State holds the current container state, and has methods to get and
|
|
|
|
// set the state. Container has an embed, which allows all of the
|
|
|
|
// functions defined against State to run against Container.
|
2013-01-18 19:13:39 -05:00
|
|
|
type State struct {
|
2014-08-28 07:39:27 -04:00
|
|
|
sync.Mutex
|
2015-07-30 17:01:53 -04:00
|
|
|
// FIXME: Why do we have both paused and running if a
|
|
|
|
// container cannot be paused and running at the same time?
|
2015-03-12 15:26:17 -04:00
|
|
|
Running bool
|
|
|
|
Paused bool
|
|
|
|
Restarting bool
|
|
|
|
OOMKilled bool
|
2015-11-12 14:55:17 -05:00
|
|
|
RemovalInProgress bool // Not need for this to be persistent on disk.
|
2015-03-12 15:26:17 -04:00
|
|
|
Dead bool
|
|
|
|
Pid int
|
2016-08-30 12:44:06 -04:00
|
|
|
ExitCodeValue int `json:"ExitCode"`
|
|
|
|
ErrorMsg string `json:"Error"` // contains last known error when starting the container
|
2015-03-12 15:26:17 -04:00
|
|
|
StartedAt time.Time
|
|
|
|
FinishedAt time.Time
|
|
|
|
waitChan chan struct{}
|
2016-04-18 05:48:13 -04:00
|
|
|
Health *Health
|
2014-06-06 07:28:12 -04:00
|
|
|
}
|
|
|
|
|
2016-08-30 12:44:06 -04:00
|
|
|
// StateStatus is used to return an error type implementing both
|
|
|
|
// exec.ExitCode and error.
|
|
|
|
// This type is needed as State include a sync.Mutex field which make
|
|
|
|
// copying it unsafe.
|
|
|
|
type StateStatus struct {
|
|
|
|
exitCode int
|
|
|
|
error string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStateStatus(ec int, err string) *StateStatus {
|
|
|
|
return &StateStatus{
|
|
|
|
exitCode: ec,
|
|
|
|
error: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExitCode returns current exitcode for the state.
|
|
|
|
func (ss *StateStatus) ExitCode() int {
|
|
|
|
return ss.exitCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns current error for the state.
|
|
|
|
func (ss *StateStatus) Error() string {
|
|
|
|
return ss.error
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// NewState creates a default state object with a fresh channel for state changes.
|
2014-06-06 07:28:12 -04:00
|
|
|
func NewState() *State {
|
|
|
|
return &State{
|
|
|
|
waitChan: make(chan struct{}),
|
|
|
|
}
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2013-01-27 18:42:42 -05:00
|
|
|
// String returns a human-readable description of the state
|
|
|
|
func (s *State) String() string {
|
|
|
|
if s.Running {
|
2014-05-21 17:06:18 -04:00
|
|
|
if s.Paused {
|
|
|
|
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
|
|
}
|
2014-08-11 14:35:18 -04:00
|
|
|
if s.Restarting {
|
2016-08-30 12:44:06 -04:00
|
|
|
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2014-08-11 14:35:18 -04:00
|
|
|
}
|
2014-08-11 14:07:37 -04:00
|
|
|
|
2016-04-18 05:48:13 -04:00
|
|
|
if h := s.Health; h != nil {
|
|
|
|
return fmt.Sprintf("Up %s (%s)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)), h.String())
|
|
|
|
}
|
2016-07-15 14:21:19 -04:00
|
|
|
|
2014-05-12 19:40:19 -04:00
|
|
|
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
2013-01-27 18:42:42 -05:00
|
|
|
}
|
2014-08-11 14:07:37 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if s.RemovalInProgress {
|
2015-03-12 15:26:17 -04:00
|
|
|
return "Removal In Progress"
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Dead {
|
|
|
|
return "Dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:51:58 -04:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "Created"
|
|
|
|
}
|
|
|
|
|
2014-03-18 23:05:54 -04:00
|
|
|
if s.FinishedAt.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
2014-08-11 14:07:37 -04:00
|
|
|
|
2016-08-30 12:44:06 -04:00
|
|
|
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2013-01-27 18:42:42 -05:00
|
|
|
}
|
|
|
|
|
2016-07-15 14:21:19 -04:00
|
|
|
// HealthString returns a single string to describe health status.
|
|
|
|
func (s *State) HealthString() string {
|
|
|
|
if s.Health == nil {
|
|
|
|
return types.NoHealthcheck
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Health.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValidHealthString checks if the provided string is a valid container health status or not.
|
|
|
|
func IsValidHealthString(s string) bool {
|
|
|
|
return s == types.Starting ||
|
|
|
|
s == types.Healthy ||
|
|
|
|
s == types.Unhealthy ||
|
|
|
|
s == types.NoHealthcheck
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:25:50 -04:00
|
|
|
// StateString returns a single string to describe state
|
|
|
|
func (s *State) StateString() string {
|
|
|
|
if s.Running {
|
|
|
|
if s.Paused {
|
|
|
|
return "paused"
|
|
|
|
}
|
|
|
|
if s.Restarting {
|
|
|
|
return "restarting"
|
|
|
|
}
|
|
|
|
return "running"
|
|
|
|
}
|
2015-03-12 15:26:17 -04:00
|
|
|
|
2016-08-15 07:33:56 -04:00
|
|
|
if s.RemovalInProgress {
|
|
|
|
return "removing"
|
|
|
|
}
|
|
|
|
|
2015-03-12 15:26:17 -04:00
|
|
|
if s.Dead {
|
|
|
|
return "dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:51:58 -04:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "created"
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:25:50 -04:00
|
|
|
return "exited"
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// IsValidStateString checks if the provided string is a valid container state or not.
|
|
|
|
func IsValidStateString(s string) bool {
|
2015-06-30 17:40:27 -04:00
|
|
|
if s != "paused" &&
|
|
|
|
s != "restarting" &&
|
2016-08-15 07:33:56 -04:00
|
|
|
s != "removing" &&
|
2015-06-30 17:40:27 -04:00
|
|
|
s != "running" &&
|
|
|
|
s != "dead" &&
|
|
|
|
s != "created" &&
|
|
|
|
s != "exited" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-06 07:28:12 -04:00
|
|
|
func wait(waitChan <-chan struct{}, timeout time.Duration) error {
|
|
|
|
if timeout < 0 {
|
|
|
|
<-waitChan
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This 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.
Our 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.
Moreover, 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:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By 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.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return fmt.Errorf("Timed out: %v", timeout)
|
2014-06-06 07:28:12 -04:00
|
|
|
case <-waitChan:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitStop waits until state is stopped. If state already stopped it returns
|
2014-09-05 04:22:32 -04:00
|
|
|
// immediately. If you want wait forever you must supply negative timeout.
|
2016-09-20 07:09:18 -04:00
|
|
|
// Returns exit code, that was passed to SetStopped
|
2014-06-06 07:28:12 -04:00
|
|
|
func (s *State) WaitStop(timeout time.Duration) (int, error) {
|
2016-11-16 22:08:37 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
if timeout >= 0 {
|
|
|
|
var cancel func()
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
if err := s.WaitWithContext(ctx); err != nil {
|
|
|
|
if status, ok := err.(*StateStatus); ok {
|
|
|
|
return status.ExitCode(), nil
|
|
|
|
}
|
2014-06-06 07:28:12 -04:00
|
|
|
return -1, err
|
|
|
|
}
|
2016-11-16 22:08:37 -05:00
|
|
|
return 0, nil
|
2014-06-06 07:28:12 -04:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
// WaitWithContext waits for the container to stop. Optional context can be
|
|
|
|
// passed for canceling the request.
|
2016-06-14 14:11:43 -04:00
|
|
|
func (s *State) WaitWithContext(ctx context.Context) error {
|
|
|
|
s.Lock()
|
|
|
|
if !s.Running {
|
2016-08-30 12:44:06 -04:00
|
|
|
state := newStateStatus(s.ExitCode(), s.Error())
|
2016-06-14 14:11:43 -04:00
|
|
|
defer s.Unlock()
|
2016-08-30 12:44:06 -04:00
|
|
|
if state.ExitCode() == 0 {
|
2016-06-14 14:11:43 -04:00
|
|
|
return nil
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
2016-08-30 12:44:06 -04:00
|
|
|
return state
|
2016-06-14 14:11:43 -04:00
|
|
|
}
|
|
|
|
waitChan := s.waitChan
|
|
|
|
s.Unlock()
|
|
|
|
select {
|
|
|
|
case <-waitChan:
|
|
|
|
s.Lock()
|
2016-08-30 12:44:06 -04:00
|
|
|
state := newStateStatus(s.ExitCode(), s.Error())
|
2016-06-13 22:52:49 -04:00
|
|
|
s.Unlock()
|
2016-08-30 12:44:06 -04:00
|
|
|
if state.ExitCode() == 0 {
|
2016-06-14 14:11:43 -04:00
|
|
|
return nil
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
2016-08-30 12:44:06 -04:00
|
|
|
return state
|
2016-06-14 14:11:43 -04:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// IsRunning returns whether the running flag is set. Used by Container to check whether a container is running.
|
2013-11-21 15:21:03 -05:00
|
|
|
func (s *State) IsRunning() bool {
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Lock()
|
2014-06-06 07:28:12 -04:00
|
|
|
res := s.Running
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
return res
|
|
|
|
}
|
2013-11-21 15:21:03 -05:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// GetPID holds the process id of a container.
|
2015-11-02 18:25:26 -05:00
|
|
|
func (s *State) GetPID() int {
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Lock()
|
2014-06-06 07:28:12 -04:00
|
|
|
res := s.Pid
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
return res
|
2013-11-21 15:21:03 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 14:11:43 -04:00
|
|
|
// ExitCode returns current exitcode for the state. Take lock before if state
|
|
|
|
// may be shared.
|
|
|
|
func (s *State) ExitCode() int {
|
2016-08-30 12:44:06 -04:00
|
|
|
return s.ExitCodeValue
|
2013-11-21 15:21:03 -05:00
|
|
|
}
|
|
|
|
|
2016-07-03 08:47:39 -04:00
|
|
|
// SetExitCode sets current exitcode for the state. Take lock before if state
|
2016-06-14 14:11:43 -04:00
|
|
|
// may be shared.
|
|
|
|
func (s *State) SetExitCode(ec int) {
|
2016-08-30 12:44:06 -04:00
|
|
|
s.ExitCodeValue = ec
|
2016-06-14 14:11:43 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetRunning sets the state of the container to "running".
|
2016-03-18 14:50:19 -04:00
|
|
|
func (s *State) SetRunning(pid int, initial bool) {
|
2016-08-30 12:44:06 -04:00
|
|
|
s.ErrorMsg = ""
|
2014-07-29 03:14:10 -04:00
|
|
|
s.Running = true
|
2014-08-13 17:56:35 -04:00
|
|
|
s.Restarting = false
|
2016-08-30 12:44:06 -04:00
|
|
|
s.ExitCodeValue = 0
|
2014-07-29 03:14:10 -04:00
|
|
|
s.Pid = pid
|
2016-03-18 14:50:19 -04:00
|
|
|
if initial {
|
|
|
|
s.StartedAt = time.Now().UTC()
|
|
|
|
}
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetStopped sets the container state to "stopped" without locking.
|
2016-03-18 14:50:19 -04:00
|
|
|
func (s *State) SetStopped(exitStatus *ExitStatus) {
|
2014-07-29 03:14:10 -04:00
|
|
|
s.Running = false
|
2016-03-18 14:50:19 -04:00
|
|
|
s.Paused = false
|
2014-08-13 17:56:35 -04:00
|
|
|
s.Restarting = false
|
2014-07-29 03:14:10 -04:00
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
2015-10-31 22:31:24 -04:00
|
|
|
s.setFromExitStatus(exitStatus)
|
2014-07-29 03:14:10 -04:00
|
|
|
close(s.waitChan) // fire waiters for stop
|
|
|
|
s.waitChan = make(chan struct{})
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
2014-05-21 17:06:18 -04:00
|
|
|
|
2016-09-20 07:09:18 -04:00
|
|
|
// SetRestarting sets the container state to "restarting" without locking.
|
2015-11-12 14:55:17 -05:00
|
|
|
// It also sets the container PID to 0.
|
2016-03-18 14:50:19 -04:00
|
|
|
func (s *State) SetRestarting(exitStatus *ExitStatus) {
|
2014-08-13 17:56:35 -04:00
|
|
|
// we should consider the container running when it is restarting because of
|
|
|
|
// all the checks in docker around rm/stop/etc
|
|
|
|
s.Running = true
|
|
|
|
s.Restarting = true
|
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
2015-10-31 22:31:24 -04:00
|
|
|
s.setFromExitStatus(exitStatus)
|
2014-08-13 17:56:35 -04:00
|
|
|
close(s.waitChan) // fire waiters for stop
|
|
|
|
s.waitChan = make(chan struct{})
|
2014-08-11 14:07:37 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetError sets the container's error state. This is useful when we want to
|
2014-09-30 04:30:58 -04:00
|
|
|
// know the error that occurred when container transits to another state
|
|
|
|
// when inspecting it
|
2015-11-12 14:55:17 -05:00
|
|
|
func (s *State) SetError(err error) {
|
2016-08-30 12:44:06 -04:00
|
|
|
s.ErrorMsg = err.Error()
|
2014-09-30 04:30:58 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// IsPaused returns whether the container is paused or not.
|
|
|
|
func (s *State) IsPaused() bool {
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Lock()
|
2014-06-06 07:28:12 -04:00
|
|
|
res := s.Paused
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
return res
|
2014-05-21 17:06:18 -04:00
|
|
|
}
|
2015-03-12 15:26:17 -04:00
|
|
|
|
2016-01-25 10:38:03 -05:00
|
|
|
// IsRestarting returns whether the container is restarting or not.
|
|
|
|
func (s *State) IsRestarting() bool {
|
|
|
|
s.Lock()
|
|
|
|
res := s.Restarting
|
|
|
|
s.Unlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetRemovalInProgress sets the container state as being removed.
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This 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.
Our 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.
Moreover, 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:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By 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.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
// It returns true if the container was already in that state.
|
|
|
|
func (s *State) SetRemovalInProgress() bool {
|
2015-03-12 15:26:17 -04:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2015-11-12 14:55:17 -05:00
|
|
|
if s.RemovalInProgress {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This 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.
Our 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.
Moreover, 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:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By 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.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return true
|
2015-03-12 15:26:17 -04:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
s.RemovalInProgress = true
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This 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.
Our 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.
Moreover, 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:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By 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.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return false
|
2015-03-12 15:26:17 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 08:47:39 -04:00
|
|
|
// ResetRemovalInProgress makes the RemovalInProgress state to false.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (s *State) ResetRemovalInProgress() {
|
2015-03-12 15:26:17 -04:00
|
|
|
s.Lock()
|
2015-11-12 14:55:17 -05:00
|
|
|
s.RemovalInProgress = false
|
2015-03-12 15:26:17 -04:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetDead sets the container state to "dead"
|
|
|
|
func (s *State) SetDead() {
|
2015-03-12 15:26:17 -04:00
|
|
|
s.Lock()
|
|
|
|
s.Dead = true
|
|
|
|
s.Unlock()
|
|
|
|
}
|
2016-06-14 14:11:43 -04:00
|
|
|
|
|
|
|
// Error returns current error for the state.
|
|
|
|
func (s *State) Error() string {
|
2016-08-30 12:44:06 -04:00
|
|
|
return s.ErrorMsg
|
2016-06-14 14:11:43 -04:00
|
|
|
}
|