Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrap errors for database repositories #829

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions internal/store/postgres/audit_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func (a AuditRepository) Create(ctx context.Context, l *audit.Log) error {

marshaledActor, err := json.Marshal(l.Actor)
if err != nil {
return fmt.Errorf("%s: %w", err, parseErr)
return fmt.Errorf("%w: %w", err, parseErr)
}
marshaledTarget, err := json.Marshal(l.Target)
if err != nil {
return fmt.Errorf("%s: %w", err, parseErr)
return fmt.Errorf("%w: %w", err, parseErr)
}
marshaledMetadata, err := json.Marshal(l.Metadata)
if err != nil {
return fmt.Errorf("%s: %w", err, parseErr)
return fmt.Errorf("%w: %w", err, parseErr)
}

query, params, err := dialect.Insert(TABLE_AUDITLOGS).Rows(
Expand All @@ -60,7 +60,7 @@ func (a AuditRepository) Create(ctx context.Context, l *audit.Log) error {
"metadata": marshaledMetadata,
}).Returning(&Audit{}).ToSQL()
if err != nil {
return fmt.Errorf("%s: %w", err, queryErr)
return fmt.Errorf("%w: %w", err, queryErr)
}

var auditModel Audit
Expand Down Expand Up @@ -95,7 +95,7 @@ func (a AuditRepository) List(ctx context.Context, flt audit.Filter) ([]audit.Lo

query, params, err := sqlStatement.Order(goqu.C("created_at").Desc()).ToSQL()
if err != nil {
return nil, fmt.Errorf("%w: %s", queryErr, err)
return nil, fmt.Errorf("%w: %w", queryErr, err)
}

var fetched []Audit
Expand All @@ -107,7 +107,7 @@ func (a AuditRepository) List(ctx context.Context, flt audit.Filter) ([]audit.Lo
case errors.Is(err, sql.ErrNoRows):
return nil, nil
default:
return nil, fmt.Errorf("%w: %s", dbErr, err)
return nil, fmt.Errorf("%w: %w", dbErr, err)
}
}

Expand All @@ -126,7 +126,7 @@ func (a AuditRepository) List(ctx context.Context, flt audit.Filter) ([]audit.Lo
for _, v := range fetched {
transformedGroup, err := v.transform()
if err != nil {
return nil, fmt.Errorf("%w: %s", parseErr, err)
return nil, fmt.Errorf("%w: %w", parseErr, err)
}
transformedLogs = append(transformedLogs, transformedGroup)
}
Expand All @@ -144,7 +144,7 @@ func (a AuditRepository) GetByID(ctx context.Context, id string) (audit.Log, err
"id": id,
}).Where(notDisabledGroupExp).ToSQL()
if err != nil {
return audit.Log{}, fmt.Errorf("%w: %s", queryErr, err)
return audit.Log{}, fmt.Errorf("%w: %w", queryErr, err)
}

var logModel Audit
Expand Down
2 changes: 1 addition & 1 deletion internal/store/postgres/billing_checkout_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (r BillingCheckoutRepository) UpdateByID(ctx context.Context, toUpdate chec
case errors.Is(err, ErrInvalidTextRepresentation):
return checkout.Checkout{}, checkout.ErrInvalidUUID
default:
return checkout.Checkout{}, fmt.Errorf("%s: %w", txnErr, err)
return checkout.Checkout{}, fmt.Errorf("%w: %w", txnErr, err)
}
}

Expand Down
26 changes: 13 additions & 13 deletions internal/store/postgres/billing_customer_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ func (r BillingCustomerRepository) Create(ctx context.Context, toCreate customer
"metadata": marshaledMetadata,
}).Returning(&Customer{}).ToSQL()
if err != nil {
return customer.Customer{}, fmt.Errorf("%w: %s", parseErr, err)
return customer.Customer{}, fmt.Errorf("%w: %w", parseErr, err)
}

var customerModel Customer
if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_CUSTOMERS, "Create", func(ctx context.Context) error {
return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&customerModel)
}); err != nil {
return customer.Customer{}, fmt.Errorf("%w: %s", dbErr, err)
return customer.Customer{}, fmt.Errorf("%w: %w", dbErr, err)
}

