Skip to content

Commit

Permalink
[refactor] - Adjust File Handling Errors (#3519)
Browse files Browse the repository at this point in the history
* adjust error handling to make more explicit

* address comments
  • Loading branch information
ahrav authored Nov 15, 2024
1 parent c6abe85 commit af3e682
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 61 deletions.
32 changes: 13 additions & 19 deletions pkg/handlers/ar.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,35 @@ func (h *arHandler) HandleFile(ctx logContext.Context, input fileReader) (chan [
}

go func() {
ctx, cancel := logContext.WithTimeout(ctx, maxTimeout)
defer cancel()
defer close(archiveChan)

// Update the metrics for the file processing.
start := time.Now()
var err error
defer func() {
h.measureLatencyAndHandleErrors(start, err)
h.metrics.incFilesProcessed()
}()

// Defer a panic recovery to handle any panics that occur during the AR processing.
defer func() {
if r := recover(); r != nil {
// Return the panic as an error.
var panicErr error
if e, ok := r.(error); ok {
err = e
panicErr = e
} else {
err = fmt.Errorf("panic occurred: %v", r)
panicErr = fmt.Errorf("panic occurred: %v", r)
}
ctx.Logger().Error(err, "Panic occurred when reading ar archive")
ctx.Logger().Error(panicErr, "Panic occurred when attempting to open ar archive")
}
}()

var arReader *deb.Ar
arReader, err = deb.LoadAr(input)
start := time.Now()
arReader, err := deb.LoadAr(input)
if err != nil {
ctx.Logger().Error(err, "error reading AR")
ctx.Logger().Error(err, "Error loading AR file")
return
}

if err = h.processARFiles(ctx, arReader, archiveChan); err != nil {
ctx.Logger().Error(err, "error processing AR files")
err = h.processARFiles(ctx, arReader, archiveChan)
if err == nil {
h.metrics.incFilesProcessed()
}

// Update the metrics for the file processing and handle any errors.
h.measureLatencyAndHandleErrors(start, err)
}()

return archiveChan, nil
Expand Down
21 changes: 9 additions & 12 deletions pkg/handlers/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,31 @@ func (h *archiveHandler) HandleFile(ctx logContext.Context, input fileReader) (c
}

go func() {
var err error
defer close(dataChan)

// The underlying 7zip library may panic when attempting to open an archive.
// This is due to an Index Out Of Range (IOOR) error when reading the archive header.
// See: https://github.com/bodgit/sevenzip/blob/74bff0da9b233317e4ea7dd8c184a315db71af2a/types.go#L846
defer func() {
if r := recover(); r != nil {
// Return the panic as an error.
var panicErr error
if e, ok := r.(error); ok {
err = e
panicErr = e
} else {
err = fmt.Errorf("panic occurred: %v", r)
panicErr = fmt.Errorf("panic occurred: %v", r)
}
ctx.Logger().Error(err, "Panic occurred when attempting to open archive")
ctx.Logger().Error(panicErr, "Panic occurred when attempting to open archive")
}
}()

// Update the metrics for the file processing.
start := time.Now()
defer func() {
h.measureLatencyAndHandleErrors(start, err)
err := h.openArchive(ctx, 0, input, dataChan)
if err == nil {
h.metrics.incFilesProcessed()
}()

if err = h.openArchive(ctx, 0, input, dataChan); err != nil {
ctx.Logger().Error(err, "error unarchiving chunk.")
}

// Update the metrics for the file processing and handle any errors.
h.measureLatencyAndHandleErrors(start, err)
}()

return dataChan, nil
Expand Down
13 changes: 5 additions & 8 deletions pkg/handlers/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ func (h *defaultHandler) HandleFile(ctx logContext.Context, input fileReader) (c
go func() {
defer close(dataChan)

// Update the metrics for the file processing.
start := time.Now()
var err error
defer func() {
h.measureLatencyAndHandleErrors(start, err)
err := h.handleNonArchiveContent(ctx, newMimeTypeReaderFromFileReader(input), dataChan)
if err == nil {
h.metrics.incFilesProcessed()
}()

if err = h.handleNonArchiveContent(ctx, newMimeTypeReaderFromFileReader(input), dataChan); err != nil {
ctx.Logger().Error(err, "error handling non-archive content.")
}

// Update the metrics for the file processing and handle errors.
h.measureLatencyAndHandleErrors(start, err)
}()

return dataChan, nil
Expand Down
37 changes: 15 additions & 22 deletions pkg/handlers/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,41 @@ func (h *rpmHandler) HandleFile(ctx logContext.Context, input fileReader) (chan
}

go func() {
ctx, cancel := logContext.WithTimeout(ctx, maxTimeout)
defer cancel()
defer close(archiveChan)

// Update the metrics for the file processing.
start := time.Now()
var err error
defer func() {
h.measureLatencyAndHandleErrors(start, err)
h.metrics.incFilesProcessed()
}()

// Defer a panic recovery to handle any panics that occur during the RPM processing.
defer func() {
if r := recover(); r != nil {
// Return the panic as an error.
var panicErr error
if e, ok := r.(error); ok {
err = e
panicErr = e
} else {
err = fmt.Errorf("panic occurred: %v", r)
panicErr = fmt.Errorf("panic occurred: %v", r)
}
ctx.Logger().Error(err, "Panic occurred when reading rpm archive")
ctx.Logger().Error(panicErr, "Panic occurred when attempting to open rpm archive")
}
}()

var rpm *rpmutils.Rpm
rpm, err = rpmutils.ReadRpm(input)
start := time.Now()
rpm, err := rpmutils.ReadRpm(input)
if err != nil {
ctx.Logger().Error(err, "error reading RPM")
ctx.Logger().Error(err, "error reading rpm file")
return
}

var reader rpmutils.PayloadReader
reader, err = rpm.PayloadReaderExtended()
reader, err := rpm.PayloadReaderExtended()
if err != nil {
ctx.Logger().Error(err, "error getting RPM payload reader")
ctx.Logger().Error(err, "error reading rpm payload")
return
}

if err = h.processRPMFiles(ctx, reader, archiveChan); err != nil {
ctx.Logger().Error(err, "error processing RPM files")
err = h.processRPMFiles(ctx, reader, archiveChan)
if err == nil {
h.metrics.incFilesProcessed()
}

// Update the metrics for the file processing and handle any errors.
h.measureLatencyAndHandleErrors(start, err)
}()

return archiveChan, nil
Expand Down

0 comments on commit af3e682

Please sign in to comment.