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