X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/d8f3785bb69288663f4cc46bf4d609c6770452d5..8707a09fee94c97e29d668d24fb9713373913e0b:/journal.go?ds=sidebyside diff --git a/journal.go b/journal.go index 745ae79..d048913 100644 --- a/journal.go +++ b/journal.go @@ -38,13 +38,13 @@ type Journal struct { func (j *Journal) Write(day string, line []byte) (Position, error) { number, err := journalDayNumber(day) if err != nil { - return Position{}, err + return Position{}, fmt.Errorf("parse journal day: %w", err) } if number != j.day { err = j.closeWriter() if err != nil { - return Position{}, err + return Position{}, fmt.Errorf("close previous day writer: %w", err) } j.file, err = os.OpenFile(filepath.Join(j.dir, day+".jsonl"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600) @@ -150,7 +150,7 @@ func (j *Journal) Scan(ctx context.Context, place Position, add func([]byte, Pos for n, path := range paths { day, err := journalDayNumber(strings.TrimSuffix(filepath.Base(path), ".jsonl")) if err != nil { - return err + return fmt.Errorf("parse day filename: %w", err) } if day < place.day { @@ -164,7 +164,7 @@ func (j *Journal) Scan(ctx context.Context, place Position, add func([]byte, Pos err = journalScanFile(ctx, path, day, offset, n == len(paths)-1, add) if err != nil { - return err + return fmt.Errorf("scan day file: %w", err) } } @@ -215,7 +215,12 @@ func journalScanFile(ctx context.Context, path string, day uint32, offset uint64 } if errors.Is(err, io.EOF) && last { - return journalCutFile(path, offset) + err = journalCutFile(path, offset) + if err != nil { + return fmt.Errorf("cut incomplete day line: %w", err) + } + + return nil } if errors.Is(err, io.EOF) {