Skip to content

Commit

Permalink
feat: error handling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulguptajss authored and cgrinds committed Jul 11, 2023
1 parent 7ff0f48 commit a8865fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pkg/api/ontapi/zapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,9 @@ func (c *Client) invoke(withTimers bool) (*node.Node, time.Duration, time.Durati

if response.StatusCode != 200 {
if response.StatusCode == 401 {
return result, responseT, parseT, errs.NewWithStatus(errs.ErrAuthFailed, response.Status, response.StatusCode)
return result, responseT, parseT, errs.New(errs.ErrAuthFailed, response.Status, errs.WithStatus(response.StatusCode))
}
return result, responseT, parseT, errs.NewWithStatus(errs.ErrAPIResponse, response.Status, response.StatusCode)
return result, responseT, parseT, errs.New(errs.ErrAPIResponse, response.Status, errs.WithStatus(response.StatusCode))
}

// read response body
Expand Down Expand Up @@ -584,7 +584,7 @@ func (c *Client) invoke(withTimers bool) (*node.Node, time.Duration, time.Durati
reason = "no reason"
}
errNum, _ = result.GetAttrValueS("errno")
err = errs.NewWithErrorNum(errs.ErrAPIRequestRejected, reason, errNum)
err = errs.New(errs.ErrAPIRequestRejected, reason, errs.WithErrorNum(errNum))
return result, responseT, parseT, err
}

Expand Down
30 changes: 20 additions & 10 deletions pkg/errs/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ type HarvestError struct {
StatusCode int
}

type Option func(*HarvestError)

func WithStatus(statusCode int) Option {
return func(e *HarvestError) {
e.StatusCode = statusCode
}
}

func WithErrorNum(errNum string) Option {
return func(e *HarvestError) {
e.ErrNum = errNum
}
}

func (e HarvestError) Error() string {
if e.Inner == nil {
return e.Message
Expand All @@ -59,14 +73,10 @@ func (e HarvestError) Unwrap() error {
return e.Inner
}

func New(err error, message string) error {
return HarvestError{Message: message, Inner: err}
}

func NewWithStatus(err error, message string, statusCode int) error {
return HarvestError{Message: message, Inner: err, StatusCode: statusCode}
}

func NewWithErrorNum(err error, message string, errNum string) error {
return HarvestError{Message: message, Inner: err, ErrNum: errNum}
func New(innerError error, message string, opts ...Option) error {
err := HarvestError{Message: message, Inner: innerError}
for _, opt := range opts {
opt(&err)
}
return err
}

0 comments on commit a8865fe

Please sign in to comment.