From 21674c05e5251b9d1218e11da2ca98d0911a5659 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Wed, 27 Jun 2018 16:56:49 -0700 Subject: [PATCH] channeldb/codec: add concrete error for unknown types --- channeldb/codec.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/channeldb/codec.go b/channeldb/codec.go index 12d14627..03d2b416 100644 --- a/channeldb/codec.go +++ b/channeldb/codec.go @@ -43,6 +43,19 @@ func readOutpoint(r io.Reader, o *wire.OutPoint) error { return nil } +// UnknownElementType is an error returned when the codec is unable to encode or +// decode a particular type. +type UnknownElementType struct { + method string + element interface{} +} + +// Error returns the name of the method that encountered the error, as well as +// the type that was unsupported. +func (e UnknownElementType) Error() string { + return fmt.Sprintf("Unknown type in %s: %T", e.method, e.element) +} + // WriteElement is a one-stop shop to write the big endian representation of // any element which is to be serialized for storage on disk. The passed // io.Writer should be backed by an appropriately sized byte slice, or be able @@ -164,7 +177,7 @@ func WriteElement(w io.Writer, element interface{}) error { } default: - return fmt.Errorf("Unknown type in WriteElement: %T", e) + return UnknownElementType{"WriteElement", e} } return nil @@ -343,7 +356,7 @@ func ReadElement(r io.Reader, element interface{}) error { } default: - return fmt.Errorf("Unknown type in ReadElement: %T", e) + return UnknownElementType{"ReadElement", e} } return nil