adjust error handling to make more explicit

This commit is contained in:
Ahrav Dutta 2024-10-28 11:12:53 -07:00
parent c6aa491201
commit 240039a9b8
4 changed files with 42 additions and 61 deletions

View file

@ -31,41 +31,35 @@ func (h *arHandler) HandleFile(ctx logContext.Context, input fileReader) (chan [
} }
go func() { go func() {
ctx, cancel := logContext.WithTimeout(ctx, maxTimeout)
defer cancel()
defer close(archiveChan) 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 a panic recovery to handle any panics that occur during the AR processing.
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
// Return the panic as an error. var panicErr error
if e, ok := r.(error); ok { if e, ok := r.(error); ok {
err = e panicErr = e
} else { } 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 start := time.Now()
arReader, err = deb.LoadAr(input) arReader, err := deb.LoadAr(input)
if err != nil { if err != nil {
ctx.Logger().Error(err, "error reading AR") ctx.Logger().Error(err, "Error loading AR file")
return return
} }
if err = h.processARFiles(ctx, arReader, archiveChan); err != nil { err = h.processARFiles(ctx, arReader, archiveChan)
ctx.Logger().Error(err, "error processing AR files") if err == nil {
h.metrics.incFilesProcessed()
} }
// Update the metrics for the file processing and handle any errors.
h.measureLatencyAndHandleErrors(start, err)
}() }()
return archiveChan, nil return archiveChan, nil

View file

@ -53,7 +53,6 @@ func (h *archiveHandler) HandleFile(ctx logContext.Context, input fileReader) (c
} }
go func() { go func() {
var err error
defer close(dataChan) defer close(dataChan)
// The underlying 7zip library may panic when attempting to open an archive. // The underlying 7zip library may panic when attempting to open an archive.
@ -61,26 +60,24 @@ func (h *archiveHandler) HandleFile(ctx logContext.Context, input fileReader) (c
// See: https://github.com/bodgit/sevenzip/blob/74bff0da9b233317e4ea7dd8c184a315db71af2a/types.go#L846 // See: https://github.com/bodgit/sevenzip/blob/74bff0da9b233317e4ea7dd8c184a315db71af2a/types.go#L846
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
// Return the panic as an error. var panicErr error
if e, ok := r.(error); ok { if e, ok := r.(error); ok {
err = e panicErr = e
} else { } 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() start := time.Now()
defer func() { err := h.openArchive(ctx, 0, input, dataChan)
h.measureLatencyAndHandleErrors(start, err) if err == nil {
h.metrics.incFilesProcessed() 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 return dataChan, nil

View file

@ -37,17 +37,14 @@ func (h *defaultHandler) HandleFile(ctx logContext.Context, input fileReader) (c
go func() { go func() {
defer close(dataChan) defer close(dataChan)
// Update the metrics for the file processing.
start := time.Now() start := time.Now()
var err error err := h.handleNonArchiveContent(ctx, newMimeTypeReaderFromFileReader(input), dataChan)
defer func() { if err == nil {
h.measureLatencyAndHandleErrors(start, err)
h.metrics.incFilesProcessed() 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 return dataChan, nil

View file

@ -31,48 +31,41 @@ func (h *rpmHandler) HandleFile(ctx logContext.Context, input fileReader) (chan
} }
go func() { go func() {
ctx, cancel := logContext.WithTimeout(ctx, maxTimeout)
defer cancel()
defer close(archiveChan) 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 a panic recovery to handle any panics that occur during the RPM processing.
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
// Return the panic as an error. var panicErr error
if e, ok := r.(error); ok { if e, ok := r.(error); ok {
err = e panicErr = e
} else { } 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 start := time.Now()
rpm, err = rpmutils.ReadRpm(input) rpm, err := rpmutils.ReadRpm(input)
if err != nil { if err != nil {
ctx.Logger().Error(err, "error reading RPM") ctx.Logger().Error(err, "Error reading rpm file")
return return
} }
var reader rpmutils.PayloadReader reader, err := rpm.PayloadReaderExtended()
reader, err = rpm.PayloadReaderExtended()
if err != nil { if err != nil {
ctx.Logger().Error(err, "error getting RPM payload reader") ctx.Logger().Error(err, "Error reading rpm file")
return return
} }
if err = h.processRPMFiles(ctx, reader, archiveChan); err != nil { err = h.processRPMFiles(ctx, reader, archiveChan)
ctx.Logger().Error(err, "error processing RPM files") if err == nil {
h.metrics.incFilesProcessed()
} }
// Update the metrics for the file processing and handle any errors.
h.measureLatencyAndHandleErrors(start, err)
}() }()
return archiveChan, nil return archiveChan, nil