feat(process): add service error helper
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
429675ca29
commit
b74ee080a2
3 changed files with 31 additions and 4 deletions
12
errors.go
Normal file
12
errors.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
15
errors_test.go
Normal file
15
errors_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue