diff --git a/define.go b/define.go index 22bf18d..c488ae8 100644 --- a/define.go +++ b/define.go @@ -94,25 +94,38 @@ func init() { handleError(err, flags.Parse(os.Args[1:])) } -func handleError(err ...error) { +func handleSourceError(source string, err ...error) { for _, e := range err { - if e != nil { - msg := e.Error() + if e == nil { + continue + } - if len(msg) > 1 { - // Format the message - msg = strings.ToTitle(msg[:1]) + msg[1:] + msg := e.Error() - stdErrWriter.IndentWrites(func(writer *defineio.PanicWriter) { - writer.WritePaddedStringLine(msg, 1) - }) - } + if len(msg) > 1 { + // Format the message + msg = strings.ToTitle(msg[:1]) + msg[1:] - quit(1) + stdErrWriter.IndentWrites(func(writer *defineio.PanicWriter) { + if source != "" { + sourceMessage := fmt.Sprintf("Source %q encountered an error.", source) + + writer.WriteNewLine() + writer.WriteStringLine(sourceMessage) + } + + writer.WritePaddedStringLine(msg, 1) + }) } + + quit(1) } } +func handleError(err ...error) { + handleSourceError("", err...) +} + func quit(code int) { os.Exit(code) } @@ -164,7 +177,7 @@ func printUsage(writer *defineio.PanicWriter) { func defineWord(word string) { results, err := src.Define(word) - handleError(err, source.ValidateDictionaryResults(word, results)) + handleSourceError(src.Name(), err, source.ValidateDictionaryResults(word, results)) resultPrinter := printer.NewResultPrinter(stdOutWriter)