Skip to content

gopherjs serve: Display compilation errors in browser console. #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func main() {
}
}
return nil
}, options)
}, options, nil)

if s.Watcher == nil {
os.Exit(exitCode)
Expand Down Expand Up @@ -184,7 +184,7 @@ func main() {
}
}
return nil
}, options)
}, options, nil)

if s.Watcher == nil {
os.Exit(exitCode)
Expand Down Expand Up @@ -238,7 +238,7 @@ func main() {
return err
}
return nil
}, options))
}, options, nil))
}

cmdTest := &cobra.Command{
Expand Down Expand Up @@ -394,7 +394,7 @@ func main() {
fmt.Printf("%s\t%s\t%.3fs\n", status, pkg.ImportPath, time.Now().Sub(start).Seconds())
}
return exitErr
}, options))
}, options, nil))
}

cmdTool := &cobra.Command{
Expand Down Expand Up @@ -422,7 +422,7 @@ func main() {
}
cmdTool.Help()
return nil
}, options))
}, options, nil))
}

cmdServe := &cobra.Command{
Expand Down Expand Up @@ -485,7 +485,8 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {

if isMain {
buf := bytes.NewBuffer(nil)
handleError(func() error {
browserErrors := bytes.NewBuffer(nil)
exitCode := handleError(func() error {
pkg := &gbuild.PackageData{Package: buildPkg}
if err := s.BuildPackage(pkg); err != nil {
return err
Expand All @@ -509,7 +510,10 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
fs.sourceMaps[name+".map"] = mapBuf.Bytes()

return nil
}, fs.options)
}, fs.options, browserErrors)
if exitCode != 0 {
buf = browserErrors
}
return newFakeFile("main.js", buf.Bytes()), nil
}
}
Expand Down Expand Up @@ -563,24 +567,26 @@ func (f *fakeFile) Sys() interface{} {
return nil
}

func handleError(f func() error, options *gbuild.Options) int {
// If browserErrors is non-nil, errors are written for presentation in browser.
func handleError(f func() error, options *gbuild.Options, browserErrors *bytes.Buffer) int {
switch err := f().(type) {
case nil:
return 0
case compiler.ErrorList:
for _, entry := range err {
printError(entry, options)
printError(entry, options, browserErrors)
}
return 1
case *exec.ExitError:
return err.Sys().(syscall.WaitStatus).ExitStatus()
default:
printError(err, options)
printError(err, options, browserErrors)
return 1
}
}

func printError(err error, options *gbuild.Options) {
// sprintError returns an annotated error string without trailing newline.
func sprintError(err error) string {
makeRel := func(name string) string {
if relname, err := filepath.Rel(currentDirectory, name); err == nil {
return relname
Expand All @@ -590,12 +596,21 @@ func printError(err error, options *gbuild.Options) {

switch e := err.(type) {
case *scanner.Error:
options.PrintError("%s:%d:%d: %s\n", makeRel(e.Pos.Filename), e.Pos.Line, e.Pos.Column, e.Msg)
return fmt.Sprintf("%s:%d:%d: %s", makeRel(e.Pos.Filename), e.Pos.Line, e.Pos.Column, e.Msg)
case types.Error:
pos := e.Fset.Position(e.Pos)
options.PrintError("%s:%d:%d: %s\n", makeRel(pos.Filename), pos.Line, pos.Column, e.Msg)
return fmt.Sprintf("%s:%d:%d: %s", makeRel(pos.Filename), pos.Line, pos.Column, e.Msg)
default:
options.PrintError("%s\n", e)
return fmt.Sprintf("%s", e)
}
}

// printError prints err to Stderr with options. If browserErrors is non-nil, errors are also written for presentation in browser.
func printError(err error, options *gbuild.Options, browserErrors *bytes.Buffer) {
e := sprintError(err)
options.PrintError("%s\n", e)
if browserErrors != nil {
fmt.Fprintln(browserErrors, `console.error("`+template.JSEscapeString(e)+`");`)
}
}

Expand Down