Exemple #1
0
// SendResponseError returns a response for a processed request that failed.
func SendResponseError(conn *nats.EncodedConn, reply string, err error) error {
	log.Println("Reponding Error:", reply, err)

	var m []map[string]interface{}
	json.Unmarshal([]byte(fmt.Sprintf(`[{"error":"%s"}]`, err.Error())), &m)

	resp := Response{
		Status: 1,
		JSON:   m,
	}

	return conn.Publish(reply, resp)
}
Exemple #2
0
// SendResponse returns a response for a processed request.
func SendResponse(conn *nats.EncodedConn, reply string, v interface{}) error {
	log.Println("Reponding:", reply)

	data, err := json.Marshal(v)
	if err != nil {
		return SendResponseError(conn, reply, err)
	}

	var m []map[string]interface{}
	if err := json.Unmarshal(data, &m); err != nil {
		return SendResponseError(conn, reply, err)
	}

	resp := Response{
		Status: 0,
		JSON:   m,
	}

	return conn.Publish(reply, resp)
}