Skip to content

Commit

Permalink
fix: propagate errors instead of panicking (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
obs-gh-irismcleary committed Jun 21, 2023
1 parent bbe34b8 commit 03b5d08
Show file tree
Hide file tree
Showing 26 changed files with 211 additions and 66 deletions.
10 changes: 10 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,13 @@ func SendRecords(ctx context.Context, ch chan<- *Record, name string, s Source)
}
return true
}

func FirstError(errors ...error) error {
for _, err := range errors {
if err != nil {
return err
}
}

return nil
}
14 changes: 11 additions & 3 deletions pkg/service/apigateway/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,34 @@ func (fn *GetDeployments) New(name string, config interface{}) ([]api.Request, e
}

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.GetRestApisPagesWithContext(ctx, &input, func(output *apigateway.GetRestApisOutput, last bool) bool {
var innerErr, outerErr error

outerErr = fn.GetRestApisPagesWithContext(ctx, &input, func(output *apigateway.GetRestApisOutput, last bool) bool {
for _, restApi := range output.Items {
stagesInput := &apigateway.GetDeploymentsInput{
RestApiId: restApi.Id,
}

deploymentsOutput, err := fn.GetDeploymentsWithContext(ctx, stagesInput)
if err != nil {
panic(err)
innerErr = err
return false
}

source := &GetDeploymentsOutput{
GetDeploymentsOutput: deploymentsOutput,
restApiId: restApi.Id,
restApiName: restApi.Name,
}
_ = api.SendRecords(ctx, ch, name, source)

if !api.SendRecords(ctx, ch, name, source) {
return false
}
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
14 changes: 11 additions & 3 deletions pkg/service/apigateway/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,34 @@ func (fn *GetStages) New(name string, config interface{}) ([]api.Request, error)
}

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.GetRestApisPagesWithContext(ctx, &input, func(output *apigateway.GetRestApisOutput, last bool) bool {
var innerErr, outerErr error

outerErr = fn.GetRestApisPagesWithContext(ctx, &input, func(output *apigateway.GetRestApisOutput, last bool) bool {
for _, restApi := range output.Items {
stagesInput := &apigateway.GetStagesInput{
RestApiId: restApi.Id,
}

stagesOutput, err := fn.GetStagesWithContext(ctx, stagesInput)
if err != nil {
panic(err)
innerErr = err
return false
}

source := &GetStagesOutput{
GetStagesOutput: stagesOutput,
restApiId: restApi.Id,
restApiName: restApi.Name,
}
_ = api.SendRecords(ctx, ch, name, source)

if !api.SendRecords(ctx, ch, name, source) {
return false
}
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
9 changes: 7 additions & 2 deletions pkg/service/dynamodb/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ func (fn *DescribeTable) New(name string, config interface{}) ([]api.Request, er
}

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.ListTablesPagesWithContext(ctx, &input, func(output *dynamodb.ListTablesOutput, last bool) bool {
var innerErr, outerErr error

outerErr = fn.ListTablesPagesWithContext(ctx, &input, func(output *dynamodb.ListTablesOutput, last bool) bool {
for _, tableName := range output.TableNames {
describeTableInput := &dynamodb.DescribeTableInput{
TableName: tableName,
}

describeTableOutput, err := fn.DescribeTableWithContext(ctx, describeTableInput)
if err != nil {
panic(err)
innerErr = err
return false
}

if !api.SendRecords(ctx, ch, name, &DescribeTableOutput{describeTableOutput}) {
Expand All @@ -54,6 +57,8 @@ func (fn *DescribeTable) New(name string, config interface{}) ([]api.Request, er
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
9 changes: 7 additions & 2 deletions pkg/service/ecs/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@ func (fn *DescribeClusters) New(name string, config interface{}) ([]api.Request,
}

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.ListClustersPagesWithContext(ctx, &input, func(output *ecs.ListClustersOutput, last bool) bool {
var innerErr, outerErr error

outerErr = fn.ListClustersPagesWithContext(ctx, &input, func(output *ecs.ListClustersOutput, last bool) bool {
describeClustersInput := &ecs.DescribeClustersInput{
Clusters: output.ClusterArns,
}

describeClustersOutput, err := fn.DescribeClustersWithContext(ctx, describeClustersInput)
if err != nil {
panic(err)
innerErr = err
return false
}
return api.SendRecords(ctx, ch, name, &DescribeClustersOutput{describeClustersOutput})
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
14 changes: 10 additions & 4 deletions pkg/service/ecs/container_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func (fn *DescribeContainerInstances) New(name string, config interface{}) ([]ap
var input ecs.ListClustersInput

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.ListClustersPagesWithContext(ctx, &input, func(output *ecs.ListClustersOutput, last bool) bool {
var innerErr, outerErr error

outerErr = fn.ListClustersPagesWithContext(ctx, &input, func(output *ecs.ListClustersOutput, last bool) bool {
for _, clusterArn := range output.ClusterArns {
// we can now describe up to 10 tasks per nested page
listContainerInstancesInput := &ecs.ListContainerInstancesInput{
Expand All @@ -56,16 +58,20 @@ func (fn *DescribeContainerInstances) New(name string, config interface{}) ([]ap

describeContainerInstancesOutput, err := fn.DescribeContainerInstancesWithContext(ctx, describeContainerInstancesInput)
if err != nil {
panic(err)
innerErr = err
return false
}
return api.SendRecords(ctx, ch, name, &DescribeContainerInstancesOutput{describeContainerInstancesOutput})
})
if err != nil {
panic(err)

if innerErr = api.FirstError(err, innerErr); innerErr != nil {
return false
}
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
14 changes: 10 additions & 4 deletions pkg/service/ecs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func (fn *DescribeServices) New(name string, config interface{}) ([]api.Request,
var input ecs.ListClustersInput

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.ListClustersPagesWithContext(ctx, &input, func(output *ecs.ListClustersOutput, last bool) bool {
var outerErr, innerErr error

outerErr = fn.ListClustersPagesWithContext(ctx, &input, func(output *ecs.ListClustersOutput, last bool) bool {
for _, clusterArn := range output.ClusterArns {

// we can now describe up to 10 services per nested page
Expand All @@ -57,16 +59,20 @@ func (fn *DescribeServices) New(name string, config interface{}) ([]api.Request,

describeServicesOutput, err := fn.DescribeServicesWithContext(ctx, describeServicesInput)
if err != nil {
panic(err)
innerErr = err
return false
}
return api.SendRecords(ctx, ch, name, &DescribeServicesOutput{describeServicesOutput})
})
if err != nil {
panic(err)
if innerErr = api.FirstError(err, innerErr); innerErr != nil {
innerErr = err
return false
}
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
13 changes: 9 additions & 4 deletions pkg/service/ecs/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func (fn *DescribeTasks) New(name string, config interface{}) ([]api.Request, er
var input ecs.ListClustersInput

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.ListClustersPagesWithContext(ctx, &input, func(output *ecs.ListClustersOutput, last bool) bool {
var outerErr, innerErr error

outerErr = fn.ListClustersPagesWithContext(ctx, &input, func(output *ecs.ListClustersOutput, last bool) bool {
for _, clusterArn := range output.ClusterArns {
// we can now describe up to 10 tasks per nested page
listTasksInput := &ecs.ListTasksInput{
Expand All @@ -56,16 +58,19 @@ func (fn *DescribeTasks) New(name string, config interface{}) ([]api.Request, er

describeTasksOutput, err := fn.DescribeTasksWithContext(ctx, describeTasksInput)
if err != nil {
panic(err)
innerErr = err
return false
}
return api.SendRecords(ctx, ch, name, &DescribeTasksOutput{describeTasksOutput})
})
if err != nil {
panic(err)
if innerErr = api.FirstError(err, innerErr); innerErr != nil {
return false
}
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
15 changes: 12 additions & 3 deletions pkg/service/efs/backuppolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,29 @@ func (fn *DescribeBackupPolicy) New(name string, config interface{}) ([]api.Requ
}

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.DescribeFileSystemsPagesWithContext(ctx, &fsInput, func(output *efs.DescribeFileSystemsOutput, last bool) bool {
var outerErr, innerErr error

outerErr = fn.DescribeFileSystemsPagesWithContext(ctx, &fsInput, func(output *efs.DescribeFileSystemsOutput, last bool) bool {
for _, fs := range output.FileSystems {
bcInput.FileSystemId = fs.FileSystemId
output, err := fn.DescribeBackupPolicyWithContext(ctx, &bcInput)
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == efs.ErrCodePolicyNotFound {
continue
}
panic(err)

innerErr = err
return false
}

if !api.SendRecords(ctx, ch, name, &DescribeBackupPolicyOutput{FilesystemID: fs.FileSystemId, DescribeBackupPolicyOutput: output}) {
return false
}
api.SendRecords(ctx, ch, name, &DescribeBackupPolicyOutput{FilesystemID: fs.FileSystemId, DescribeBackupPolicyOutput: output})
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
15 changes: 12 additions & 3 deletions pkg/service/efs/filesystempolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,29 @@ func (fn *DescribeFileSystemPolicy) New(name string, config interface{}) ([]api.
}

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.DescribeFileSystemsPagesWithContext(ctx, &fsInput, func(output *efs.DescribeFileSystemsOutput, last bool) bool {
var outerErr, innerErr error

outerErr = fn.DescribeFileSystemsPagesWithContext(ctx, &fsInput, func(output *efs.DescribeFileSystemsOutput, last bool) bool {
for _, fs := range output.FileSystems {
fspInput.FileSystemId = fs.FileSystemId
output, err := fn.DescribeFileSystemPolicyWithContext(ctx, &fspInput)
if err != nil {
if ae, ok := err.(awserr.Error); ok && ae.Code() == efs.ErrCodePolicyNotFound {
continue
}
panic(err)

innerErr = err
return false
}

if !api.SendRecords(ctx, ch, name, &DescribeFileSystemPolicyOutput{output}) {
return false
}
api.SendRecords(ctx, ch, name, &DescribeFileSystemPolicyOutput{output})
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
14 changes: 11 additions & 3 deletions pkg/service/efs/lifecycleconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,25 @@ func (fn *DescribeLifecycleConfiguration) New(name string, config interface{}) (
}

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.DescribeFileSystemsPagesWithContext(ctx, &fsInput, func(output *efs.DescribeFileSystemsOutput, last bool) bool {
var outerErr, innerErr error

outerErr = fn.DescribeFileSystemsPagesWithContext(ctx, &fsInput, func(output *efs.DescribeFileSystemsOutput, last bool) bool {
for _, fs := range output.FileSystems {
mtInput.FileSystemId = fs.FileSystemId
output, err := fn.DescribeLifecycleConfigurationWithContext(ctx, &mtInput)
if err != nil {
panic(err)
innerErr = err
return false
}

if !api.SendRecords(ctx, ch, name, &DescribeLifecycleConfigurationOutput{FilesystemID: fs.FileSystemId, DescribeLifecycleConfigurationOutput: output}) {
return false
}
api.SendRecords(ctx, ch, name, &DescribeLifecycleConfigurationOutput{FilesystemID: fs.FileSystemId, DescribeLifecycleConfigurationOutput: output})
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
14 changes: 11 additions & 3 deletions pkg/service/efs/mounttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,25 @@ func (fn *DescribeMountTargets) New(name string, config interface{}) ([]api.Requ
}

call := func(ctx context.Context, ch chan<- *api.Record) error {
return fn.DescribeFileSystemsPagesWithContext(ctx, &fsInput, func(output *efs.DescribeFileSystemsOutput, last bool) bool {
var outerErr, innerErr error

outerErr = fn.DescribeFileSystemsPagesWithContext(ctx, &fsInput, func(output *efs.DescribeFileSystemsOutput, last bool) bool {
for _, fs := range output.FileSystems {
mtInput.FileSystemId = fs.FileSystemId
output, err := fn.DescribeMountTargetsWithContext(ctx, &mtInput)
if err != nil {
panic(err)
innerErr = err
return false
}

if !api.SendRecords(ctx, ch, name, &DescribeMountTargetsOutput{output}) {
return false
}
api.SendRecords(ctx, ch, name, &DescribeMountTargetsOutput{output})
}
return true
})

return api.FirstError(outerErr, innerErr)
}

return []api.Request{call}, nil
Expand Down
Loading

0 comments on commit 03b5d08

Please sign in to comment.