Permalink
Browse files

Refactor: Use common function for reporting errors

This removes the if (oo) { throw exception } else { }

Change-Id: I0c4943913a5cebf24e46af8f6df5b926083cb8c8
Reviewed-on: http://review.couchbase.org/23196
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Mordechai Nunberg <mnunberg@haskalah.org>
Tested-by: Matt Ingenthron <matt@couchbase.com>
  • Loading branch information...
trondn authored and ingenthr committed Dec 11, 2012
1 parent fc50990 commit cf70968ed3f0c781e75e4cc43de6c561019b459d
Showing with 303 additions and 333 deletions.
  1. +21 −28 arithmetic.c
  2. +1 −0 config.m4
  3. +1 −0 config.w32
  4. +19 −14 create.c
  5. +61 −0 error.c
  6. +13 −38 flush.c
  7. +19 −12 get.c
  8. +6 −1 internal.h
  9. +22 −54 options.c
  10. +15 −27 remove.c
  11. +3 −10 stat.c
  12. +56 −64 store.c
  13. +7 −8 tests/Connection.inc
  14. +23 −18 touch.c
  15. +12 −29 unlock.c
  16. +4 −10 version.c
  17. +20 −20 views.c
View
@@ -5,7 +5,6 @@ struct arithmetic_cookie {
uint64_t value;
};
-/* {{{ static void php_couchbase_arithmetic_callback(...) */
static void php_couchbase_arithmetic_callback(lcb_t instance,
const void *cookie,
lcb_error_t error,
@@ -21,12 +20,15 @@ static void php_couchbase_arithmetic_callback(lcb_t instance,
}
PHP_COUCHBASE_LOCAL
-void php_couchbase_arithmetic_impl(INTERNAL_FUNCTION_PARAMETERS, char op, int oo) /* {{{ */
+void php_couchbase_arithmetic_impl(INTERNAL_FUNCTION_PARAMETERS, char op, int oo)
{
char *key;
time_t exp;
- long klen = 0, offset = 1, expire = 0;
- long create = 0, initial = 0;
+ long klen = 0;
+ long offset = 1;
+ long expire = 0;
+ long create = 0;
+ long initial = 0;
php_couchbase_res *couchbase_res;
lcb_arithmetic_cmd_t cmd;
const lcb_arithmetic_cmd_t *const commands[] = { &cmd };
@@ -63,40 +65,31 @@ void php_couchbase_arithmetic_impl(INTERNAL_FUNCTION_PARAMETERS, char op, int oo
retval = lcb_arithmetic(instance, &cookie, 1, commands);
lcb_behavior_set_syncmode(instance, LCB_ASYNCHRONOUS);
- if (retval != LCB_SUCCESS || cookie.error != LCB_SUCCESS) {
- char errmsg[256];
- if (cookie.error == LCB_KEY_ENOENT && create == 0) {
+ if (retval == LCB_SUCCESS) {
+ retval = cookie.error ;
+ }
+ couchbase_res->rc = retval;
+
+ if (retval == LCB_SUCCESS) {
+ ZVAL_LONG(return_value, cookie.value);
+ } else {
+ if (retval == LCB_KEY_ENOENT && create == 0) {
/* The user told us to not create the key... */
RETURN_FALSE;
}
- if (retval == LCB_SUCCESS) {
- retval = cookie.error ;
- }
- couchbase_res->rc = retval;
-
- snprintf(errmsg, sizeof(errmsg), "Failed to %s value in the server: %s",
- (op == '+') ? "increment" : "decrement",
- lcb_strerror(instance, retval));
- if (oo) {
- zend_throw_exception(cb_lcb_exception, errmsg, 0 TSRMLS_CC);
- return;
- } else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errmsg);
- RETURN_FALSE;
- }
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_lcb_exception,
+ "Failed to %s value in the server: %s",
+ (op == '+') ? "increment" : "decrement",
+ lcb_strerror(instance, retval));
}
-
- couchbase_res->rc = retval;
- ZVAL_LONG(return_value, cookie.value);
}
-/* }}} */
PHP_COUCHBASE_LOCAL
void php_couchbase_callbacks_arithmetic_init(lcb_t handle)
{
- php_ignore_value(
- lcb_set_arithmetic_callback(handle, php_couchbase_arithmetic_callback));
+ (void)lcb_set_arithmetic_callback(handle, php_couchbase_arithmetic_callback);
}
/*
View
@@ -178,6 +178,7 @@ if test "$PHP_COUCHBASE" != "no"; then
convert.c \
couchbase.c \
create.c \
+ error.c \
exceptions.c \
fastlz/fastlz.c \
flush.c \
View
@@ -12,6 +12,7 @@ if (PHP_COUCHBASE != "no") {
ADD_SOURCES(configure_module_dirname, "compress.c", "couchbase");
ADD_SOURCES(configure_module_dirname, "convert.c", "couchbase");
ADD_SOURCES(configure_module_dirname, "create.c", "couchbase");
+ ADD_SOURCES(configure_module_dirname, "error.c", "couchbase");
ADD_SOURCES(configure_module_dirname, "exceptions.c", "couchbase");
ADD_SOURCES(configure_module_dirname, "flush.c", "couchbase");
ADD_SOURCES(configure_module_dirname, "get.c", "couchbase");
View
@@ -46,13 +46,11 @@ static int parse_host(const char *host,
if (!(url = php_url_parse_ex(host, host_len))) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "malformed host url %s", host);
return 0;
}
if (!url->host) {
php_url_free(url);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "malformed host url %s", host);
return 0;
}
@@ -212,25 +210,28 @@ void php_couchbase_create_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {{{ */
zend_hash_get_current_data_ex(hthosts, (void **)&curzv, &htpos) == SUCCESS;
zend_hash_move_forward_ex(hthosts, &htpos), ii++) {
if (!Z_TYPE_PP(curzv) == IS_STRING) {
-
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "Couldn't get string from node lists");
free_connparams(&cparams);
- RETURN_FALSE;
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_exception,
+ "Couldn't get string from node lists");
+ return;
}
if (!parse_host(Z_STRVAL_PP(curzv),
Z_STRLEN_PP(curzv),
&cparams TSRMLS_CC)) {
free_connparams(&cparams);
- RETURN_FALSE;
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_exception,
+ "malformed host URL");
+ return;
}
}
-
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "Hosts is neither a string nor an array");
free_connparams(&cparams);
- RETURN_FALSE;
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_exception,
+ "Hosts is neither a string nor an array");
+ return;
}
make_params(&cparams);
@@ -266,8 +267,10 @@ void php_couchbase_create_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {{{ */
if (lcb_create(&handle, &create_options) != LCB_SUCCESS) {
free_connparams(&cparams);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create libcouchbase instance");
- RETURN_FALSE;
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_lcb_exception,
+ "Failed to create libcouchbase instance");
+ return;
}
@@ -314,7 +317,9 @@ void php_couchbase_create_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {{{ */
le.ptr = couchbase_res;
if (zend_hash_update(&EG(persistent_list), hashed_key, hashed_key_len + 1, (void *) &le, sizeof(zend_rsrc_list_entry), NULL) == FAILURE) {
free_connparams(&cparams);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to store persistent link");
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_exception,
+ "Failed to store persistent link");
}
efree(hashed_key);
}
View
61 error.c
@@ -0,0 +1,61 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP Version 5 |
+ +----------------------------------------------------------------------+
+ | Copyright 2012 Couchbase, Inc. |
+ +----------------------------------------------------------------------+
+ | Licensed under the Apache License, Version 2.0 (the "License"); |
+ | you may not use this file except in compliance with the License. |
+ | You may obtain a copy of the License at |
+ | http://www.apache.org/licenses/LICENSE-2.0 |
+ | Unless required by applicable law or agreed to in writing, software |
+ | distributed under the License is distributed on an "AS IS" BASIS, |
+ | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
+ | implied. See the License for the specific language governing |
+ | permissions and limitations under the License. |
+ +----------------------------------------------------------------------+
+ | Author: Trond Norbye <trond.norbye@gmail.org> |
+ +----------------------------------------------------------------------+
+*/
+#include "internal.h"
+
+static const char errmsg[] = "Failed to allocate buffer for error message";
+
+PHP_COUCHBASE_LOCAL
+void couchbase_report_error(INTERNAL_FUNCTION_PARAMETERS, int oo,
+ zend_class_entry *exception, const char *fmt, ...)
+{
+ /* No error messages should be longer than this! */
+#define MAX_ERROR_MSG_LENGTH 4096
+ char *msg = malloc(MAX_ERROR_MSG_LENGTH);
+ va_list ap;
+
+ if (msg == NULL) {
+ msg = (char *)errmsg;
+ } else {
+ /* format the error message */
+ va_start(ap, fmt);
+ vsnprintf(msg, MAX_ERROR_MSG_LENGTH, fmt, ap);
+ va_end(ap);
+ }
+
+ if (oo) {
+ zend_throw_exception(exception, msg, 0 TSRMLS_CC);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", msg);
+ RETVAL_FALSE;
+ }
+
+ if (msg != (char *)errmsg) {
+ free(msg);
+ }
+}
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: noet sw=4 ts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4
+ */
View
51 flush.c
@@ -97,21 +97,14 @@ void php_couchbase_flush_impl(INTERNAL_FUNCTION_PARAMETERS, int oo)
if (rc != LCB_SUCCESS) {
/* An error occured occurred on libcouchbase level */
- char errmsg[256];
- snprintf(errmsg, sizeof(errmsg), "Failed to flush bucket: %s",
- lcb_strerror(instance, rc));
-
if (ctx.payload) {
efree(ctx.payload);
}
- if (oo) {
- zend_throw_exception(cb_lcb_exception, errmsg, 0 TSRMLS_CC);
- return ;
- } else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errmsg);
- RETURN_FALSE;
- }
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_lcb_exception, "Failed to flush bucket: %s",
+ lcb_strerror(instance, rc));
+ return;
}
switch (ctx.status) {
@@ -121,44 +114,26 @@ void php_couchbase_flush_impl(INTERNAL_FUNCTION_PARAMETERS, int oo)
RETURN_TRUE;
case LCB_HTTP_STATUS_UNAUTHORIZED:
- if (oo) {
- zend_throw_exception(cb_auth_exception, "Incorrect credentials",
- 0 TSRMLS_CC);
- } else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "Incorrect credentials");
- RETVAL_FALSE;
- }
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_auth_exception, "Incorrect credentials");
break;
default:
if (ctx.payload == NULL) {
- char message[200];
- sprintf(message, "{\"errors\":{\"http response\": %d }}",
- (int)ctx.status);
- if (oo) {
- zend_throw_exception(cb_server_exception, message, 0 TSRMLS_CC);
- } else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "%s", message);
- RETVAL_FALSE;
- }
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_server_exception,
+ "{\"errors\":{\"http response\": %d }}",
+ (int)ctx.status);
} else {
- if (oo) {
- zend_throw_exception(cb_server_exception, ctx.payload,
- 0 TSRMLS_CC);
- } else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "%s", ctx.payload);
- RETVAL_FALSE;
- }
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_server_exception,
+ ctx.payload);
}
}
if (ctx.payload != NULL) {
efree(ctx.payload);
}
- /* exception already thrown */
}
/*
View
31 get.c
@@ -302,20 +302,23 @@ void php_couchbase_get_impl(INTERNAL_FUNCTION_PARAMETERS,
zval_dtor(return_value);
}
efree(ctx);
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "Failed to schedule get request: %s",
- lcb_strerror(couchbase_res->handle, retval));
- RETURN_FALSE;
+
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_lcb_exception,
+ "Failed to schedule get request: %s",
+ lcb_strerror(couchbase_res->handle, retval));
+ return;
}
couchbase_res->seqno += nkey;
pcbc_start_loop(couchbase_res);
if (LCB_SUCCESS != ctx->res->rc) {
if (LCB_KEY_ENOENT != ctx->res->rc) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "Failed to get a value from server: %s",
- lcb_strerror(couchbase_res->handle,
- ctx->res->rc));
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_lcb_exception,
+ "Failed to get a value from server: %s",
+ lcb_strerror(couchbase_res->handle,
+ ctx->res->rc));
}
}
efree(ctx);
@@ -372,7 +375,9 @@ void php_couchbase_get_delayed_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {{{
if (callback && Z_TYPE_P(callback) != IS_NULL
&& !zend_is_callable(callback, 0, NULL)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "third argument is expected to be a valid callback");
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_exception,
+ "third argument is expected to be a valid callback");
return;
}
#endif
@@ -458,9 +463,11 @@ void php_couchbase_get_delayed_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {{{
efree(keys);
efree(klens);
efree(ctx);
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "Failed to schedule delayed get request: %s", lcb_strerror(couchbase_res->handle, retval));
- RETURN_FALSE;
+ couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
+ cb_lcb_exception,
+ "Failed to schedule delayed get request: %s",
+ lcb_strerror(couchbase_res->handle, retval));
+ return;
}
couchbase_res->async = 1;
couchbase_res->async_ctx = ctx;
Oops, something went wrong.

0 comments on commit cf70968

Please sign in to comment.