return customerModel.transform()
Expand All @@ -168,7 +168,7 @@ func (r BillingCustomerRepository) GetByID(ctx context.Context, id string) (cust
})
query, params, err := stmt.ToSQL()
if err != nil {
return customer.Customer{}, fmt.Errorf("%w: %s", parseErr, err)
return customer.Customer{}, fmt.Errorf("%w: %w", parseErr, err)
}

var customerModel Customer
Expand All @@ -180,7 +180,7 @@ func (r BillingCustomerRepository) GetByID(ctx context.Context, id string) (cust
case errors.Is(err, sql.ErrNoRows):
return customer.Customer{}, customer.ErrNotFound
}
return customer.Customer{}, fmt.Errorf("%w: %s", dbErr, err)
return customer.Customer{}, fmt.Errorf("%w: %w", dbErr, err)
}

return customerModel.transform()
Expand Down Expand Up @@ -211,7 +211,7 @@ func (r BillingCustomerRepository) List(ctx context.Context, flt customer.Filter
}
query, params, err := stmt.ToSQL()
if err != nil {
return nil, fmt.Errorf("%w: %s", parseErr, err)
return nil, fmt.Errorf("%w: %w", parseErr, err)
}

var customerModels []Customer
Expand All @@ -221,7 +221,7 @@ func (r BillingCustomerRepository) List(ctx context.Context, flt customer.Filter
if errors.Is(err, sql.ErrNoRows) {
return []customer.Customer{}, nil
}
return nil, fmt.Errorf("%w: %s", dbErr, err)
return nil, fmt.Errorf("%w: %w", dbErr, err)
}

customers := make([]customer.Customer, 0, len(customerModels))
Expand All @@ -248,7 +248,7 @@ func (r BillingCustomerRepository) UpdateByID(ctx context.Context, toUpdate cust
}
marshaledMetadata, err := json.Marshal(toUpdate.Metadata)
if err != nil {
return customer.Customer{}, fmt.Errorf("%w: %s", parseErr, err)
return customer.Customer{}, fmt.Errorf("%w: %w", parseErr, err)
}
updateRecord := goqu.Record{
"name": toUpdate.Name,
Expand All @@ -273,7 +273,7 @@ func (r BillingCustomerRepository) UpdateByID(ctx context.Context, toUpdate cust
"id": toUpdate.ID,
}).Returning(&Customer{}).ToSQL()
if err != nil {
return customer.Customer{}, fmt.Errorf("%w: %s", queryErr, err)
return customer.Customer{}, fmt.Errorf("%w: %w", queryErr, err)
}

var customerModel Customer
Expand All @@ -287,7 +287,7 @@ func (r BillingCustomerRepository) UpdateByID(ctx context.Context, toUpdate cust
case errors.Is(err, ErrInvalidTextRepresentation):
return customer.Customer{}, customer.ErrInvalidUUID
default:
return customer.Customer{}, fmt.Errorf("%s: %w", txnErr, err)
return customer.Customer{}, fmt.Errorf("%w: %w", txnErr, err)
}
}

Expand All @@ -306,7 +306,7 @@ func (r BillingCustomerRepository) UpdateCreditMinByID(ctx context.Context, cust
"id": customerID,
}).Returning(&Customer{}).ToSQL()
if err != nil {
return customer.Customer{}, fmt.Errorf("%w: %s", queryErr, err)
return customer.Customer{}, fmt.Errorf("%w: %w", queryErr, err)
}

var customerModel Customer
Expand All @@ -320,7 +320,7 @@ func (r BillingCustomerRepository) UpdateCreditMinByID(ctx context.Context, cust
case errors.Is(err, ErrInvalidTextRepresentation):
return customer.Customer{}, customer.ErrInvalidUUID
default:
return customer.Customer{}, fmt.Errorf("%s: %w", txnErr, err)
return customer.Customer{}, fmt.Errorf("%w: %w", txnErr, err)
}
}

