diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..ba540e6 --- /dev/null +++ b/errors.go @@ -0,0 +1,12 @@ +package process + +import coreerr "dappco.re/go/core/log" + +// ServiceError wraps a service-level failure with a message string. +// +// Example: +// +// return process.ServiceError("context is required", process.ErrContextRequired) +func ServiceError(message string, err error) error { + return coreerr.E("ServiceError", message, err) +} diff --git a/errors_test.go b/errors_test.go new file mode 100644 index 0000000..61d3f42 --- /dev/null +++ b/errors_test.go @@ -0,0 +1,15 @@ +package process + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestServiceError(t *testing.T) { + err := ServiceError("service failed", ErrContextRequired) + require.Error(t, err) + assert.Contains(t, err.Error(), "service failed") + assert.ErrorIs(t, err, ErrContextRequired) +} diff --git a/service.go b/service.go index edb30c5..28979f1 100644 --- a/service.go +++ b/service.go @@ -138,10 +138,10 @@ func (s *Service) Start(ctx context.Context, command string, args ...string) (*P // proc, err := svc.StartWithOptions(ctx, process.RunOptions{Command: "pwd", Dir: "/tmp"}) func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Process, error) { if opts.Command == "" { - return nil, coreerr.E("Service.StartWithOptions", "command is required", nil) + return nil, ServiceError("command is required", nil) } if ctx == nil { - return nil, coreerr.E("Service.StartWithOptions", "context is required", ErrContextRequired) + return nil, ServiceError("context is required", ErrContextRequired) } id := fmt.Sprintf("proc-%d", s.idCounter.Add(1)) @@ -426,7 +426,7 @@ func (s *Service) Kill(id string) error { // _ = svc.KillPID(1234) func (s *Service) KillPID(pid int) error { if pid <= 0 { - return coreerr.E("Service.KillPID", "pid must be positive", nil) + return ServiceError("pid must be positive", nil) } if proc := s.findByPID(pid); proc != nil { @@ -467,7 +467,7 @@ func (s *Service) Signal(id string, sig os.Signal) error { // _ = svc.SignalPID(1234, syscall.SIGTERM) func (s *Service) SignalPID(pid int, sig os.Signal) error { if pid <= 0 { - return coreerr.E("Service.SignalPID", "pid must be positive", nil) + return ServiceError("pid must be positive", nil) } if proc := s.findByPID(pid); proc != nil {