func()

in websocketserver/websocketserver.go [229:265]


func (s *Server) processMessage(msg common.Message) (*common.Message, error) {
	if msg.Code == common.Ping {
		return &msg, nil
	}

	result, err := s.handleMessageType(msg)
	if err != nil {
		logrus.Error("error handling the received message: ", err)
		// Try to send a message with the error
		err := s.WriteErrorMessage(msg, http.StatusInternalServerError, err.Error())
		// if the message cannot be sent, then the connection is closed
		if err != nil {
			logrus.Error("error sending error message: ", err)
			return nil, err
		}
		// we had an error handling the message and we notified the user,
		// we can start reading messages again
		return nil, nil
	}

	var payload json.RawMessage
	if result != nil {
		payload, err = json.Marshal(result)
		if err != nil {
			logrus.Error("error encoding response: ", err)
			err := s.WriteErrorMessage(msg, http.StatusInternalServerError, "error encoding response")
			if err != nil {
				return nil, err
			}

			return nil, nil
		}
		msg.Payload = payload
	}

	return &msg, nil
}