Skip to content

Commit

Permalink
feat(SPV-847): add option to wrap defined errors with cause (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 authored Aug 5, 2024
1 parent b39f901 commit 4984587
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
38 changes: 38 additions & 0 deletions models/errors.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package models

import (
"github.com/pkg/errors"
)

type ExtendedError interface {
error
GetCode() string
GetMessage() string
GetStatusCode() int
StackTrace() (trace errors.StackTrace)
}

// SPVError is extended error which holds information about http status and code that should be returned
type SPVError struct {
Code string
Message string
StatusCode int
cause error
}

type stackTracer interface {
StackTrace() errors.StackTrace
}

// ResponseError is an error which will be returned in HTTP response
Expand Down Expand Up @@ -41,3 +51,31 @@ func (e SPVError) GetMessage() string {
func (e SPVError) GetStatusCode() int {
return e.StatusCode
}

// StackTrace returns the error's stack trace.
func (e SPVError) StackTrace() (trace errors.StackTrace) {
err, ok := e.cause.(stackTracer)
if !ok {
return
}

trace = err.StackTrace()

return
}

func (e SPVError) Unwrap() error {
return e.cause
}

func (e SPVError) Wrap(err error) SPVError {
e.cause = err
return e
}

func (e SPVError) WithTrace(err error) SPVError {
if st := stackTracer(nil); !errors.As(e.cause, &st) {
return e.Wrap(errors.WithStack(err))
}
return e.Wrap(err)
}
5 changes: 4 additions & 1 deletion models/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions models/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4984587

Please sign in to comment.