Example #1
0
// UserErrorMessageP is responsible for formatting user-facing error messages
// This function is similar to UserErrorMessage except that it also allows the
// caller to specify the message prefix.
// prefix: An error message prefix such as "Error:". It should not end with a space.
func UserErrorMessageP(file *MojomFile, token lexer.Token, prefix, message string) string {
	// TODO(rudominer) Allow users to disable the use of color in snippets.
	useColor := true

	// Optionally color the prefix red.
	if useColor && len(prefix) > 0 {
		prefix = fmt.Sprintf("\x1b[31;1m%s\x1b[0m", prefix)
	}
	if len(prefix) > 0 {
		prefix = fmt.Sprintf("%s ", prefix)
	}
	message = fmt.Sprintf("%s%s", prefix, message)

	filePath := "Unknown file"
	importedFromMessage := ""
	snippet := ""
	if file != nil {
		if len(file.fileContents) > 0 {
			snippet = fmt.Sprintf("\n%s", token.Snippet(file.fileContents, useColor))
		}
		filePath = RelPathIfShorter(file.CanonicalFileName)
		importedFromMessage = file.ImportedFromMessage()
	}
	return fmt.Sprintf("\n%s:%s: %s%s\n%s", filePath, token.ShortLocationString(),
		message, snippet, importedFromMessage)
}