Skip to content

Commit

Permalink
chore: add more specific api error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasZeissner committed Sep 16, 2024
1 parent fabbe31 commit 44191ac
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 70 deletions.
34 changes: 34 additions & 0 deletions internal/api/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package api

import (
"fmt"

"github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/common"
)

var (
ErrEmptyMsg = fmt.Errorf("empty message")
ErrNotFound = fmt.Errorf("resource not found")
ErrSubgraphCompositionFailed = fmt.Errorf("subgraph composition failed")
)

func IsNotFoundError(err error) bool {
return err == ErrNotFound
}

func IsSubgraphCompositionFailedError(err error) bool {
return err == ErrSubgraphCompositionFailed
}

func handleErrorCodes(statusCode common.EnumStatusCode) error {
switch statusCode {
case common.EnumStatusCode_OK:
return nil
case common.EnumStatusCode_ERR_SUBGRAPH_COMPOSITION_FAILED:
return ErrSubgraphCompositionFailed
case common.EnumStatusCode_ERR_NOT_FOUND:
return ErrNotFound
default:
return fmt.Errorf("failed to create resource: %s", statusCode.String())
}
}
30 changes: 16 additions & 14 deletions internal/api/federated_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package api

import (
"context"
"fmt"

"connectrpc.com/connect"
"github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/platform/v1"
)

Expand Down Expand Up @@ -33,11 +31,12 @@ func (p *PlatformClient) CreateFederatedGraph(ctx context.Context, admissionWebh
}

if response.Msg == nil {
return nil, fmt.Errorf("failed to create federated graph: %s, the server response is nil", graph.Name)
return nil, ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return nil, fmt.Errorf("failed to create federated graph: %s", response.Msg.GetResponse().GetDetails())
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return nil, err
}

return response.Msg, nil
Expand Down Expand Up @@ -65,11 +64,12 @@ func (p *PlatformClient) UpdateFederatedGraph(ctx context.Context, admissionWebh
}

if response.Msg == nil {
return nil, fmt.Errorf("failed to create federated graph: %s, the server response is nil", graph.Name)
return nil, ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return nil, fmt.Errorf("failed to update federated graph: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return nil, err
}

return response.Msg, nil
Expand All @@ -87,11 +87,12 @@ func (p *PlatformClient) DeleteFederatedGraph(ctx context.Context, name, namespa
}

if response.Msg == nil {
return fmt.Errorf("failed to delete federated graph: %s, the server response is nil", name)
return ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to delete federated graph: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}

return nil
Expand All @@ -109,11 +110,12 @@ func (p *PlatformClient) GetFederatedGraph(ctx context.Context, name, namespace
}

if response.Msg == nil {
return nil, fmt.Errorf("failed to get federated graph: %s, the server response is nil", name)
return nil, ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return nil, fmt.Errorf("failed to get federated graph: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return nil, err
}

return response.Msg, nil
Expand Down
30 changes: 16 additions & 14 deletions internal/api/monograph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package api

import (
"context"
"fmt"

"connectrpc.com/connect"
"github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/platform/v1"
)

Expand All @@ -28,11 +26,12 @@ func (p PlatformClient) CreateMonograph(ctx context.Context, name string, namesp
}

if response.Msg == nil {
return fmt.Errorf("failed to create monograph: %s, the server response is nil", name)
return ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to create monograph: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}

return nil
Expand All @@ -57,11 +56,12 @@ func (p PlatformClient) UpdateMonograph(ctx context.Context, name string, namesp
}

if response.Msg == nil {
return fmt.Errorf("failed to update monograph: %s, the server response is nil", name)
return ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to update monograph: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}

return nil
Expand All @@ -78,11 +78,12 @@ func (p PlatformClient) DeleteMonograph(ctx context.Context, name string, namesp
}

if response.Msg == nil {
return fmt.Errorf("failed to delete monograph: %s, the server response is nil", name)
return ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to delete monograph: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}

return nil
Expand All @@ -99,11 +100,12 @@ func (p PlatformClient) GetMonograph(ctx context.Context, name string, namespace
}

if response.Msg == nil {
return nil, fmt.Errorf("failed to get monograph: %s, the server response is nil", name)
return nil, ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return nil, fmt.Errorf("failed to get monograph: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return nil, err
}

return response.Msg.Graph, nil
Expand Down
36 changes: 20 additions & 16 deletions internal/api/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package api

import (
"context"
"fmt"

"connectrpc.com/connect"

"github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/platform/v1"
)

Expand All @@ -18,11 +16,12 @@ func (p PlatformClient) CreateNamespace(ctx context.Context, name string) error
}

if response.Msg == nil {
return fmt.Errorf("failed to create namespace: %s, the server response is nil", name)
return ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to create namespace: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}

return err
Expand All @@ -39,13 +38,15 @@ func (p PlatformClient) RenameNamespace(ctx context.Context, oldName, newName st
}

if response.Msg == nil {
return fmt.Errorf("failed to rename the namespace: %s, the server response is nil", oldName)
return ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to rename the namespace: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}
return err

return nil
}

func (p PlatformClient) DeleteNamespace(ctx context.Context, name string) error {
Expand All @@ -56,13 +57,15 @@ func (p PlatformClient) DeleteNamespace(ctx context.Context, name string) error
}

if response.Msg == nil {
return fmt.Errorf("failed to delete namespace: %s, the server response is nil", name)
return ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to delete namespace: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}
return err

return nil
}

