Skip to content

Commit

Permalink
Merge pull request #15 from CrowdStrike/chore/APIError_method
Browse files Browse the repository at this point in the history
chore: add Error behavior to APIError type
  • Loading branch information
jsteenb2 committed Feb 1, 2024
2 parents 9889afc + 84e0464 commit 736cbf6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/crowdstrike/gofalcon v0.4.2
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand Down Expand Up @@ -39,6 +40,5 @@ require (
golang.org/x/sys v0.11.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 12 additions & 6 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fdk
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/url"
Expand Down Expand Up @@ -109,14 +110,19 @@ type (
Method string
AccessToken string
}

// APIError defines a error that is shared back to the caller.
APIError struct {
Code int `json:"code"`
Message string `json:"message"`
}
)

// APIError defines a error that is shared back to the caller.
type APIError struct {
Code int `json:"code"`
Message string `json:"message"`
}

// Error provides a human readable error message.
func (a APIError) Error() string {
return fmt.Sprintf("[%d] %s", a.Code, a.Message)
}

// Response is the domain type for the response.
type Response struct {
Body json.Marshaler
Expand Down
14 changes: 14 additions & 0 deletions sdk_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fdk_test

import (
"fmt"
"net/http"
"testing"

fdk "github.com/CrowdStrike/foundry-fn-go"
Expand Down Expand Up @@ -56,3 +58,15 @@ func TestFn(t *testing.T) {
}

}

func TestAPIError(t *testing.T) {
errs := []fdk.APIError{
{Code: http.StatusInternalServerError, Message: "some internal error"},
{Code: http.StatusBadRequest, Message: "user dorked it up"},
{Message: "missing code will print a zero"},
}
for _, err := range errs {
want := fmt.Sprintf("[%d] %s", err.Code, err.Message)
equalVals(t, want, err.Error())
}
}

0 comments on commit 736cbf6

Please sign in to comment.