]> git.giacomolli.uk - onechat/blobdiff - journal.go
Select notification targets by stable ID
[onechat] / journal.go
index 745ae790089ec1f5a9bf98debe7e17d212537859..d048913b6e6c069b4c71a4b46b47087cd7d6dfd5 100644 (file)
@@ -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) {