From c8182b800613e630aace81b61e36f73bbd4ea86a Mon Sep 17 00:00:00 2001 From: Roman Necheporenko Date: Mon, 11 Nov 2024 11:59:03 +0200 Subject: [PATCH] Implement errors.PrettyPrinter for decoding errors --- decode.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/decode.go b/decode.go index afc005c6..8c705ecd 100644 --- a/decode.go +++ b/decode.go @@ -607,6 +607,16 @@ type unknownFieldError struct { err error } +func (e *unknownFieldError) PrettyPrint(p errors.Printer, colored, inclSource bool) error { + var pp errors.PrettyPrinter + if errors.As(e.err, &pp) { + return pp.PrettyPrint(p, colored, inclSource) + } + + p.Print(e) + return nil +} + func (e *unknownFieldError) Error() string { return e.err.Error() } @@ -623,6 +633,16 @@ type duplicateKeyError struct { err error } +func (e *duplicateKeyError) PrettyPrint(p errors.Printer, colored, inclSource bool) error { + var pp errors.PrettyPrinter + if errors.As(e.err, &pp) { + return pp.PrettyPrint(p, colored, inclSource) + } + + p.Print(e) + return nil +} + func (e *duplicateKeyError) Error() string { return e.err.Error() }