-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement error handler #105
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a8e2886
Add flag and system to report errors to a directory
MetalBlueberry 568a244
attempt to create output dir OR fail properly
MetalBlueberry 11b0d65
Merge branch 'vperez/use-options-pattern' into vperez/skip-invalid-rows
MetalBlueberry 4c6f916
try ErrorBatchHandler
MetalBlueberry bfe1afc
organise code
MetalBlueberry a2df567
check
MetalBlueberry 656bc2d
Merge branch 'vperez/use-options-pattern' into vperez/skip-invalid-rows
MetalBlueberry 628c2af
Merge remote-tracking branch 'origin/main' into vperez/skip-invalid-rows
MetalBlueberry eee8052
Improve location info
MetalBlueberry 033f12b
cleanup
MetalBlueberry 0ac2c44
lint
MetalBlueberry 599eef6
Apply suggestions from code review
MetalBlueberry acf7193
Update pkg/csvcopy/csvcopy_test.go
MetalBlueberry 4080115
Update pkg/csvcopy/csvcopy_test.go
MetalBlueberry 389144e
Rename
MetalBlueberry d4eafa6
rename
MetalBlueberry 69907bf
simplify
MetalBlueberry 41e3c44
consistent require/assert
MetalBlueberry a4e1245
Merge branch 'vperez/skip-invalid-rows' of github.com:timescale/times…
MetalBlueberry bb17609
cleanup test assertions
MetalBlueberry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package csvcopy | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/timescale/timescaledb-parallel-copy/pkg/batch" | ||
) | ||
|
||
// BatchHandlerSaveToFile saves the errors to the given directory using the batch start row as file name. | ||
func BatchHandlerSaveToFile(dir string, next BatchErrorHandler) BatchErrorHandler { | ||
return BatchErrorHandler(func(batch batch.Batch, reason error) error { | ||
err := os.MkdirAll(dir, os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf("failed to ensure directory exists: %w", err) | ||
} | ||
|
||
fileName := fmt.Sprintf("%d.csv", batch.Location.StartRow) | ||
AdrianLC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
path := filepath.Join(dir, fileName) | ||
|
||
dst, err := os.Create(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to create file to store batch error, %w", err) | ||
} | ||
defer dst.Close() | ||
|
||
batch.Rewind() | ||
_, err = io.Copy(dst, &batch.Data) | ||
if err != nil { | ||
return fmt.Errorf("failed to write file to store batch error, %w", err) | ||
} | ||
|
||
if next != nil { | ||
return next(batch, reason) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
// BatchHandlerLog prints a log line that reports the error in the given batch | ||
func BatchHandlerLog(log Logger, next BatchErrorHandler) BatchErrorHandler { | ||
return BatchErrorHandler(func(batch batch.Batch, reason error) error { | ||
log.Infof("Batch %d, starting at byte %d with len %d, has error: %s", batch.Location.StartRow, batch.Location.ByteOffset, batch.Location.ByteLen, reason.Error()) | ||
|
||
if next != nil { | ||
return next(batch, reason) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
// BatchHandlerNoop no operation | ||
func BatchHandlerNoop() BatchErrorHandler { | ||
return BatchErrorHandler(func(_ batch.Batch, _ error) error { return nil }) | ||
} | ||
|
||
// BatchHandlerError fails the process | ||
func BatchHandlerError() BatchErrorHandler { | ||
return BatchErrorHandler(func(_ batch.Batch, err error) error { return err }) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this could be extractor to an auxiliar function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far I think main is the best place to put this logic, moving this to a function won't make it much easier. as you need to pass all the flags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can have more functions directly in the main.go file so you would still have access to the flags since they're global. It's just a way to avoid an ever-growing main function that happens as programs become more complex