func (p PlatformClient) ListNamespaces(ctx context.Context) ([]*platformv1.Namespace, error) {
Expand All @@ -73,11 +76,12 @@ func (p PlatformClient) ListNamespaces(ctx context.Context) ([]*platformv1.Names
}

if response.Msg == nil {
return nil, fmt.Errorf("failed to list namespaces, the server response is nil")
return nil, ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return nil, fmt.Errorf("failed to list namespaces: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return nil, err
}

return response.Msg.Namespaces, nil
Expand Down
41 changes: 20 additions & 21 deletions internal/api/subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package api

import (
"context"
"fmt"

"connectrpc.com/connect"
"github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/platform/v1"
)

Expand All @@ -29,11 +27,12 @@ func (p PlatformClient) CreateSubgraph(ctx context.Context, name string, namespa
}

if response.Msg == nil {
return fmt.Errorf("failed to create subgraph: %s, the server response is nil", name)
return ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to create subgraph: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}

return nil
Expand All @@ -59,7 +58,12 @@ func (p PlatformClient) UpdateSubgraph(ctx context.Context, name, namespace, rou
}

if response.Msg == nil {
return fmt.Errorf("failed to update subgraph: %s, the server response is nil", name)
return ErrEmptyMsg
}

err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}

return nil
Expand All @@ -76,11 +80,12 @@ func (p PlatformClient) DeleteSubgraph(ctx context.Context, name, namespace stri
}

if response.Msg == nil {
return fmt.Errorf("failed to delete subgraph: %s, the server response is nil", name)
return ErrEmptyMsg
}

if response.Msg.GetResponse().Code != common.EnumStatusCode_OK {
return fmt.Errorf("failed to delete subgraph: %s", response.Msg)
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return err
}

return nil
Expand All @@ -97,21 +102,15 @@ func (p PlatformClient) GetSubgraph(ctx context.Context, name, namespace string)
}

if response.Msg == nil {
return nil, fmt.Errorf("failed to get subgraph: %s, the server response is nil", name)
}

if response.Msg.Graph == nil {
return nil, fmt.Errorf("failed to get subgraph: %s", name)
return nil, ErrEmptyMsg
}

subgraph := &platformv1.Subgraph{
Id: response.Msg.Graph.Id,
Name: response.Msg.Graph.Name,
Namespace: response.Msg.Graph.Namespace,
RoutingURL: response.Msg.Graph.RoutingURL,
err = handleErrorCodes(response.Msg.GetResponse().Code)
if err != nil {
return nil, err
}

return subgraph, nil
return response.Msg.GetGraph(), nil
}

func (p PlatformClient) PublishSubgraph(ctx context.Context, name, namespace, schema string) (*platformv1.PublishFederatedSubgraphResponse, error) {
Expand All @@ -126,7 +125,7 @@ func (p PlatformClient) PublishSubgraph(ctx context.Context, name, namespace, sc
}

if response.Msg == nil {
return nil, fmt.Errorf("failed to publish subgraph: %s, the server response is nil", name)
return nil, ErrEmptyMsg
}

return response.Msg, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ func (r *FederatedGraphResource) Read(ctx context.Context, req resource.ReadRequ

apiResponse, err := r.client.GetFederatedGraph(ctx, data.Name.ValueString(), data.Namespace.ValueString())
if err != nil {
utils.AddDiagnosticError(resp, ErrReadingGraph, fmt.Sprintf("Could not read federated graph: %s, graph name: %s, graph namespace: %s", err, data.Name.ValueString(), data.Namespace.ValueString()))
if api.IsNotFoundError(err) {
utils.AddDiagnosticWarning(resp, "Graph not found", fmt.Sprintf("Graph '%s' not found will be recreated", data.Name.ValueString()))
resp.State.RemoveResource(ctx)
return
}
utils.AddDiagnosticError(resp, ErrReadingGraph, fmt.Sprintf("Could not fetch subgraph '%s': %s", data.Name.ValueString(), err))
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestAccFederatedGraphResourceInvalidConfig(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccFederatedGraphResourceConfig(name, namespace, "invalid-url", ""),
ExpectError: regexp.MustCompile(`.*URL is not a valid URL*`),
ExpectError: regexp.MustCompile(`.*failed to create resource*`),
},
},
})
Expand Down
5 changes: 5 additions & 0 deletions internal/service/monograph/resource_cosmo_monograph.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ func (r *MonographResource) Read(ctx context.Context, req resource.ReadRequest,

monograph, err := r.client.GetMonograph(ctx, data.Name.ValueString(), data.Namespace.ValueString())
if err != nil {
if api.IsNotFoundError(err) {
utils.AddDiagnosticWarning(resp, "Monograph not found", fmt.Sprintf("Monograph '%s' not found will be recreated", data.Name.ValueString()))
resp.State.RemoveResource(ctx)
return
}
utils.AddDiagnosticError(resp, ErrReadingMonograph, fmt.Sprintf("Could not read monograph: %s, name: %s, namespace: %s", err, data.Name.ValueString(), data.Namespace.ValueString()))
return
}
Expand Down
Loading

0 comments on commit 44191ac

Please sign in to comment.