Skip to content
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

text/scanner: default error to os.Stderr should mention "text/scanner" #14166

Closed
client9 opened this issue Jan 31, 2016 · 2 comments
Closed

text/scanner: default error to os.Stderr should mention "text/scanner" #14166

client9 opened this issue Jan 31, 2016 · 2 comments
Milestone

Comments

@client9
Copy link

client9 commented Jan 31, 2016

What version of Go are you using (go version)?
go version go1.5.2 darwin/amd64

What did you do?

Ran golang text/scanner Scanner on a file.

GOT:
My program was outputting odd error messages to Stderr.

87:17: illegal char escape

But as text/scanner is a small part of the whole program it wasn't obvious what this message was or where it was coming from. I was reviewing a lot of code until i traced all the back to

https://golang.org/src/text/scanner/scanner.go?s=14356:14385#L531

   326  func (s *Scanner) error(msg string) {
   327      s.ErrorCount++
   328      if s.Error != nil {
   329          s.Error(s, msg)
   330          return
   331      }
   332      pos := s.Position
   333      if !pos.IsValid() {
   334          pos = s.Pos()
   335      }
   336      fmt.Fprintf(os.Stderr, "%s: %s\n", pos, msg)  // <----HERE
   337  }

(The error it self isn't the problem and was correct, it's just the delivery that is the issue).

WANT:

It would be nice to make error output configurable somehow. For my needs, just having a io.Writer (default to os.Stderr) that i can set to ioutil.Discard is all I need but I'm sure others will have more advanced requirements.

thanks all!

nickg

@minux minux changed the title text/scanner hardwired error output to os.Stderr, make configurable text/scanner: hardwired error output to os.Stderr, make configurable Jan 31, 2016
@minux minux added this to the Go1.7 milestone Jan 31, 2016
@griesemer
Copy link
Contributor

It is configurable. Read the doc string for Scan:

It reports scanner errors (read and token errors) by calling s.Error, if not nil; otherwise it prints an error message to os.Stderr.

Note also that you tracked down the offending code, and line 328 above shows the logic invoking s.Error.

But I agree that the default error message should report "text/scanner" so we know where it's coming from. Changed issue title.

@griesemer griesemer changed the title text/scanner: hardwired error output to os.Stderr, make configurable text/scanner: default error to os.Stderr should mention "text/scanner" Jan 31, 2016
@griesemer griesemer self-assigned this Jan 31, 2016
@client9
Copy link
Author

client9 commented Feb 1, 2016

@griesemer ha! great. I traced down into error() and assumed that since it was a private function so that was the end of that. Duh. Thanks for your help and the update on the ticket. Regards, n

@golang golang locked and limited conversation to collaborators Feb 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants