diff --git a/cmd/printers/table/collector.go b/cmd/printers/table/collector.go index bbfc63c..9b7a55c 100644 --- a/cmd/printers/table/collector.go +++ b/cmd/printers/table/collector.go @@ -48,6 +48,8 @@ type ResourceStateCollector struct { // resource identifier to a ResourceInfo object that captures // the latest state for the given resource. resourceInfos map[object.ObjMetadata]*ResourceInfo + + err error } // ResourceInfo captures the latest seen state of a single resource. @@ -185,8 +187,7 @@ func (r *ResourceStateCollector) processApplyEvent(e event.ApplyEvent) { // processErrorEvent handles events for errors. func (r *ResourceStateCollector) processErrorEvent(err error) { - // TODO: Handle errors more gracefully than this. - panic(err) + r.err = err } // toIdentifier extracts the identifying information from an @@ -202,19 +203,25 @@ func toIdentifier(o runtime.Object) object.ObjMetadata { // ResourceState contains the latest state for all the resources. type ResourceState struct { - ResourceInfos ResourceInfos + resourceInfos ResourceInfos + + err error } // Resources returns a slice containing the latest state // for each individual resource. func (r *ResourceState) Resources() []table.Resource { var resources []table.Resource - for _, res := range r.ResourceInfos { + for _, res := range r.resourceInfos { resources = append(resources, res) } return resources } +func (r *ResourceState) Error() error { + return r.err +} + // LatestState returns a ResourceState object that contains // a copy of the latest state for all resources. func (r *ResourceStateCollector) LatestState() *ResourceState { @@ -233,7 +240,8 @@ func (r *ResourceStateCollector) LatestState() *ResourceState { sort.Sort(resourceInfos) return &ResourceState{ - ResourceInfos: resourceInfos, + resourceInfos: resourceInfos, + err: r.err, } } diff --git a/cmd/printers/table/printer.go b/cmd/printers/table/printer.go index 41251d5..57653ad 100644 --- a/cmd/printers/table/printer.go +++ b/cmd/printers/table/printer.go @@ -30,7 +30,8 @@ func (t *Printer) Print(ch <-chan event.Event, _ bool) { // Eventually we need a more graceful shutdown if // this happens. if e.Type == event.ErrorType { - panic(e.ErrorEvent.Err) + _, _ = fmt.Fprintf(t.IOStreams.Out, "Fatal error: %v\n", e.ErrorEvent.Err) + return } } // Create a new collector and initialize it with the resources diff --git a/cmd/status/printers/adapter.go b/cmd/status/printers/adapter.go index 1b8fec2..6db9af6 100644 --- a/cmd/status/printers/adapter.go +++ b/cmd/status/printers/adapter.go @@ -41,12 +41,17 @@ func (r *ResourceInfo) SubResources() []table.Resource { type ResourceState struct { resources []table.Resource + err error } func (rss *ResourceState) Resources() []table.Resource { return rss.resources } +func (rss *ResourceState) Error() error { + return rss.err +} + func (ca *CollectorAdapter) LatestStatus() *ResourceState { observation := ca.collector.LatestObservation() var resources []table.Resource @@ -57,5 +62,6 @@ func (ca *CollectorAdapter) LatestStatus() *ResourceState { } return &ResourceState{ resources: resources, + err: observation.Error, } } diff --git a/pkg/print/table/base.go b/pkg/print/table/base.go index 7763cfd..7c19ef4 100644 --- a/pkg/print/table/base.go +++ b/pkg/print/table/base.go @@ -28,6 +28,7 @@ type ColumnDefinition interface { // that should be printed. type ResourceStates interface { Resources() []Resource + Error() error } // Resource defines the interface that each of the Resource @@ -58,8 +59,12 @@ func (t *BaseTablePrinter) PrintTable(rs ResourceStates, t.moveUp() t.eraseCurrentLine() } - linePrintCount := 0 + if rs.Error() != nil { + return t.printError(rs.Error()) + } + + linePrintCount := 0 for i, column := range t.Columns { format := fmt.Sprintf("%%-%ds", column.Width()) t.printOrDie(format, column.Header()) @@ -136,6 +141,11 @@ func (t *BaseTablePrinter) printSubTable(resources []Resource, return linePrintCount } +func (t *BaseTablePrinter) printError(err error) int { + t.printOrDie("Fatal error: %v\n", err) + return 1 // This is the number of lines printed. +} + func (t *BaseTablePrinter) printOrDie(format string, a ...interface{}) { _, err := fmt.Fprintf(t.IOStreams.Out, format, a...) if err != nil { diff --git a/pkg/print/table/base_test.go b/pkg/print/table/base_test.go index 89a1008..3ee95eb 100644 --- a/pkg/print/table/base_test.go +++ b/pkg/print/table/base_test.go @@ -161,6 +161,10 @@ func (r *fakeResourceStates) Resources() []Resource { return r.resources } +func (r *fakeResourceStates) Error() error { + return nil +} + type fakeResource struct { resourceStatus *pe.ResourceStatus }