From 3aeea424bef623539d5a85cbbbd15ef09cabc0a3 Mon Sep 17 00:00:00 2001 From: Aneurin Barker Snook Date: Sat, 25 Nov 2023 15:24:21 +0000 Subject: [PATCH] tweak rest WithError --- error.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/error.go b/error.go index beb9e67..c8ac17d 100644 --- a/error.go +++ b/error.go @@ -37,14 +37,7 @@ type Error struct { } // Error retrieves the message of a REST API error. -// If it has a "error" string attached using WithData or WithError, that message is returned. -// Otherwise, the Error's own message is returned. func (e Error) Error() string { - if e.Data != nil && e.Data["error"] != nil { - if value, ok := e.Data["error"].(string); ok { - return value - } - } return e.Message } @@ -73,8 +66,12 @@ func (e Error) WithData(data map[string]interface{}) Error { return e } -// WithError returns a copy of the HTTP error with the given error's message merged in to its additional data. +// WithError returns a copy of the HTTP error with the given error added either as the Message, if it empty, or as additional data. func (e Error) WithError(err error) Error { + if e.Message == "" { + return e.WithMessage(err.Error()) + } + return e.WithData(map[string]interface{}{ "error": err.Error(), })