Skip to content

Commit

Permalink
handle errors related to sqrt returning NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Sep 25, 2024
1 parent 9b41354 commit 88e90c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### 1.5.0 (Next)
- Do not coerce `number` type ID to `string` type ID (may cause superficial plan changes to existing states).
- Add `exp`, `mod`, and `round` functions.
- Add `exp`, `mod`, `round`, and `sqrt` functions.

### 1.4.1
- Add `sorted` parameter to `list_index` function.
Expand Down
11 changes: 10 additions & 1 deletion stdlib/number/sqrt_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mschuchard/terraform-provider-stdlib/internal"
Expand Down Expand Up @@ -65,8 +66,16 @@ func (_ *sqrtDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
// initialize input number
inputNum := state.Param.ValueFloat64()

// determine the sqrted integer
// determine the square root
sqrt := math.Sqrt(inputNum)
if math.IsNaN(sqrt) {
resp.Diagnostics.AddAttributeError(
path.Root("param"),
"Invalid Value",
"The square root of the input parameter must return a valid number, but instead returned 'NaN'.",
)
return
}

// provide debug logging
ctx = tflog.SetField(ctx, "stdlib_sqrt_result", sqrt)
Expand Down

0 comments on commit 88e90c2

Please sign in to comment.