<commit_before>package llog

import "github.com/levenlabs/golib/errctx"

type errKVKey int

const errKV errKVKey = 1

// ErrWithKV embeds a KV into an error, returning a new error instance. If the
// error already has a KV embedded in it then the returned error will have a
// the merging of the two.
func ErrWithKV(err error, kv KV) error {
	existingKV := errctx.Get(err, errKV)
	if existingKV != nil {
		kv = Merge(existingKV.(KV), kv)
	}
	return errctx.Set(err, errKV, kv)
}

func getInnerKV(err error) KV {
	kvi := errctx.Get(err, errKV)
	if kvi == nil {
		return KV{}
	}
	return kvi.(KV)
}<commit_msg>make ErrWithKV be able to take in nil<commit_after>package llog

import "github.com/levenlabs/golib/errctx"

type errKVKey int

const errKV errKVKey = 1

// ErrWithKV embeds a KV into an error, returning a new error instance. If the
// error already has a KV embedded in it then the returned error will have a
// the merging of the two.
func ErrWithKV(err error, kv KV) error {
	if err == nil {
		return nil
	}
	existingKV := errctx.Get(err, errKV)
	if existingKV != nil {
		kv = Merge(existingKV.(KV), kv)
	}
	return errctx.Set(err, errKV, kv)
}

func getInnerKV(err error) KV {
	kvi := errctx.Get(err, errKV)
	if kvi == nil {
		return KV{}
	}
	return kvi.(KV)
}
{'fraction_non_alphanumeric': 0.10559495665878645, 'fraction_numerical': 0.0015760441292356187, 'mean_word_length': 3.601449275362319, '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': '6967756', 'n_tokens_mistral': 510, 'n_tokens_neox': 497, 'n_words': 168}