github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/diag/error.go (about)

     1  package diag
     2  
     3  import "fmt"
     4  
     5  // Error represents an error with context that can be showed.
     6  type Error struct {
     7  	Type    string
     8  	Message string
     9  	Context Context
    10  }
    11  
    12  // Error returns a plain text representation of the error.
    13  func (e *Error) Error() string {
    14  	return fmt.Sprintf("%s: %d-%d in %s: %s",
    15  		e.Type, e.Context.From, e.Context.To, e.Context.Name, e.Message)
    16  }
    17  
    18  // Range returns the range of the error.
    19  func (e *Error) Range() Ranging {
    20  	return e.Context.Range()
    21  }
    22  
    23  // Show shows the error.
    24  func (e *Error) Show(indent string) string {
    25  	header := fmt.Sprintf("%s: \033[31;1m%s\033[m\n", e.Type, e.Message)
    26  	return header + e.Context.ShowCompact(indent+"  ")
    27  }