Wrap errors to ensure a stacktrack is returned where possible

This commit is contained in:
Dane Everitt 2019-11-16 17:05:21 -08:00
parent cae0090763
commit 48a303608a
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 44 additions and 44 deletions

View File

@ -120,7 +120,7 @@ func (d *DockerEnvironment) IsRunning() (bool, error) {
func (d *DockerEnvironment) OnBeforeStart() error { func (d *DockerEnvironment) OnBeforeStart() error {
c, err := d.Server.GetProcessConfiguration() c, err := d.Server.GetProcessConfiguration()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
d.Server.processConfiguration = c d.Server.processConfiguration = c
@ -143,7 +143,7 @@ func (d *DockerEnvironment) Start() error {
c, err := d.Client.ContainerInspect(context.Background(), d.Server.Uuid) c, err := d.Client.ContainerInspect(context.Background(), d.Server.Uuid)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
// No reason to try starting a container that is already running. // No reason to try starting a container that is already running.
@ -163,7 +163,7 @@ func (d *DockerEnvironment) Start() error {
// Run the before start function and wait for it to finish. // Run the before start function and wait for it to finish.
if err := d.OnBeforeStart(); err != nil { if err := d.OnBeforeStart(); err != nil {
return err return errors.WithStack(err)
} }
// Truncate the log file so we don't end up outputting a bunch of useless log information // Truncate the log file so we don't end up outputting a bunch of useless log information
@ -171,19 +171,19 @@ func (d *DockerEnvironment) Start() error {
// to truncate them. // to truncate them.
if _, err := os.Stat(c.LogPath); err == nil { if _, err := os.Stat(c.LogPath); err == nil {
if err := os.Truncate(c.LogPath, 0); err != nil { if err := os.Truncate(c.LogPath, 0); err != nil {
return err return errors.WithStack(err)
} }
} }
// Reset the permissions on files for the server before actually trying // Reset the permissions on files for the server before actually trying
// to start it. // to start it.
if err := d.Server.Filesystem.Chown("/"); err != nil { if err := d.Server.Filesystem.Chown("/"); err != nil {
return err return errors.WithStack(err)
} }
opts := types.ContainerStartOptions{} opts := types.ContainerStartOptions{}
if err := d.Client.ContainerStart(context.Background(), d.Server.Uuid, opts); err != nil { if err := d.Client.ContainerStart(context.Background(), d.Server.Uuid, opts); err != nil {
return err return errors.WithStack(err)
} }
// No errors, good to continue through. // No errors, good to continue through.
@ -216,7 +216,7 @@ func (d *DockerEnvironment) Terminate(signal os.Signal) error {
c, err := d.Client.ContainerInspect(ctx, d.Server.Uuid) c, err := d.Client.ContainerInspect(ctx, d.Server.Uuid)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
if !c.State.Running { if !c.State.Running {
@ -240,7 +240,7 @@ func (d *DockerEnvironment) Attach() error {
} }
if err := d.FollowConsoleOutput(); err != nil { if err := d.FollowConsoleOutput(); err != nil {
return err return errors.WithStack(err)
} }
ctx := context.Background() ctx := context.Background()
@ -254,7 +254,7 @@ func (d *DockerEnvironment) Attach() error {
}) })
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
console := Console{ console := Console{
@ -283,7 +283,7 @@ func (d *DockerEnvironment) Attach() error {
func (d *DockerEnvironment) FollowConsoleOutput() error { func (d *DockerEnvironment) FollowConsoleOutput() error {
if exists, err := d.Exists(); !exists { if exists, err := d.Exists(); !exists {
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
return errors.New(fmt.Sprintf("no such container: %s", d.Server.Uuid)) return errors.New(fmt.Sprintf("no such container: %s", d.Server.Uuid))
@ -312,7 +312,7 @@ func (d *DockerEnvironment) FollowConsoleOutput() error {
} }
}(reader) }(reader)
return err return errors.WithStack(err)
} }
// Enables resource polling on the docker instance. Except we aren't actually polling Docker for this // Enables resource polling on the docker instance. Except we aren't actually polling Docker for this
@ -327,7 +327,7 @@ func (d *DockerEnvironment) EnableResourcePolling() error {
stats, err := d.Client.ContainerStats(ctx, d.Server.Uuid, true) stats, err := d.Client.ContainerStats(ctx, d.Server.Uuid, true)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
d.stats = stats.Body d.stats = stats.Body
@ -383,7 +383,7 @@ func (d *DockerEnvironment) DisableResourcePolling() error {
d.Server.Resources.Network.TxBytes = 0 d.Server.Resources.Network.TxBytes = 0
d.Server.Resources.Network.RxBytes = 0 d.Server.Resources.Network.RxBytes = 0
return err return errors.WithStack(err)
} }
// Creates a new container for the server using all of the data that is currently // Creates a new container for the server using all of the data that is currently
@ -394,14 +394,14 @@ func (d *DockerEnvironment) Create() error {
ctx := context.Background() ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv) cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
var oomDisabled = true var oomDisabled = true
// Ensure the data directory exists before getting too far through this process. // Ensure the data directory exists before getting too far through this process.
if err := d.Server.Filesystem.EnsureDataDirectory(); err != nil { if err := d.Server.Filesystem.EnsureDataDirectory(); err != nil {
return err return errors.WithStack(err)
} }
// If the container already exists don't hit the user with an error, just return // If the container already exists don't hit the user with an error, just return
@ -410,7 +410,7 @@ func (d *DockerEnvironment) Create() error {
if _, err := cli.ContainerInspect(ctx, d.Server.Uuid); err == nil { if _, err := cli.ContainerInspect(ctx, d.Server.Uuid); err == nil {
return nil return nil
} else if !client.IsErrNotFound(err) { } else if !client.IsErrNotFound(err) {
return err return errors.WithStack(err)
} }
conf := &container.Config{ conf := &container.Config{
@ -501,7 +501,7 @@ func (d *DockerEnvironment) Create() error {
} }
if _, err := cli.ContainerCreate(ctx, conf, hostConf, nil, d.Server.Uuid); err != nil { if _, err := cli.ContainerCreate(ctx, conf, hostConf, nil, d.Server.Uuid); err != nil {
return err return errors.WithStack(err)
} }
return nil return nil
@ -516,7 +516,7 @@ func (d *DockerEnvironment) SendCommand(c string) error {
_, err := d.stream.Conn.Write([]byte(c + "\n")) _, err := d.stream.Conn.Write([]byte(c + "\n"))
return err return errors.WithStack(err)
} }
// Reads the log file for the server. This does not care if the server is running or not, it will // Reads the log file for the server. This does not care if the server is running or not, it will

View File

@ -205,21 +205,21 @@ func (fs *Filesystem) Readfile(p string) (io.Reader, error) {
func (fs *Filesystem) Writefile(p string, r io.Reader) error { func (fs *Filesystem) Writefile(p string, r io.Reader) error {
cleaned, err := fs.SafePath(p) cleaned, err := fs.SafePath(p)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
// If the file does not exist on the system already go ahead and create the pathway // If the file does not exist on the system already go ahead and create the pathway
// to it and an empty file. We'll then write to it later on after this completes. // to it and an empty file. We'll then write to it later on after this completes.
if stat, err := os.Stat(cleaned); err != nil && os.IsNotExist(err) { if stat, err := os.Stat(cleaned); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(cleaned), 0755); err != nil { if err := os.MkdirAll(filepath.Dir(cleaned), 0755); err != nil {
return err return errors.WithStack(err)
} }
if err := fs.Chown(filepath.Dir(cleaned)); err != nil { if err := fs.Chown(filepath.Dir(cleaned)); err != nil {
return err return errors.WithStack(err)
} }
} else if err != nil { } else if err != nil {
return err return errors.WithStack(err)
} else if stat.IsDir() { } else if stat.IsDir() {
return errors.New("cannot use a directory as a file for writing") return errors.New("cannot use a directory as a file for writing")
} }
@ -228,7 +228,7 @@ func (fs *Filesystem) Writefile(p string, r io.Reader) error {
// truncate the existing file. // truncate the existing file.
file, err := os.OpenFile(cleaned, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) file, err := os.OpenFile(cleaned, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
defer file.Close() defer file.Close()
@ -240,7 +240,7 @@ func (fs *Filesystem) Writefile(p string, r io.Reader) error {
for { for {
n, err := r.Read(buf) n, err := r.Read(buf)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
return err return errors.WithStack(err)
} }
if n == 0 { if n == 0 {
@ -248,12 +248,12 @@ func (fs *Filesystem) Writefile(p string, r io.Reader) error {
} }
if _, err := w.Write(buf[:n]); err != nil { if _, err := w.Write(buf[:n]); err != nil {
return err return errors.WithStack(err)
} }
} }
if err := w.Flush(); err != nil { if err := w.Flush(); err != nil {
return err return errors.WithStack(err)
} }
// Finally, chown the file to ensure the permissions don't end up out-of-whack // Finally, chown the file to ensure the permissions don't end up out-of-whack
@ -324,7 +324,7 @@ func (fs *Filesystem) Stat(p string) (*Stat, error) {
func (fs *Filesystem) CreateDirectory(name string, p string) error { func (fs *Filesystem) CreateDirectory(name string, p string) error {
cleaned, err := fs.SafePath(path.Join(p, name)) cleaned, err := fs.SafePath(path.Join(p, name))
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
return os.MkdirAll(cleaned, 0755) return os.MkdirAll(cleaned, 0755)
@ -334,12 +334,12 @@ func (fs *Filesystem) CreateDirectory(name string, p string) error {
func (fs *Filesystem) Rename(from string, to string) error { func (fs *Filesystem) Rename(from string, to string) error {
cleanedFrom, err := fs.SafePath(from) cleanedFrom, err := fs.SafePath(from)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
cleanedTo, err := fs.SafePath(to) cleanedTo, err := fs.SafePath(to)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
return os.Rename(cleanedFrom, cleanedTo) return os.Rename(cleanedFrom, cleanedTo)
@ -350,11 +350,11 @@ func (fs *Filesystem) Rename(from string, to string) error {
func (fs *Filesystem) Chown(path string) error { func (fs *Filesystem) Chown(path string) error {
cleaned, err := fs.SafePath(path) cleaned, err := fs.SafePath(path)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
if s, err := os.Stat(cleaned); err != nil { if s, err := os.Stat(cleaned); err != nil {
return err return errors.WithStack(err)
} else if !s.IsDir() { } else if !s.IsDir() {
return os.Chown(cleaned, fs.Configuration.User.Uid, fs.Configuration.User.Gid) return os.Chown(cleaned, fs.Configuration.User.Uid, fs.Configuration.User.Gid)
} }
@ -370,12 +370,12 @@ func (fs *Filesystem) chownDirectory(path string) error {
cleaned, err := fs.SafePath(path) cleaned, err := fs.SafePath(path)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
files, err := ioutil.ReadDir(cleaned) files, err := ioutil.ReadDir(cleaned)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
for _, f := range files { for _, f := range files {
@ -403,7 +403,7 @@ func (fs *Filesystem) chownDirectory(path string) error {
func (fs *Filesystem) Copy(p string) error { func (fs *Filesystem) Copy(p string) error {
cleaned, err := fs.SafePath(p) cleaned, err := fs.SafePath(p)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
if s, err := os.Stat(cleaned); (err != nil && os.IsNotExist(err)) || s.IsDir() || !s.Mode().IsRegular() { if s, err := os.Stat(cleaned); (err != nil && os.IsNotExist(err)) || s.IsDir() || !s.Mode().IsRegular() {
@ -412,7 +412,7 @@ func (fs *Filesystem) Copy(p string) error {
// re-evaluate if this is a smart decision (I'm guessing not). // re-evaluate if this is a smart decision (I'm guessing not).
return nil return nil
} else if err != nil { } else if err != nil {
return err return errors.WithStack(err)
} }
base := filepath.Base(cleaned) base := filepath.Base(cleaned)
@ -438,12 +438,12 @@ func (fs *Filesystem) Copy(p string) error {
tryName := fmt.Sprintf("%s%s%s", name, copySuffix, extension) tryName := fmt.Sprintf("%s%s%s", name, copySuffix, extension)
tryLocation, err := fs.SafePath(path.Join(relative, tryName)) tryLocation, err := fs.SafePath(path.Join(relative, tryName))
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
// If the file exists, continue to the next loop, otherwise we're good to start a copy. // If the file exists, continue to the next loop, otherwise we're good to start a copy.
if _, err := os.Stat(tryLocation); err != nil && !os.IsNotExist(err) { if _, err := os.Stat(tryLocation); err != nil && !os.IsNotExist(err) {
return err return errors.WithStack(err)
} else if os.IsNotExist(err) { } else if os.IsNotExist(err) {
break break
} }
@ -455,23 +455,23 @@ func (fs *Filesystem) Copy(p string) error {
finalPath, err := fs.SafePath(path.Join(relative, fmt.Sprintf("%s%s%s", name, copySuffix, extension))) finalPath, err := fs.SafePath(path.Join(relative, fmt.Sprintf("%s%s%s", name, copySuffix, extension)))
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
source, err := os.Open(cleaned) source, err := os.Open(cleaned)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
defer source.Close() defer source.Close()
dest, err := os.Create(finalPath) dest, err := os.Create(finalPath)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
defer dest.Close() defer dest.Close()
if _, err := io.Copy(dest, source); err != nil { if _, err := io.Copy(dest, source); err != nil {
return err return errors.WithStack(err)
} }
return nil return nil
@ -482,7 +482,7 @@ func (fs *Filesystem) Copy(p string) error {
func (fs *Filesystem) Delete(p string) error { func (fs *Filesystem) Delete(p string) error {
cleaned, err := fs.SafePath(p) cleaned, err := fs.SafePath(p)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
// Block any whoopsies. // Block any whoopsies.
@ -557,12 +557,12 @@ func (fs *Filesystem) ListDirectory(p string) ([]*Stat, error) {
// Ensures that the data directory for the server instance exists. // Ensures that the data directory for the server instance exists.
func (fs *Filesystem) EnsureDataDirectory() error { func (fs *Filesystem) EnsureDataDirectory() error {
if _, err := os.Stat(fs.Path()); err != nil && !os.IsNotExist(err) { if _, err := os.Stat(fs.Path()); err != nil && !os.IsNotExist(err) {
return err return errors.WithStack(err)
} else if err != nil { } else if err != nil {
// Create the server data directory because it does not currently exist // Create the server data directory because it does not currently exist
// on the system. // on the system.
if err := os.MkdirAll(fs.Path(), 0600); err != nil { if err := os.MkdirAll(fs.Path(), 0600); err != nil {
return err return errors.WithStack(err)
} }
} }