Skip to content

Commit

Permalink
Merge pull request #1138 from sundowndev/feat/remote-panic
Browse files Browse the repository at this point in the history
Handle panics in scanners
  • Loading branch information
sundowndev authored Sep 19, 2022
2 parents b310548 + 87c2821 commit 0d93557
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
4 changes: 0 additions & 4 deletions lib/output/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ func (o *ConsoleOutput) displayResult(val interface{}, prefix string) {

field, ok := reflectType.Field(i).Tag.Lookup("console")
if !ok || field == "-" {
logrus.WithFields(map[string]interface{}{
"found": ok,
"value": field,
}).Debug("Console field was ignored")
continue
}

Expand Down
25 changes: 16 additions & 9 deletions lib/remote/remote.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package remote

import (
"errors"
"github.com/sirupsen/logrus"
"github.com/sundowndev/phoneinfoga/v2/lib/filter"
"github.com/sundowndev/phoneinfoga/v2/lib/number"
Expand Down Expand Up @@ -59,18 +60,24 @@ func (r *Library) Scan(n *number.Number) (map[string]interface{}, map[string]err
continue
}

if err := s.DryRun(*n); err != nil {
logrus.
WithField("scanner", s.Name()).
WithField("reason", err.Error()).
Debug("Scanner was ignored because it should not run")
continue
}

wg.Add(1)

go func(s Scanner) {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
logrus.WithField("scanner", s.Name()).WithField("error", err).Debug("Scanner panicked")
r.addError(s.Name(), errors.New("panic occurred while running scan, see debug logs"))
}
}()

if err := s.DryRun(*n); err != nil {
logrus.
WithField("scanner", s.Name()).
WithField("reason", err.Error()).
Debug("Scanner was ignored because it should not run")
return
}

data, err := s.Run(*n)
if err != nil {
r.addError(s.Name(), err)
Expand Down
49 changes: 46 additions & 3 deletions lib/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
)

func TestRemoteLibrarySuccessScan(t *testing.T) {
func TestRemoteLibrary_SuccessScan(t *testing.T) {
type fakeScannerResponse struct {
Valid bool
}
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestRemoteLibrarySuccessScan(t *testing.T) {
fakeScanner2.AssertExpectations(t)
}

func TestRemoteLibraryFailedScan(t *testing.T) {
func TestRemoteLibrary_FailedScan(t *testing.T) {
num, err := number.NewNumber("15556661212")
if err != nil {
t.Fatal(err)
Expand All @@ -71,7 +71,7 @@ func TestRemoteLibraryFailedScan(t *testing.T) {
fakeScanner.AssertExpectations(t)
}

func TestRemoteLibraryEmptyScan(t *testing.T) {
func TestRemoteLibrary_EmptyScan(t *testing.T) {
num, err := number.NewNumber("15556661212")
if err != nil {
t.Fatal(err)
Expand All @@ -92,6 +92,49 @@ func TestRemoteLibraryEmptyScan(t *testing.T) {
fakeScanner.AssertExpectations(t)
}

func TestRemoteLibrary_PanicRun(t *testing.T) {
num, err := number.NewNumber("15556661212")
if err != nil {
t.Fatal(err)
}

fakeScanner := &mocks.Scanner{}
fakeScanner.On("Name").Return("fake")
fakeScanner.On("DryRun", *num).Return(nil).Once()
fakeScanner.On("Run", *num).Panic("dummy panic").Once()

lib := NewLibrary(filter.NewEngine())

lib.AddScanner(fakeScanner)

result, errs := lib.Scan(num)
assert.Equal(t, map[string]interface{}{}, result)
assert.Equal(t, map[string]error{"fake": errors.New("panic occurred while running scan, see debug logs")}, errs)

fakeScanner.AssertExpectations(t)
}

func TestRemoteLibrary_PanicDryRun(t *testing.T) {
num, err := number.NewNumber("15556661212")
if err != nil {
t.Fatal(err)
}

fakeScanner := &mocks.Scanner{}
fakeScanner.On("Name").Return("fake")
fakeScanner.On("DryRun", *num).Panic("dummy panic").Once()

lib := NewLibrary(filter.NewEngine())

lib.AddScanner(fakeScanner)

result, errs := lib.Scan(num)
assert.Equal(t, map[string]interface{}{}, result)
assert.Equal(t, map[string]error{"fake": errors.New("panic occurred while running scan, see debug logs")}, errs)

fakeScanner.AssertExpectations(t)
}

func TestRemoteLibrary_GetAllScanners(t *testing.T) {
fakeScanner := &mocks.Scanner{}
fakeScanner2 := &mocks.Scanner{}
Expand Down
6 changes: 6 additions & 0 deletions logs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logs
import (
"github.com/sirupsen/logrus"
"github.com/sundowndev/phoneinfoga/v2/build"
"os"
)

type Config struct {
Expand All @@ -20,5 +21,10 @@ func getConfig() Config {
config.Level = logrus.DebugLevel
}

if lvl := os.Getenv("LOG_LEVEL"); lvl != "" {
loglevel, _ := logrus.ParseLevel(lvl)
config.Level = loglevel
}

return config
}

0 comments on commit 0d93557

Please sign in to comment.