Browse code

container: Handle failed memdb lookups

If a container doesn't exist in the memdb, First will return nil, not an
error. This should be checked for before using the result.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>

Aaron Lehmann authored on 2017/06/30 07:02:46
Showing 1 changed files
... ...
@@ -73,6 +73,17 @@ type memDB struct {
73 73
 	store *memdb.MemDB
74 74
 }
75 75
 
76
+// NoSuchContainerError indicates that the container wasn't found in the
77
+// database.
78
+type NoSuchContainerError struct {
79
+	id string
80
+}
81
+
82
+// Error satisfies the error interface.
83
+func (e NoSuchContainerError) Error() string {
84
+	return "no such container " + e.id
85
+}
86
+
76 87
 // NewViewDB provides the default implementation, with the default schema
77 88
 func NewViewDB() (ViewDB, error) {
78 89
 	store, err := memdb.NewMemDB(schema)
... ...
@@ -134,6 +145,9 @@ func (v *memdbView) Get(id string) (*Snapshot, error) {
134 134
 	if err != nil {
135 135
 		return nil, err
136 136
 	}
137
+	if s == nil {
138
+		return nil, NoSuchContainerError{id: id}
139
+	}
137 140
 	return v.transform(s.(*Container)), nil
138 141
 }
139 142