Skip to content

Commit

Permalink
Merge pull request #3703 from onflow/bastian/3688-standardize-interna…
Browse files Browse the repository at this point in the history
…l-error-messages

Standardize internal error messages
  • Loading branch information
turbolent authored Nov 28, 2024
2 parents d555d17 + 854dd10 commit fb30201
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
15 changes: 13 additions & 2 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func NewUnreachableError() InternalError {
return NewUnexpectedError("unreachable")
}

const InternalErrorMessagePrefix = "internal error:"

// InternalError is an implementation error, e.g: an unreachable code path (UnreachableError).
// A program should never throw an InternalError in an ideal world.
//
Expand Down Expand Up @@ -174,9 +176,18 @@ func (e UnexpectedError) Unwrap() error {
func (e UnexpectedError) Error() string {
message := e.Err.Error()
if len(e.Stack) == 0 {
return fmt.Sprintf("unexpected error: %s", message)
return fmt.Sprintf(
"%s unexpected: %s",
InternalErrorMessagePrefix,
message,
)
} else {
return fmt.Sprintf("unexpected error: %s\n%s", message, e.Stack)
return fmt.Sprintf(
"%s unexpected: %s\n%s",
InternalErrorMessagePrefix,
message,
e.Stack,
)
}
}

Expand Down
6 changes: 4 additions & 2 deletions interpreter/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func (UnsupportedTagDecodingError) IsInternalError() {}

func (e UnsupportedTagDecodingError) Error() string {
return fmt.Sprintf(
"internal error: unsupported decoded tag: %d",
"%s unsupported decoded tag: %d",
errors.InternalErrorMessagePrefix,
e.Tag,
)
}
Expand All @@ -69,7 +70,8 @@ func (InvalidStringLengthError) IsInternalError() {}

func (e InvalidStringLengthError) Error() string {
return fmt.Sprintf(
"internal error: invalid string length: got %d, expected max %d",
"%s invalid string length: got %d, expected max %d",
errors.InternalErrorMessagePrefix,
e.Length,
goMaxInt,
)
Expand Down
33 changes: 24 additions & 9 deletions interpreter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func (*unsupportedOperation) IsInternalError() {}

func (e *unsupportedOperation) Error() string {
return fmt.Sprintf(
"internal error: cannot evaluate unsupported %s operation: %s",
"%s cannot evaluate unsupported %s operation: %s",
errors.InternalErrorMessagePrefix,
e.kind.Name(),
e.operation.Symbol(),
)
Expand Down Expand Up @@ -323,7 +324,10 @@ var _ errors.InternalError = InvalidatedResourceError{}
func (InvalidatedResourceError) IsInternalError() {}

func (e InvalidatedResourceError) Error() string {
return "internal error: resource is invalidated and cannot be used anymore"
return fmt.Sprintf(
"%s resource is invalidated and cannot be used anymore",
errors.InternalErrorMessagePrefix,
)
}

// DestroyedResourceError is the error which is reported
Expand Down Expand Up @@ -633,7 +637,8 @@ func (MemberAccessTypeError) IsInternalError() {}

func (e MemberAccessTypeError) Error() string {
return fmt.Sprintf(
"invalid member access: expected `%s`, got `%s`",
"%s invalid member access: expected `%s`, got `%s`",
errors.InternalErrorMessagePrefix,
e.ExpectedType.QualifiedString(),
e.ActualType.QualifiedString(),
)
Expand All @@ -657,7 +662,8 @@ func (e ValueTransferTypeError) Error() string {
)

return fmt.Sprintf(
"invalid transfer of value: expected `%s`, got `%s`",
"%s invalid transfer of value: expected `%s`, got `%s`",
errors.InternalErrorMessagePrefix,
expected,
actual,
)
Expand All @@ -675,7 +681,8 @@ func (UnexpectedMappedEntitlementError) IsInternalError() {}

func (e UnexpectedMappedEntitlementError) Error() string {
return fmt.Sprintf(
"invalid transfer of value: found an unexpected runtime mapped entitlement `%s`",
"%s invalid transfer of value: found an unexpected runtime mapped entitlement `%s`",
errors.InternalErrorMessagePrefix,
e.Type.QualifiedString(),
)
}
Expand All @@ -692,7 +699,8 @@ func (ResourceConstructionError) IsInternalError() {}

func (e ResourceConstructionError) Error() string {
return fmt.Sprintf(
"cannot create resource `%s`: outside of declaring location %s",
"%s cannot create resource `%s`: outside of declaring location %s",
errors.InternalErrorMessagePrefix,
e.CompositeType.QualifiedString(),
e.CompositeType.Location.String(),
)
Expand Down Expand Up @@ -952,7 +960,8 @@ func (InvalidAttachmentOperationTargetError) IsInternalError() {}

func (e InvalidAttachmentOperationTargetError) Error() string {
return fmt.Sprintf(
"cannot add or remove attachment with non-owned value (%T)",
"%s cannot add or remove attachment with non-owned value (%T)",
errors.InternalErrorMessagePrefix,
e.Value,
)
}
Expand Down Expand Up @@ -1092,7 +1101,10 @@ var _ errors.InternalError = ResourceReferenceDereferenceError{}
func (ResourceReferenceDereferenceError) IsInternalError() {}

func (e ResourceReferenceDereferenceError) Error() string {
return "internal error: resource-references cannot be dereferenced"
return fmt.Sprintf(
"%s resource-references cannot be dereferenced",
errors.InternalErrorMessagePrefix,
)
}

// ResourceLossError
Expand All @@ -1117,7 +1129,10 @@ var _ errors.InternalError = InvalidCapabilityIDError{}
func (InvalidCapabilityIDError) IsInternalError() {}

func (e InvalidCapabilityIDError) Error() string {
return "capability created with invalid ID"
return fmt.Sprintf(
"%s capability created with invalid ID",
errors.InternalErrorMessagePrefix,
)
}

// ReferencedValueChangedError
Expand Down
6 changes: 5 additions & 1 deletion runtime/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,5 +380,9 @@ var _ errors.InternalError = UnreferencedRootSlabsError{}
func (UnreferencedRootSlabsError) IsInternalError() {}

func (e UnreferencedRootSlabsError) Error() string {
return fmt.Sprintf("slabs not referenced: %s", e.UnreferencedRootSlabIDs)
return fmt.Sprintf(
"%s slabs not referenced: %s",
errors.InternalErrorMessagePrefix,
e.UnreferencedRootSlabIDs,
)
}

0 comments on commit fb30201

Please sign in to comment.