Forge

Errors

Shared error values across backends.

GitHub

Every backend translates native driver errors into shared values in the dbswitch package, so your domain code doesn't depend on a specific driver. Compare them with errors.Is / errors.As.

Not found

FindOne returns dbswitch.ErrNotFound when no row matches:

_, err := db.FindOne(ctx, "users", map[string]any{"email": "nope@x.com"})
if errors.Is(err, dbswitch.ErrNotFound) {
	// handle missing row
}

Duplicate (unique violation)

A unique-constraint violation is reported as dbswitch.ErrDuplicate. Use errors.As to recover the constraint name and map it to your own meaning:

err := db.Insert(ctx, "users", map[string]any{"email": "a@b.com"})
if errors.Is(err, dbswitch.ErrDuplicate) {
	var dup *dbswitch.DuplicateError
	if errors.As(err, &dup) {
		// dup.Constraint == "users_email_key" (Postgres), "email_1" (Mongo),
		// or "users.id" (DynamoDB — only the primary key can be duplicate-checked)
	}
}

Reference

ValueTypeMeaning
dbswitch.ErrNotFounderrorNo rows matched
dbswitch.ErrDuplicateerrorUnique-constraint violation (sentinel)
dbswitch.DuplicateErrorstruct{ Constraint string }Concrete duplicate error; errors.Is(err, ErrDuplicate) is true, and it carries the constraint name

On this page