mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
Remove string checking in API error handling
Use strongly typed errors to set HTTP status codes. Error interfaces are defined in the api/errors package and errors returned from controllers are checked against these interfaces. Errors can be wraeped in a pkg/errors.Causer, as long as somewhere in the line of causes one of the interfaces is implemented. The special error interfaces take precedence over Causer, meaning if both Causer and one of the new error interfaces are implemented, the Causer is not traversed. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
@@ -28,16 +28,20 @@ func (daemon *Daemon) ContainerCopy(name string, res string) (io.ReadCloser, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if res[0] == '/' || res[0] == '\\' {
|
||||
res = res[1:]
|
||||
}
|
||||
|
||||
// Make sure an online file-system operation is permitted.
|
||||
if err := daemon.isOnlineFSOperationPermitted(container); err != nil {
|
||||
return nil, err
|
||||
return nil, systemError{err}
|
||||
}
|
||||
|
||||
return daemon.containerCopy(container, res)
|
||||
data, err := daemon.containerCopy(container, res)
|
||||
if err == nil {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
return nil, containerFileNotFound{res, name}
|
||||
}
|
||||
return nil, systemError{err}
|
||||
}
|
||||
|
||||
// ContainerStatPath stats the filesystem resource at the specified path in the
|
||||
@@ -50,10 +54,18 @@ func (daemon *Daemon) ContainerStatPath(name string, path string) (stat *types.C
|
||||
|
||||
// Make sure an online file-system operation is permitted.
|
||||
if err := daemon.isOnlineFSOperationPermitted(container); err != nil {
|
||||
return nil, err
|
||||
return nil, systemError{err}
|
||||
}
|
||||
|
||||
return daemon.containerStatPath(container, path)
|
||||
stat, err = daemon.containerStatPath(container, path)
|
||||
if err == nil {
|
||||
return stat, nil
|
||||
}
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
return nil, containerFileNotFound{path, name}
|
||||
}
|
||||
return nil, systemError{err}
|
||||
}
|
||||
|
||||
// ContainerArchivePath creates an archive of the filesystem resource at the
|
||||
@@ -67,10 +79,18 @@ func (daemon *Daemon) ContainerArchivePath(name string, path string) (content io
|
||||
|
||||
// Make sure an online file-system operation is permitted.
|
||||
if err := daemon.isOnlineFSOperationPermitted(container); err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, systemError{err}
|
||||
}
|
||||
|
||||
return daemon.containerArchivePath(container, path)
|
||||
content, stat, err = daemon.containerArchivePath(container, path)
|
||||
if err == nil {
|
||||
return content, stat, nil
|
||||
}
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil, containerFileNotFound{path, name}
|
||||
}
|
||||
return nil, nil, systemError{err}
|
||||
}
|
||||
|
||||
// ContainerExtractToDir extracts the given archive to the specified location
|
||||
@@ -87,10 +107,18 @@ func (daemon *Daemon) ContainerExtractToDir(name, path string, copyUIDGID, noOve
|
||||
|
||||
// Make sure an online file-system operation is permitted.
|
||||
if err := daemon.isOnlineFSOperationPermitted(container); err != nil {
|
||||
return err
|
||||
return systemError{err}
|
||||
}
|
||||
|
||||
return daemon.containerExtractToDir(container, path, copyUIDGID, noOverwriteDirNonDir, content)
|
||||
err = daemon.containerExtractToDir(container, path, copyUIDGID, noOverwriteDirNonDir, content)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
return containerFileNotFound{path, name}
|
||||
}
|
||||
return systemError{err}
|
||||
}
|
||||
|
||||
// containerStatPath stats the filesystem resource at the specified path in this
|
||||
@@ -297,6 +325,9 @@ func (daemon *Daemon) containerExtractToDir(container *container.Container, path
|
||||
}
|
||||
|
||||
func (daemon *Daemon) containerCopy(container *container.Container, resource string) (rc io.ReadCloser, err error) {
|
||||
if resource[0] == '/' || resource[0] == '\\' {
|
||||
resource = resource[1:]
|
||||
}
|
||||
container.Lock()
|
||||
|
||||
defer func() {
|
||||
|
||||
Reference in New Issue
Block a user