Upatede Error type to SourceError

Added processor object to code
Verified compile errors appearing
Signed-off-by: notzippy@gmail.com
This commit is contained in:
notzippy@gmail.com
2020-04-25 15:32:29 -07:00
parent 9a9511d28f
commit 548cbc1764
10 changed files with 282 additions and 99 deletions

View File

@@ -3,6 +3,8 @@ package utils
import (
"fmt"
"github.com/revel/cmd/logger"
"strconv"
"regexp"
)
type (
@@ -43,3 +45,60 @@ func NewBuildIfError(err error, message string, args ...interface{}) (b error) {
func (b *BuildError) Error() string {
return fmt.Sprint(b.Message, b.Args)
}
// Parse the output of the "go build" command.
// Return a detailed Error.
func NewCompileError(importPath, errorLink string, error error) *SourceError {
// Get the stack from the error
errorMatch := regexp.MustCompile(`(?m)^([^:#]+):(\d+):(\d+:)? (.*)$`).
FindSubmatch([]byte(error.Error()))
if errorMatch == nil {
errorMatch = regexp.MustCompile(`(?m)^(.*?):(\d+):\s(.*?)$`).FindSubmatch([]byte(error.Error()))
if errorMatch == nil {
Logger.Error("Failed to parse build errors", "error", error)
return &SourceError{
SourceType: "Go code",
Title: "Go Compilation Error",
Description: "See console for build error.",
}
}
errorMatch = append(errorMatch, errorMatch[3])
Logger.Error("Build errors", "errors", error)
}
// Read the source for the offending file.
var (
relFilename = string(errorMatch[1]) // e.g. "src/revel/sample/app/controllers/app.go"
absFilename = relFilename
line, _ = strconv.Atoi(string(errorMatch[2]))
description = string(errorMatch[4])
compileError = &SourceError{
SourceType: "Go code",
Title: "Go Compilation Error",
Path: relFilename,
Description: description,
Line: line,
}
)
// errorLink := paths.Config.StringDefault("error.link", "")
if errorLink != "" {
compileError.SetLink(errorLink)
}
fileStr, err := ReadLines(absFilename)
if err != nil {
compileError.MetaError = absFilename + ": " + err.Error()
Logger.Info("Unable to readlines "+compileError.MetaError, "error", err)
return compileError
}
compileError.SourceLines = fileStr
return compileError
}

View File

@@ -8,7 +8,7 @@ import (
// The error is a wrapper for the
type (
Error struct {
SourceError struct {
SourceType string // The type of source that failed to build.
Title, Path, Description string // Description of the error, as presented to the user.
Line, Column int // Where the error was encountered.
@@ -24,8 +24,8 @@ type (
}
)
// Return a new error object
func NewError(source, title,path,description string) *Error {
return &Error {
func NewError(source, title,path,description string) *SourceError {
return &SourceError{
SourceType:source,
Title:title,
Path:path,
@@ -34,7 +34,7 @@ func NewError(source, title,path,description string) *Error {
}
// Creates a link based on the configuration setting "errors.link"
func (e *Error) SetLink(errorLink string) {
func (e *SourceError) SetLink(errorLink string) {
errorLink = strings.Replace(errorLink, "{{Path}}", e.Path, -1)
errorLink = strings.Replace(errorLink, "{{Line}}", strconv.Itoa(e.Line), -1)
@@ -44,7 +44,7 @@ func (e *Error) SetLink(errorLink string) {
// Error method constructs a plaintext version of the error, taking
// account that fields are optionally set. Returns e.g. Compilation Error
// (in views/header.html:51): expected right delim in end; got "}"
func (e *Error) Error() string {
func (e *SourceError) Error() string {
if e == nil {
panic("opps")
}
@@ -69,7 +69,7 @@ func (e *Error) Error() string {
// ContextSource method returns a snippet of the source around
// where the error occurred.
func (e *Error) ContextSource() []SourceLine {
func (e *SourceError) ContextSource() []SourceLine {
if e.SourceLines == nil {
return nil
}

View File

@@ -150,7 +150,7 @@ func MustChmod(filename string, mode os.FileMode) {
// Called if panic
func PanicOnError(err error, msg string) {
if revErr, ok := err.(*Error); (ok && revErr != nil) || (!ok && err != nil) {
if revErr, ok := err.(*SourceError); (ok && revErr != nil) || (!ok && err != nil) {
Logger.Panicf("Abort: %s: %s %s", msg, revErr, err)
}
}