Expand All @@ -333,7 +333,7 @@ func (r BillingCustomerRepository) Delete(ctx context.Context, id string) error
})
query, params, err := stmt.ToSQL()
if err != nil {
return fmt.Errorf("%w: %s", parseErr, err)
return fmt.Errorf("%w: %w", parseErr, err)
}

if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_CUSTOMERS, "Delete", func(ctx context.Context) error {
Expand All @@ -345,7 +345,7 @@ func (r BillingCustomerRepository) Delete(ctx context.Context, id string) error
case errors.Is(err, sql.ErrNoRows):
return customer.ErrNotFound
default:
return fmt.Errorf("%s: %w", txnErr, err)
return fmt.Errorf("%w: %w", txnErr, err)
}
}

Expand Down
18 changes: 9 additions & 9 deletions internal/store/postgres/billing_feature_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func (r BillingFeatureRepository) Create(ctx context.Context, toCreate product.F
"updated_at": goqu.L("now()"),
}).Returning(&Feature{}).ToSQL()
if err != nil {
return product.Feature{}, fmt.Errorf("%w: %s", parseErr, err)
return product.Feature{}, fmt.Errorf("%w: %w", parseErr, err)
}

var featureModel Feature
if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_FEATURES, "Create", func(ctx context.Context) error {
return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&featureModel)
}); err != nil {
return product.Feature{}, fmt.Errorf("%w: %s", dbErr, err)
return product.Feature{}, fmt.Errorf("%w: %w", dbErr, err)
}

return featureModel.transform()
Expand All @@ -102,7 +102,7 @@ func (r BillingFeatureRepository) GetByID(ctx context.Context, id string) (produ
})
query, params, err := stmt.ToSQL()
if err != nil {
return product.Feature{}, fmt.Errorf("%w: %s", parseErr, err)
return product.Feature{}, fmt.Errorf("%w: %w", parseErr, err)
}

var featureModel Feature
Expand All @@ -114,7 +114,7 @@ func (r BillingFeatureRepository) GetByID(ctx context.Context, id string) (produ
case errors.Is(err, sql.ErrNoRows):
return product.Feature{}, product.ErrFeatureNotFound
}
return product.Feature{}, fmt.Errorf("%w: %s", dbErr, err)
return product.Feature{}, fmt.Errorf("%w: %w", dbErr, err)
}

return featureModel.transform()
Expand All @@ -126,7 +126,7 @@ func (r BillingFeatureRepository) GetByName(ctx context.Context, name string) (p
})
query, params, err := stmt.ToSQL()
if err != nil {
return product.Feature{}, fmt.Errorf("%w: %s", parseErr, err)
return product.Feature{}, fmt.Errorf("%w: %w", parseErr, err)
}

var featureModel Feature
Expand All @@ -138,7 +138,7 @@ func (r BillingFeatureRepository) GetByName(ctx context.Context, name string) (p
case errors.Is(err, sql.ErrNoRows):
return product.Feature{}, product.ErrFeatureNotFound
}
return product.Feature{}, fmt.Errorf("%w: %s", dbErr, err)
return product.Feature{}, fmt.Errorf("%w: %w", dbErr, err)
}

return featureModel.transform()
Expand All @@ -154,14 +154,14 @@ func (r BillingFeatureRepository) List(ctx context.Context, flt product.Filter)
}
query, params, err := stmt.ToSQL()
if err != nil {
return nil, fmt.Errorf("%w: %s", parseErr, err)
return nil, fmt.Errorf("%w: %w", parseErr, err)
}

var featureModels []Feature
if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_FEATURES, "List", func(ctx context.Context) error {
return r.dbc.SelectContext(ctx, &featureModels, query, params...)
}); err != nil {
return nil, fmt.Errorf("%w: %s", dbErr, err)
return nil, fmt.Errorf("%w: %w", dbErr, err)
}

features := make([]product.Feature, 0, len(featureModels))
Expand Down Expand Up @@ -209,7 +209,7 @@ func (r BillingFeatureRepository) UpdateByName(ctx context.Context, toUpdate pro
case errors.Is(err, sql.ErrNoRows):
return product.Feature{}, product.ErrFeatureNotFound
default:
return product.Feature{}, fmt.Errorf("%s: %w", txnErr, err)
return product.Feature{}, fmt.Errorf("%w: %w", txnErr, err)
}
}

