<commit_before>package main

import (
	"context"
	"errors"

	"cloud.google.com/go/datastore"
)

// Errors
var (
	ErrNoUserFound = errors.New("database: no user found")
)

type Database struct {
	ds *datastore.Client
}

func initDB() (*Database, error) {
	ctx := context.Background()

	client, err := datastore.NewClient(ctx, *googleProjectID)
	if err != nil {
		return nil, err
	}

	db := &Database{
		ds: client,
	}

	return db, err
}

func (d *Database) GetUser(ctx context.Context, id string) (*DiscordUser, *datastore.Key, error) {
	result := &DiscordUser{}
	var resultKey *datastore.Key

	q := datastore.NewQuery("DiscordUser").Filter("ID =", id).Limit(1)

	for t := d.ds.Run(ctx, q); ; {
		val := &DiscordUser{}

		key, err := t.Next(val)
		if err == datastore.Done {
			break
		}
		if err != nil {
			return nil, nil, err
		}

		result = val
		resultKey = key
	}

	if len(result.ID) == 0 {
		return nil, nil, ErrNoUserFound
	}

	return result, resultKey, nil
}

func (d *Database) PutUser(ctx context.Context, dUser *DiscordUser) (*datastore.Key, error) {
	_, _, err := d.GetUser(ctx, dUser.ID)
	if err != ErrNoUserFound {
		return nil, err
	}

	k := datastore.NewIncompleteKey(ctx, "DiscordUser", nil)
	return d.ds.Put(ctx, k, dUser)
}
<commit_msg>better handle database errors in PutUser<commit_after>package main

import (
	"context"
	"errors"

	"cloud.google.com/go/datastore"
)

// Errors
var (
	ErrNoUserFound = errors.New("database: no user found")
)

type Database struct {
	ds *datastore.Client
}

func initDB() (*Database, error) {
	ctx := context.Background()

	client, err := datastore.NewClient(ctx, *googleProjectID)
	if err != nil {
		return nil, err
	}

	db := &Database{
		ds: client,
	}

	return db, err
}

func (d *Database) GetUser(ctx context.Context, id string) (*DiscordUser, *datastore.Key, error) {
	result := &DiscordUser{}
	var resultKey *datastore.Key

	q := datastore.NewQuery("DiscordUser").Filter("ID =", id).Limit(1)

	for t := d.ds.Run(ctx, q); ; {
		val := &DiscordUser{}

		key, err := t.Next(val)
		if err == datastore.Done {
			break
		}
		if err != nil {
			return nil, nil, err
		}

		result = val
		resultKey = key
	}

	if len(result.ID) == 0 {
		return nil, nil, ErrNoUserFound
	}

	return result, resultKey, nil
}

func (d *Database) PutUser(ctx context.Context, dUser *DiscordUser) (*datastore.Key, error) {
	_, key, err := d.GetUser(ctx, dUser.ID)
	if err != nil && err != ErrNoUserFound {
		return nil, err
	}

	if key == nil {
		key = datastore.NewIncompleteKey(ctx, "DiscordUser", nil)
	}

	return d.ds.Put(ctx, key, dUser)
}

{'fraction_non_alphanumeric': 0.15310077519379844, 'fraction_numerical': 0.0015503875968992248, 'mean_word_length': 3.6420863309352516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '558223', 'n_tokens_mistral': 1041, 'n_tokens_neox': 981, 'n_words': 251}