Skip to content

Commit

Permalink
new error features (#9)
Browse files Browse the repository at this point in the history
### Background

→ starting to actually use the model, changes are required.

### Changes

- expose DetailTag for special creation cases
- expose CustomSrc(...) in factory

### Testing

- existing?
  • Loading branch information
drshriveer authored Sep 21, 2023
1 parent 41f8aa9 commit 1e8636e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 25 deletions.
4 changes: 2 additions & 2 deletions gerror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gerror
// Error exposes methods that can be used as an error type.
type Error interface {
// Error implements the error interface; it returns a formatted message of the style
// "Name: <name>, DTag: <detailTag>, Src: <source>, Message: <message> \n <stack>
// "Name: <name>, DTag: <DetailTag>, Src: <source>, Message: <message> \n <stack>
Error() string

// Is implements the errors.Is interface.
Expand All @@ -25,7 +25,7 @@ type Error interface {
// ErrName returns the name of the error.
ErrName() string

// ErrDetailTag returns the detailTag (if set).
// ErrDetailTag returns the DetailTag (if set).
ErrDetailTag() string

// ErrStack returns an error stack (if available).
Expand Down
11 changes: 7 additions & 4 deletions gerror/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Factory interface {
// Source is a limited stack.
Src() Error

// CustomSrc returns a copy of the embedded error with a custom source and without a stack.
CustomSrc(src string) Error

// Stack returns a copy of the embedded error with a Stack trace and diagnostic info.
Stack() Error

Expand Down Expand Up @@ -62,18 +65,18 @@ func CloneBase[T factoryOf](
Name: base.Name,
Message: base.Message,
Source: base.Source,
detailTag: base.detailTag,
DetailTag: base.DetailTag,
factoryRef: base.factoryRef,
stack: base.stack,
srcError: base.srcError,
}

// handle detail tags:
if len(dTag) > 0 {
if len(clone.detailTag) == 0 {
clone.detailTag = dTag
if len(clone.DetailTag) == 0 {
clone.DetailTag = dTag
} else {
clone.detailTag += "-" + dTag
clone.DetailTag += "-" + dTag
}
}

Expand Down
24 changes: 12 additions & 12 deletions gerror/gen/error_types.gsort.go

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

8 changes: 8 additions & 0 deletions gerror/gen/gerror.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ func (e *{{$desc.TypeName}}) Src() gerror.Error {
return e.toPrimaryType(clone)
}

{{index $.FactoryComments "CustomSrc"}}
func (e *{{$desc.TypeName}}) CustomSrc(src string) gerror.Error {
clone := gerror.CloneBase(e, gerror.NoStack, "", "", nil)
clone.Source = src
return e.toPrimaryType(clone)
}


{{index $.FactoryComments "Stack"}}
func (e *{{$desc.TypeName}}) Stack() gerror.Error {
clone := gerror.CloneBase(e, gerror.DefaultStack, "", "", nil)
Expand Down
17 changes: 12 additions & 5 deletions gerror/gerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type GError struct {
// A derived source includes packageName, typeName (if applicable), and methodName.
Source string

// detailTag is a metric-safe 'tag' that can distinguish between different uses of the same error.
detailTag string
// DetailTag is a metric-safe 'tag' that can distinguish between different uses of the same error.
DetailTag string

// stack is the stack trace info.
stack Stack
Expand Down Expand Up @@ -55,7 +55,7 @@ func (e *GError) ErrName() string {

// ErrDetailTag returns the metric-safe detail-tag of the error.
func (e *GError) ErrDetailTag() string {
return e.detailTag
return e.DetailTag
}

// ErrStack returns an error stack (if available).
Expand Down Expand Up @@ -97,6 +97,13 @@ func (e *GError) Src() Error {
return CloneBase(e, SourceStack, "", "", nil)
}

// CustomSrc returns a copy of the embedded error with a custom source.
func (e *GError) CustomSrc(src string) Error {
base := CloneBase(e, NoStack, "", "", nil)
base.Source = src
return base
}

// Stack is a factory method for cloning the base error with a full sack trace.
func (e *GError) Stack() Error {
return CloneBase(e, DefaultStack, "", "", nil)
Expand All @@ -109,8 +116,8 @@ func (e *GError) Error() string {
if len(e.Name) > 0 {
result += "Name: " + e.Name + separator
}
if len(e.detailTag) > 0 {
result += "DTag: " + e.detailTag + separator
if len(e.DetailTag) > 0 {
result += "DTag: " + e.DetailTag + separator
}
if len(e.Source) > 0 {
result += "Source: " + e.Source + separator
Expand Down
7 changes: 7 additions & 0 deletions gerror/internal/custom_error.gerror.go

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

3 changes: 1 addition & 2 deletions gsort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ func (s GSortSortable) Less(i, j int) bool {
### TODO:

- arrive at a syntax that works to generate varients of sortable data.
- improve documentation.
-
- improve documentation. -

0 comments on commit 1e8636e

Please sign in to comment.