Skip to content

Commit aba7d68

Browse files
committed
added better error handling
1 parent d1b64c4 commit aba7d68

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

process.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TODO: Add Process Kill method.
12
package process
23

34
import (
@@ -15,6 +16,24 @@ import (
1516
"unsafe"
1617
)
1718

19+
var (
20+
// ErrProcCommandEmpty is an error that occurs when calling FindProcess
21+
// for a Process and the Process's command is empty.
22+
ErrProcCommandEmpty = fmt.Errorf("error: process command is empty")
23+
24+
// ErrProcNotRunning is an error that is returned when running a health check
25+
// for a process and the process is not running.
26+
ErrProcNotRunning = fmt.Errorf("error: process is not running")
27+
28+
// ErrProcNotInTty is an error that occurs when trying to open a Process's
29+
// tty but the Process does not have it's tty value set.
30+
ErrProcNotInTty = fmt.Errorf("process is not in a tty")
31+
32+
// ErrInvalidNumber is an error that occurs when the number scanned in
33+
// whilst searching for a ProcessByName is less than 0.
34+
ErrInvalidNumber = fmt.Errorf("please enter a valid number")
35+
)
36+
1837
// Process describes a unix process.
1938
//
2039
// The Process's Pid and the methods Kill(), Release(), Signal()
@@ -45,7 +64,7 @@ func (p *Process) String() string {
4564
// HealthCheck signals the process to see if it's still running.
4665
func (p *Process) HealthCheck() error {
4766
if err := p.Signal(syscall.Signal(0)); err != nil {
48-
return fmt.Errorf("process is not running")
67+
return ErrProcNotRunning
4968
}
5069
return nil
5170
}
@@ -134,7 +153,7 @@ func (p *Process) FindProcess() error {
134153
}
135154

136155
if p.Cmd == "" {
137-
return fmt.Errorf("process command is empty")
156+
return ErrProcCommandEmpty
138157
}
139158

140159
ps, err := exec.Command("ps", "-e").Output()
@@ -182,7 +201,7 @@ func (p *Process) InTty() bool {
182201
// OpenTty returns an opened file handle to the tty of the process.
183202
func (p *Process) OpenTty() (*os.File, error) {
184203
if !p.InTty() {
185-
return nil, fmt.Errorf("process is not in a tty")
204+
return nil, ErrProcNotInTty
186205
}
187206
return os.Open("/dev/" + p.Tty)
188207
}
@@ -227,7 +246,7 @@ func FindByName(stdout io.Writer, stdin io.Reader, name string) (*Process, error
227246
fmt.Fscanf(stdin, "%d", &procNumber)
228247

229248
if procNumber < 0 {
230-
return nil, fmt.Errorf("please enter a valid number")
249+
return nil, ErrInvalidNumber
231250
}
232251

233252
pid, err := strconv.Atoi(strings.TrimSpace(strings.Split(names[procNumber], " ")[0]))

0 commit comments

Comments
 (0)