From 7aa7dd7e68891204aa2a750f59043f22f477c53e Mon Sep 17 00:00:00 2001
From: Brian West <brian@freeswitch.org>
Date: Tue, 7 Oct 2008 17:09:31 +0000
Subject: [PATCH] add error handling and error messages for incorrect event api
 usage

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9876 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 src/switch_cpp.cpp | 35 +++++++++++++++++++++++++++++++++--
 src/switch_event.c |  8 +++-----
 2 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/src/switch_cpp.cpp b/src/switch_cpp.cpp
index b2a9d0277e..e93fcbaf34 100644
--- a/src/switch_cpp.cpp
+++ b/src/switch_cpp.cpp
@@ -208,7 +208,16 @@ SWITCH_DECLARE_CONSTRUCTOR Event::Event(const char *type, const char *subclass_n
 		event_id = SWITCH_EVENT_MESSAGE;
 	}
 
-	switch_event_create_subclass(&event, event_id, subclass_name);
+	if (!switch_strlen_zero(subclass_name) && event_id != SWITCH_EVENT_CUSTOM) {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_WARNING, "Changing event type to custom because you specified a subclass name!\n");
+		event_id = SWITCH_EVENT_CUSTOM;
+	}
+
+	if (switch_event_create_subclass(&event, event_id, subclass_name) != SWITCH_STATUS_SUCCESS) {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Failed to create event!\n");
+		event = NULL;
+	}
+
 	serialized_string = NULL;
 	mine = 1;
 }
@@ -283,9 +292,17 @@ SWITCH_DECLARE(bool) Event::fire(void)
 	if (event) {
 		switch_event_t *new_event;
 		if (switch_event_dup(&new_event, event) == SWITCH_STATUS_SUCCESS) {
-			switch_event_fire(&new_event);
+			if (switch_event_fire(&new_event) != SWITCH_STATUS_SUCCESS) {
+				switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Failed to fire the event!\n");
+				switch_event_destroy(&new_event);
+				return false;
+			}
 			return true;
+		} else {
+			switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Failed to dup the event!\n");
 		}
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to fire an event that does not exist!\n");
 	}
 	return false;
 }
@@ -297,6 +314,8 @@ SWITCH_DECLARE(bool) Event::setPriority(switch_priority_t priority)
 	if (event) {
         switch_event_set_priority(event, priority);
 		return true;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to setPriority an event that does not exist!\n");
     }
 	return false;
 }
@@ -307,6 +326,8 @@ SWITCH_DECLARE(const char *)Event::getHeader(char *header_name)
 
 	if (event) {
 		return switch_event_get_header(event, header_name);
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to getHeader an event that does not exist!\n");
 	}
 	return NULL;
 }
@@ -317,6 +338,8 @@ SWITCH_DECLARE(bool) Event::addHeader(const char *header_name, const char *value
 
 	if (event) {
 		return switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header_name, value) == SWITCH_STATUS_SUCCESS ? true : false;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to addHeader an event that does not exist!\n");
 	}
 
 	return false;
@@ -328,6 +351,8 @@ SWITCH_DECLARE(bool) Event::delHeader(const char *header_name)
 
 	if (event) {
 		return switch_event_del_header(event, header_name) == SWITCH_STATUS_SUCCESS ? true : false;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to delHeader an event that does not exist!\n");
 	}
 
 	return false;
@@ -340,6 +365,8 @@ SWITCH_DECLARE(bool) Event::addBody(const char *value)
 
 	if (event) {
 		return switch_event_add_body(event, "%s", value) == SWITCH_STATUS_SUCCESS ? true : false;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to addBody an event that does not exist!\n");
 	}
 	
 	return false;
@@ -352,6 +379,8 @@ SWITCH_DECLARE(char *)Event::getBody(void)
 
 	if (event) {
 		return switch_event_get_body(event);
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to getBody an event that does not exist!\n");
 	}
 	
 	return NULL;
@@ -363,6 +392,8 @@ SWITCH_DECLARE(const char *)Event::getType(void)
 
 	if (event) {
 		return switch_event_name(event->event_id);
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to getType an event that does not exist!\n");
 	}
 	
 	return (char *) "invalid";
diff --git a/src/switch_event.c b/src/switch_event.c
index 69e63217f0..26b4f5e8e3 100644
--- a/src/switch_event.c
+++ b/src/switch_event.c
@@ -548,11 +548,13 @@ SWITCH_DECLARE(switch_status_t) switch_event_create_subclass(switch_event_t **ev
 {
 	void *pop;
 
+	*event = NULL;
+
 	if (event_id != SWITCH_EVENT_CUSTOM && subclass_name) {
 		return SWITCH_STATUS_GENERR;
 	}
 
-	if (switch_queue_trypop(EVENT_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
+	if (switch_queue_trypop(EVENT_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS && pop) {
 		*event = (switch_event_t *) pop;
 	} else {
 		*event = ALLOC(sizeof(switch_event_t));
@@ -1014,10 +1016,6 @@ SWITCH_DECLARE(switch_status_t) switch_event_fire_detailed(const char *file, con
 	switch_event_add_header_string(*event, SWITCH_STACK_BOTTOM, "Event-Calling-Function", func);
 	switch_event_add_header(*event, SWITCH_STACK_BOTTOM, "Event-Calling-Line-Number", "%d", line);
 
-	if ((*event)->subclass_name) {
-		switch_event_add_header_string(*event, SWITCH_STACK_BOTTOM, "Event-Subclass", (*event)->subclass_name);
-	}
-
 	if (user_data) {
 		(*event)->event_user_data = user_data;
 	}