mirror of https://github.com/fluxcd/cli-utils.git
Handle errors better in table views
This commit is contained in:
parent
286b36e272
commit
0235ee579d
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue