Skip to content
This repository was archived by the owner on Jun 18, 2022. It is now read-only.

Commit 07333cc

Browse files
author
Craig Jellick
committed
Merge pull request #81 from cjellick/send404
Return 404 status code on get and delete
2 parents 13d161c + 44f131f commit 07333cc

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

daemon/daemon.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@ func makeHandlerFunc(method string, route string, version string, f requestHandl
138138
}
139139
}
140140
if err := f(version, w, r, mux.Vars(r)); err != nil {
141-
log.Errorf("Handler for %s %s returned error: %s", method, route, err)
142-
http.Error(w, err.Error(), http.StatusBadRequest)
141+
statusCode := checkForStatusCode(err)
142+
if statusCode == 0 {
143+
log.Errorf("Handler for %s %s returned error: %s", method, route, err)
144+
statusCode = http.StatusBadRequest
145+
}
146+
http.Error(w, err.Error(), statusCode)
143147
}
144148
}
145149
}
@@ -379,3 +383,19 @@ func (s *daemon) getBackupOpsForVolume(volume *Volume) (BackupOperations, error)
379383
}
380384
return driver.BackupOps()
381385
}
386+
387+
type APIError struct {
388+
error string
389+
statusCode int
390+
}
391+
392+
func (e APIError) Error() string {
393+
return e.error
394+
}
395+
396+
func checkForStatusCode(err error) int {
397+
if apiError, ok := err.(APIError); ok {
398+
return apiError.statusCode
399+
}
400+
return 0
401+
}

daemon/volume.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ type Volume struct {
1717
DriverName string
1818
}
1919

20+
var notFoundAPIError = APIError{
21+
statusCode: http.StatusNotFound,
22+
error: fmt.Sprintf("Volume not found."),
23+
}
24+
2025
func (s *daemon) getVolume(name string) *Volume {
2126
driver, err := s.getDriverForVolume(name)
2227
if err != nil {
@@ -182,7 +187,7 @@ func (s *daemon) processVolumeDelete(request *api.VolumeDeleteRequest) error {
182187

183188
volume := s.getVolume(name)
184189
if volume == nil {
185-
return fmt.Errorf("Cannot find volume %s", name)
190+
return notFoundAPIError
186191
}
187192

188193
// In the case of snapshot is not supported, snapshots would be nil
@@ -325,7 +330,7 @@ func (s *daemon) doVolumeList(version string, w http.ResponseWriter, r *http.Req
325330
func (s *daemon) inspectVolume(name string) ([]byte, error) {
326331
volume := s.getVolume(name)
327332
if volume == nil {
328-
return nil, fmt.Errorf("Cannot find volume %v", name)
333+
return nil, notFoundAPIError
329334
}
330335
resp, err := s.listVolumeInfo(volume)
331336
if err != nil {

0 commit comments

Comments
 (0)