Skip to content

Commit

Permalink
Add JSON encoding, error detail
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhb committed Sep 16, 2024
1 parent c87957b commit 47c7ff5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
8 changes: 6 additions & 2 deletions pkg/replicator/pgreplicator/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ type InitializerOpts struct {
func NewInitializer(ctx context.Context, opts InitializerOpts) (replicator.SystemInitializer[InitializeResult], error) {
conn, err := pgx.ConnectConfig(ctx, &opts.AdminConfig)
if err != nil {
return nil, ErrInvalidCredentials
newErr := ErrInvalidCredentials
newErr.Data = map[string]any{"detail": err.Error()}
return nil, newErr
}
defer conn.Close(ctx)
if err := conn.Ping(ctx); err != nil {
return nil, ErrCannotCommunicate
newErr := ErrCannotCommunicate
newErr.Data = map[string]any{"detail": err.Error()}
return nil, newErr
}
return initializer[pgsetup.TestConnResult]{opts: opts}, nil
}
Expand Down
31 changes: 26 additions & 5 deletions pkg/replicator/replicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package replicator

import (
"context"
"encoding/json"

"github.com/inngest/dbcap/pkg/changeset"
)
Expand Down Expand Up @@ -47,15 +48,35 @@ type ConnectionResult interface {
Results() map[string]ConnectionStepResult
}

type ConnectionStepResult struct {
Error error `json:"error"`
Complete bool `json:"complete"`
}

type SystemInitializer[T ConnectionResult] interface {
// PerformInit perform setup for the replicator.
PerformInit(ctx context.Context) (T, error)

// CheckInit ensures that the setup for the replicator is complete.
CheckInit(ctx context.Context) (T, error)
}

type ConnectionStepResult struct {
Error error `json:"error"`
Complete bool `json:"complete"`
}

func (c ConnectionStepResult) MarshalJSON() ([]byte, error) {
res := map[string]any{"complete": c.Complete}

if c.Error == nil {
return json.Marshal(res)
}

if m, ok := c.Error.(json.Marshaler); ok {
byt, err := m.MarshalJSON()
if err != nil {
return nil, err
}
res["error"] = json.RawMessage(byt)
return json.Marshal(res)
}

res["error"] = c.Error.Error()
return json.Marshal(res)
}

0 comments on commit 47c7ff5

Please sign in to comment.