From bf92a3b688d8cd6e0ce7159ddcdebda0a8dcf586 Mon Sep 17 00:00:00 2001 From: Trevor Suarez Date: Wed, 22 Feb 2023 01:47:33 -0700 Subject: [PATCH] Improving the handling of errors from sources This rids of the previous ambiguity of which source may have caused what error, as you wouldn't necessarily know if the error occurred from the source and the source was "automatically" chosen via the "ProvidePreferred" process. --- define.go | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) 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)