github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/vugufmt/fmterror.go (about)

     1  package vugufmt
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // FmtError is a formatting error.
    10  type FmtError struct {
    11  	Msg      string
    12  	FileName string
    13  	Line     int
    14  	Column   int
    15  }
    16  
    17  func (e FmtError) Error() string {
    18  	return fmt.Sprintf("%s:%v:%v: %v", e.FileName, e.Line, e.Column, e.Msg)
    19  }
    20  
    21  // fromGoFmt reads stdErr output from gofmt and parses it all out (if able)
    22  func fromGoFmt(msg string) *FmtError {
    23  	splitUp := strings.SplitN(msg, ":", 4)
    24  
    25  	if len(splitUp) != 4 {
    26  		return &FmtError{
    27  			Msg: msg,
    28  		}
    29  	}
    30  
    31  	line, err := strconv.Atoi(splitUp[1])
    32  	if err != nil {
    33  		return &FmtError{
    34  			Msg: msg,
    35  		}
    36  	}
    37  
    38  	column, err := strconv.Atoi(splitUp[2])
    39  	if err != nil {
    40  		return &FmtError{
    41  			Msg: msg,
    42  		}
    43  	}
    44  
    45  	return &FmtError{
    46  		Msg:      strings.TrimSpace(splitUp[3]),
    47  		FileName: splitUp[0],
    48  		Line:     line,
    49  		Column:   column,
    50  	}
    51  }