Skip to content

Commit

Permalink
add swoole_set_last_error/swoole_get_last_error
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed May 12, 2020
1 parent 117f6c4 commit e25f262
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core-tests/src/core/log.cpp
Expand Up @@ -58,7 +58,7 @@ TEST(log, date_format_long_string)
int retval = swLog_set_date_format(str->str);

ASSERT_EQ(retval, SW_ERR);
ASSERT_EQ(SwooleG.error, SW_ERROR_INVALID_PARAMS);
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_INVALID_PARAMS);
}

TEST(log, date_with_microseconds)
Expand Down
3 changes: 0 additions & 3 deletions include/error.h
Expand Up @@ -153,7 +153,4 @@ enum swErrorCode
SW_ERROR_END
};

const char* swoole_strerror(int code);
void swoole_throw_error(int code);

#endif /* SW_ERRNO_H_ */
13 changes: 13 additions & 0 deletions include/swoole.h
Expand Up @@ -2590,6 +2590,19 @@ extern __thread swThreadGlobal_t SwooleTG; //Thread Global Variable

#define SW_CPU_NUM (SwooleG.cpu_num)

static sw_inline void swoole_set_last_error(int error)
{
SwooleG.error = error;
}

static sw_inline int swoole_get_last_error()
{
return SwooleG.error;
}

SW_API const char* swoole_strerror(int code);
SW_API void swoole_throw_error(int code);

//-----------------------------------------------
//OS Feature
#if defined(HAVE_KQUEUE) || !defined(HAVE_SENDFILE)
Expand Down
2 changes: 1 addition & 1 deletion swoole.cc
Expand Up @@ -855,7 +855,7 @@ static PHP_FUNCTION(swoole_hashcode)

PHP_FUNCTION(swoole_last_error)
{
RETURN_LONG(SwooleG.error);
RETURN_LONG(swoole_get_last_error());
}

PHP_FUNCTION(swoole_cpu_num)
Expand Down
12 changes: 6 additions & 6 deletions swoole_server.cc
Expand Up @@ -1872,7 +1872,7 @@ void php_swoole_onClose(swServer *serv, swDataHead *info)
{
php_coro_context *context = coros_list->front();
coros_list->pop_front();
SwooleG.error = ECONNRESET;
swoole_set_last_error(ECONNRESET);
zval_ptr_dtor(&context->coro_params);
ZVAL_NULL(&context->coro_params);
php_swoole_server_send_resume(serv, context, info->fd);
Expand Down Expand Up @@ -1923,7 +1923,7 @@ static void php_swoole_onSendTimeout(swTimer *timer, swTimer_node *tnode)
zval result;
zval *retval = NULL;

SwooleG.error = ETIMEDOUT;
swoole_set_last_error(ETIMEDOUT);
ZVAL_FALSE(&result);

int fd = (int) (long) context->private_data;
Expand Down Expand Up @@ -1977,7 +1977,7 @@ static enum swReturn_code php_swoole_server_send_resume(swServer *serv, php_coro
goto _fail;
}
int ret = serv->send(serv, fd, data, length);
if (ret < 0 && SwooleG.error == SW_ERROR_OUTPUT_SEND_YIELD && serv->send_yield)
if (ret < 0 && swoole_get_last_error() == SW_ERROR_OUTPUT_SEND_YIELD && serv->send_yield)
{
return SW_CONTINUE;
}
Expand Down Expand Up @@ -3149,7 +3149,7 @@ static PHP_METHOD(swoole_server, send)
RETURN_FALSE;
}
ret = serv->send(serv, fd, data, length);
if (ret < 0 && SwooleG.error == SW_ERROR_OUTPUT_SEND_YIELD)
if (ret < 0 && swoole_get_last_error() == SW_ERROR_OUTPUT_SEND_YIELD)
{
zval_add_ref(zdata);
php_swoole_server_send_yield(serv, fd, zdata, return_value);
Expand Down Expand Up @@ -3657,7 +3657,7 @@ static PHP_METHOD(swoole_server, taskWaitMulti)

if (n_task == 0)
{
SwooleG.error = SW_ERROR_TASK_DISPATCH_FAIL;
swoole_set_last_error(SW_ERROR_TASK_DISPATCH_FAIL);
RETURN_FALSE;
}

Expand Down Expand Up @@ -3803,7 +3803,7 @@ static PHP_METHOD(swoole_server, taskCo)

if (n_task == 0)
{
SwooleG.error = SW_ERROR_TASK_DISPATCH_FAIL;
swoole_set_last_error(SW_ERROR_TASK_DISPATCH_FAIL);
RETURN_FALSE;
}

Expand Down

0 comments on commit e25f262

Please sign in to comment.