make recommendation on how applications should handle errors due to tabs in YAML files

Issue #42 new
Jason Sachs
created an issue

Tabs in YAML files are one of the most common errors, and my end users make it all the time depending on their editor. But the error message, while useful to a programmer, is completely bewildering to an end user:

ScannerError: while scanning for the next token
found character '\t' that cannot start any token
  in "<string>", line 91, column 1:
              1vDC_on:  'vDC > 15500'

They don't care about scanners and tokens and \t. What they need to know is that they have a tab character in column 1 of line 91. So I need a way to catch the exception and if it's a tab problem, I need to reformat a useful error message that is more meaningful to end users.

My request is:

  • help me figure out how to take a ScannerError object and distinguish between the tab problem and others
  • Because this case is so common, I would strongly suggest writing up an example for others to use as well.

Comments (2)

  1. Jason Sachs reporter

    I am handling in my code now by looking for found character '\t' that cannot start any token but this seems rather brittle.

    def interceptTabError(e, filename):
        if e.problem == "found character '\\t' that cannot start any token":
            mark = e.problem_mark
            raise ValueError("Please ensure configuration files contain no tabs;\n"
              +"use spaces for indentation. Found a tab in '%s' at line %d column %d:\n%s"
              % (filename, mark.line+1, mark.column+1, mark.get_snippet()))
          with open(self.filename) as f:
               raw =
                   y = yaml.safe_load(raw)
               except yaml.scanner.ScannerError as e:
                   interceptTabError(e, self.filename)
  2. Log in to comment