Expand Down
18 changes: 9 additions & 9 deletions internal/store/postgres/billing_invoice_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ func (r BillingInvoiceRepository) Create(ctx context.Context, toCreate invoice.I
"updated_at": goqu.L("now()"),
}).Returning(&Invoice{}).ToSQL()
if err != nil {
return invoice.Invoice{}, fmt.Errorf("%w: %s", parseErr, err)
return invoice.Invoice{}, fmt.Errorf("%w: %w", parseErr, err)
}

var invoiceModel Invoice
if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_INVOICES, "Create", func(ctx context.Context) error {
return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&invoiceModel)
}); err != nil {
return invoice.Invoice{}, fmt.Errorf("%w: %s", dbErr, err)
return invoice.Invoice{}, fmt.Errorf("%w: %w", dbErr, err)
}

return invoiceModel.transform()
Expand All @@ -163,7 +163,7 @@ func (r BillingInvoiceRepository) GetByID(ctx context.Context, id string) (invoi
})
query, params, err := stmt.ToSQL()
if err != nil {
return invoice.Invoice{}, fmt.Errorf("%w: %s", parseErr, err)
return invoice.Invoice{}, fmt.Errorf("%w: %w", parseErr, err)
}

var invoiceModel Invoice
Expand All @@ -175,7 +175,7 @@ func (r BillingInvoiceRepository) GetByID(ctx context.Context, id string) (invoi
case errors.Is(err, sql.ErrNoRows):
return invoice.Invoice{}, invoice.ErrNotFound
}
return invoice.Invoice{}, fmt.Errorf("%w: %s", dbErr, err)
return invoice.Invoice{}, fmt.Errorf("%w: %w", dbErr, err)
}

return invoiceModel.transform()
Expand Down Expand Up @@ -208,14 +208,14 @@ func (r BillingInvoiceRepository) List(ctx context.Context, flt invoice.Filter)
totalCountQuery, _, err := totalCountStmt.ToSQL()

if err != nil {
return []invoice.Invoice{}, fmt.Errorf("%w: %s", queryErr, err)
return []invoice.Invoice{}, fmt.Errorf("%w: %w", queryErr, err)
}

var totalCount int32
if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_INVOICES, "Count", func(ctx context.Context) error {
return r.dbc.GetContext(ctx, &totalCount, totalCountQuery)
}); err != nil {
return nil, fmt.Errorf("%w: %s", dbErr, err)
return nil, fmt.Errorf("%w: %w", dbErr, err)
}

flt.Pagination.SetCount(totalCount)
Expand All @@ -225,7 +225,7 @@ func (r BillingInvoiceRepository) List(ctx context.Context, flt invoice.Filter)
stmt = stmt.Order(goqu.I("created_at").Desc())
query, params, err := stmt.ToSQL()
if err != nil {
return nil, fmt.Errorf("%w: %s", parseErr, err)
return nil, fmt.Errorf("%w: %w", parseErr, err)
}

var invoiceModels []Invoice
Expand Down Expand Up @@ -287,7 +287,7 @@ func (r BillingInvoiceRepository) UpdateByID(ctx context.Context, toUpdate invoi
case errors.Is(err, sql.ErrNoRows):
return invoice.Invoice{}, invoice.ErrNotFound
default:
return invoice.Invoice{}, fmt.Errorf("%s: %w", txnErr, err)
return invoice.Invoice{}, fmt.Errorf("%w: %w", txnErr, err)
}
}

Expand All @@ -306,7 +306,7 @@ func (r BillingInvoiceRepository) Delete(ctx context.Context, id string) error {
_, err := r.dbc.ExecContext(ctx, query, params...)
return err
}); err != nil {
return fmt.Errorf("%s: %w", txnErr, err)
return fmt.Errorf("%w: %w", txnErr, err)
}
return nil
}
Loading
Loading