error.go 929 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package multierr
  2. import (
  3. "fmt"
  4. "github.com/sqlc-dev/sqlc/internal/source"
  5. "github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
  6. )
  7. type FileError struct {
  8. Filename string
  9. Line int
  10. Column int
  11. Err error
  12. }
  13. func (e *FileError) Unwrap() error {
  14. return e.Err
  15. }
  16. type Error struct {
  17. errs []*FileError
  18. }
  19. func (e *Error) Add(filename, in string, loc int, err error) {
  20. line := 1
  21. column := 1
  22. if lerr, ok := err.(*sqlerr.Error); ok {
  23. if lerr.Location != 0 {
  24. loc = lerr.Location
  25. } else if lerr.Line != 0 && lerr.Column != 0 {
  26. line = lerr.Line
  27. column = lerr.Column
  28. }
  29. }
  30. if in != "" && loc != 0 {
  31. line, column = source.LineNumber(in, loc)
  32. }
  33. e.errs = append(e.errs, &FileError{filename, line, column, err})
  34. }
  35. func (e *Error) Errs() []*FileError {
  36. return e.errs
  37. }
  38. func (e *Error) Error() string {
  39. return fmt.Sprintf("multiple errors: %d errors", len(e.errs))
  40. }
  41. func New() *Error {
  42. return &Error{}
  43. }