Skip to content

Commit

Permalink
feat(dataverse): handle more properly errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Aug 29, 2024
1 parent 6c6be3f commit e89c911
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
9 changes: 5 additions & 4 deletions dataverse/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type client struct {

func NewDataverseClient(ctx context.Context,
dataverseClient dvschema.QueryClient,
cognitariumClient cgschema.QueryClient) (Client, error) {
cognitariumClient cgschema.QueryClient,
) (Client, error) {
cognitariumAddr, err := getCognitariumAddr(ctx, dataverseClient)
if err != nil {
return nil, fmt.Errorf("failed to get cognitarium address: %w", err)
Expand Down Expand Up @@ -69,16 +70,16 @@ func (c *client) GetResourceGovAddr(_ context.Context, resourceDID string) (stri
}

if len(response.Results.Bindings) != 1 {
return "", fmt.Errorf("could not find governance code")
return "", NewDVError(ErrNoResult, nil)
}

codeBinding, ok := response.Results.Bindings[0]["code"]
if !ok {
return "", fmt.Errorf("could not find governance code")
return "", NewDVError(ErrVarNotFound, nil)
}
code, ok := codeBinding.ValueType.(cgschema.URI)
if !ok {
return "", fmt.Errorf("could not decode governance code")
return "", NewDVError(ErrType, fmt.Errorf("expected URI, got %T", codeBinding.ValueType))
}
return string(*code.Value.Full), nil
}
Expand Down
6 changes: 3 additions & 3 deletions dataverse/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestClient_GetResourceGovAddr(t *testing.T) {
},
},
responseError: nil,
wantErr: fmt.Errorf("could not find governance code"),
wantErr: dataverse.NewDVError(dataverse.ErrVarNotFound, nil),
wantResult: "",
},
{
Expand All @@ -146,7 +146,7 @@ func TestClient_GetResourceGovAddr(t *testing.T) {
},
},
responseError: nil,
wantErr: fmt.Errorf("could not find governance code"),
wantErr: dataverse.NewDVError(dataverse.ErrNoResult, nil),
wantResult: "",
},
{
Expand All @@ -170,7 +170,7 @@ func TestClient_GetResourceGovAddr(t *testing.T) {
},
},
responseError: nil,
wantErr: fmt.Errorf("could not decode governance code"),
wantErr: dataverse.NewDVError(dataverse.ErrType, fmt.Errorf("expected URI, got %T", cgschema.BlankNode{})),
wantResult: "",
},
}
Expand Down
30 changes: 30 additions & 0 deletions dataverse/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dataverse

import "fmt"

type MessageError string

const (
ErrNoResult MessageError = "no result found in binding"
ErrVarNotFound MessageError = "variable not found in binding result"
ErrType MessageError = "variable result type mismatch in binding result"
)

type DVError struct {
message MessageError
detail error
}

func (e *DVError) Error() string {
if e.detail == nil {
return fmt.Sprintf("%v", e.message)
}
return fmt.Sprintf("%v: %v", e.message, e.detail)
}

func NewDVError(message MessageError, detail error) error {
return &DVError{
message: message,
detail: detail,
}
}

0 comments on commit e89c911

Please sign in to comment.