line_error.go 1000 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package srcinfo
  2. import (
  3. "fmt"
  4. )
  5. // LineError is an error type that stores the line number at which an error
  6. // occurred as well the full Line that cased the error and an error string.
  7. type LineError struct {
  8. LineNumber int // The line number at which the error occurred
  9. Line string // The line that caused the error
  10. ErrorStr string // An error string
  11. }
  12. // Error Returns an error string in the format:
  13. // "Line <LineNumber>: <ErrorStr>: <Line>".
  14. func (le LineError) Error() string {
  15. return fmt.Sprintf("Line %d: %s: %s", le.LineNumber, le.ErrorStr, le.Line)
  16. }
  17. // Error Returns a new LineError
  18. func Error(LineNumber int, Line string, ErrorStr string) *LineError {
  19. return &LineError{
  20. LineNumber,
  21. Line,
  22. ErrorStr,
  23. }
  24. }
  25. // Errorf Returns a new LineError using the same formatting rules as
  26. // fmt.Printf.
  27. func Errorf(LineNumber int, Line string, ErrorStr string, args ...interface{}) *LineError {
  28. return &LineError{
  29. LineNumber,
  30. Line,
  31. fmt.Sprintf(ErrorStr, args...),
  32. }
  33. }