Separate error messages for public consume from internal error messages for logging

43 views
Skip to first unread message

anh

unread,
Sep 30, 2018, 10:49:03 PM9/30/18
to Go kit

Hi everyone, 

I'm trying to customize the encoding of error messages so that the json responses to public consume do not contains the internal detail. 

For example, instead of returning response with specific error from a service implementation like: { "error": "Kafka: ...."}
I want to response with just  {"error": "Internal Server Error"} AND at the same time keep logging middleware emit logs with above service errors to see what happen from inside.
 
After studying the profilesvc example's error handling [1] I replace lines L386-L388 with this:
 
...
    json.NewEncoder(w).Encode(map[string]interface{}{
        "error": messageFrom(err),
    })
...

func messageFrom(err error) string { 
    switch err.(type) {
    case sarama.KError:
        return "Internal Server Error"  
    default:
        return err.Error()
    }
}


However it doesn't look right, the implementation of service and the error types leak into the transport code. 

Looking forwards for your ideas and helps.   

Regards,
Anh 

Tamás Gulácsi

unread,
Sep 30, 2018, 10:55:13 PM9/30/18
to Go kit
and Log "+%v" (and maybe errors.Cause(err).Error()) but "%v" (err.Error()) in encodeError ?

le anh

unread,
Oct 4, 2018, 12:08:01 AM10/4/18
to tgula...@gmail.com, go-...@googlegroups.com
Hi, 

I read the example again and thanks to its extensive comments I figure it out. 
The encodeError helper  was used in both httptransport.ServerErrorEncoder and  encodeResponse.

I now use a different encode error function inside encodeResponse after checking if the response implemented Errorer (Failer) interface. 

Thank you for pointing out pkg/errors package, very helpful.


--
You received this message because you are subscribed to the Google Groups "Go kit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to go-kit+un...@googlegroups.com.
To post to this group, send email to go-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/go-kit/310bb8cb-0c64-4cd4-8c36-280fa8642e0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages