Skip to content

Commit

Permalink
mi api: simplify init_mi_error(); add init_mi_error_extra()
Browse files Browse the repository at this point in the history
  • Loading branch information
rvlad-patrascu committed Jan 10, 2019
1 parent 3c573f0 commit fe8fcb0
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 31 deletions.
10 changes: 5 additions & 5 deletions evi/event_interface.c
Expand Up @@ -449,7 +449,7 @@ static mi_response_t *mi_event_subscribe(const mi_params_t *params, unsigned int

ret = evi_event_subscribe(event_name, transport_sock, expire, 1);
if (ret < 0)
return init_mi_error(400, MI_SSTR("Bad parameter value"), 0, 0);
return init_mi_error(400, MI_SSTR("Bad parameter value"));

return ret ? init_mi_result_ok() : 0;
}
Expand All @@ -469,7 +469,7 @@ mi_response_t *w_mi_event_subscribe_1(const mi_params_t *params,
return init_mi_param_error();

if (expire < 0)
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("Negative expire value"));

Expand Down Expand Up @@ -715,7 +715,7 @@ mi_response_t *w_mi_subscribers_list_1(const mi_params_t *params,

event = evi_get_event(&event_s);
if (!event)
return init_mi_error(404, MI_SSTR("Event not published"), 0, 0);
return init_mi_error(404, MI_SSTR("Event not published"));

return mi_subscribers_list(event, NULL);
}
Expand All @@ -733,14 +733,14 @@ mi_response_t *w_mi_subscribers_list_2(const mi_params_t *params,

event = evi_get_event(&event_s);
if (!event)
return init_mi_error(404, MI_SSTR("Event not published"), 0, 0);
return init_mi_error(404, MI_SSTR("Event not published"));

if (get_mi_string_param(params, "socket", &subs_s.s, &subs_s.len) < 0)
return init_mi_param_error();

subs = evi_get_subscriber(event, subs_s);
if (!subs)
return init_mi_error(404, MI_SSTR("Subscriber does not exist"), 0, 0);
return init_mi_error(404, MI_SSTR("Subscriber does not exist"));

return mi_subscribers_list(event, subs);
}
6 changes: 3 additions & 3 deletions mi/item.c
Expand Up @@ -132,7 +132,7 @@ mi_response_t *init_mi_result_null(void)
return _init_mi_result(&item, cJSON_NULL, 0, NULL, 0);
}

mi_response_t *init_mi_error(int code, const char *msg, int msg_len,
mi_response_t *init_mi_error_extra(int code, const char *msg, int msg_len,
const char *details, int details_len)
{
mi_response_t *res;
Expand Down Expand Up @@ -497,10 +497,10 @@ mi_response_t *init_mi_param_error(void)
switch (param_err_type) {
case -2:
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG), NULL, 0);
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG));
case -1:
default:
return init_mi_error(JSONRPC_SERVER_ERR_CODE,
MI_SSTR(JSONRPC_SERVER_ERR_MSG), NULL, 0);
MI_SSTR(JSONRPC_SERVER_ERR_MSG));
}
}
12 changes: 10 additions & 2 deletions mi/item.h
Expand Up @@ -96,11 +96,19 @@ mi_response_t *init_mi_result_null(void);

/* Initializes an error MI Response
* with the given error code and message.
* Additional error details are optional.
* Accepts a string of additional error details.
*/
mi_response_t *init_mi_error(int code, const char *msg, int msg_len,
mi_response_t *init_mi_error_extra(int code, const char *msg, int msg_len,
const char *details, int details_len);

/* Initializes an error MI Response
* with the given error code and message.
*/
static inline mi_response_t *init_mi_error(int code, const char *msg, int msg_len)
{
return init_mi_error_extra(code, msg, msg_len, NULL, 0);
}

/* Frees a MI Reponse
*/
void free_mi_response(mi_response_t *response);
Expand Down
4 changes: 2 additions & 2 deletions mi/mi.c
Expand Up @@ -341,7 +341,7 @@ static mi_response_t *build_err_resp(int code, const char *msg, int msg_len,
{
mi_response_t *err_resp;

err_resp = init_mi_error(code, msg, msg_len, details, details_len);
err_resp = init_mi_error_extra(code, msg, msg_len, details, details_len);
if (!err_resp)
LM_ERR("Failed to build MI error response object\n");

Expand Down Expand Up @@ -548,7 +548,7 @@ mi_response_t *w_mi_help_1(const mi_params_t *params,
/* search the command */
cmd = lookup_mi_cmd(cmd_s.s, cmd_s.len);
if (!cmd)
return init_mi_error(404, MI_SSTR(MI_UNKNOWN_CMD), 0, 0);
return init_mi_error(404, MI_SSTR(MI_UNKNOWN_CMD));

resp = init_mi_result_object(&resp_obj);
if (!resp)
Expand Down
30 changes: 15 additions & 15 deletions mi/mi_core.c
Expand Up @@ -290,7 +290,7 @@ mi_response_t *mi_log_level(const mi_params_t *params, pid_t pid)
i = get_process_ID_by_PID(pid);
if (i == -1) {
free_mi_response(resp);
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG), MI_SSTR("Bad PID"));
}

Expand Down Expand Up @@ -375,29 +375,29 @@ static mi_response_t *mi_cachestore(const mi_params_t *params, unsigned int exp
return init_mi_param_error();

if (!mc_system.s || mc_system.len == 0)
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("Empty memory cache id"));

if (get_mi_string_param(params, "attr", &attr.s, &attr.len) < 0)
return init_mi_param_error();

if (!attr.s || attr.len == 0)
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("Empty attribute name"));

if (get_mi_string_param(params, "value", &value.s, &value.len) < 0)
return init_mi_param_error();

if (!value.s || value.len == 0)
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("Empty value"));

if (cachedb_store(&mc_system, &attr, &value, expire) < 0) {
LM_ERR("cachedb_store command failed\n");
return init_mi_error(500, MI_SSTR("Cache store command failed"), 0, 0);
return init_mi_error(500, MI_SSTR("Cache store command failed"));
}

return init_mi_result_ok();
Expand All @@ -418,7 +418,7 @@ static mi_response_t *w_cachestore_1(const mi_params_t *params,
return init_mi_param_error();

if (expire < 0)
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("Negative expire value"));

Expand All @@ -440,23 +440,23 @@ static mi_response_t *mi_cachefetch(const mi_params_t *params,
return init_mi_param_error();

if (!mc_system.s || mc_system.len == 0)
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("Empty memory cache id"));

if (get_mi_string_param(params, "attr", &attr.s, &attr.len) < 0)
return init_mi_param_error();

if (!attr.s || attr.len == 0)
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("Empty attribute name"));

ret = cachedb_fetch(&mc_system, &attr, &value);
if(ret== -1)
{
LM_ERR("cachedb_fetch command failed\n");
return init_mi_error(500, MI_SSTR("Cache fetch command failed"), 0, 0);
return init_mi_error(500, MI_SSTR("Cache fetch command failed"));
}

if(ret == -2 || value.s == 0 || value.len == 0)
Expand Down Expand Up @@ -495,22 +495,22 @@ static mi_response_t *mi_cacheremove(const mi_params_t *params,
return init_mi_param_error();

if (!mc_system.s || mc_system.len == 0)
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("Empty memory cache id"));

if (get_mi_string_param(params, "attr", &attr.s, &attr.len) < 0)
return init_mi_param_error();

if (!attr.s || attr.len == 0)
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("Empty attribute name"));

if(cachedb_remove(&mc_system, &attr)< 0)
{
LM_ERR("cachedb_remove command failed\n");
return init_mi_error(500, MI_SSTR("Cache remove command failed"), 0, 0);
return init_mi_error(500, MI_SSTR("Cache remove command failed"));
}

return init_mi_result_ok();
Expand Down Expand Up @@ -547,14 +547,14 @@ static mi_response_t *mi_mem_pkg_dump(const mi_params_t *params, int llevel)
/* convert pid to OpenSIPS id */
i = get_process_ID_by_PID(pid);
if (i == -1)
return init_mi_error(404, MI_SSTR("Process not found"), 0, 0);
return init_mi_error(404, MI_SSTR("Process not found"));

if (IPC_FD_WRITE(i)<=0)
return init_mi_error(500, MI_SSTR("Process does not support mem dump"),0,0);
return init_mi_error(500, MI_SSTR("Process does not support mem dump"));

if (ipc_send_rpc( i, rpc_do_pkg_dump, (void*)(long)llevel)<0) {
LM_ERR("failed to trigger pkg dump for process %d\n", i);
return init_mi_error(500, MI_SSTR("Internal error"),0,0);
return init_mi_error(500, MI_SSTR("Internal error"));
}

return init_mi_result_ok();
Expand Down
2 changes: 1 addition & 1 deletion net/proto_tcp/proto_tcp.c
Expand Up @@ -1238,7 +1238,7 @@ static mi_response_t *w_tcp_trace_mi_1(const mi_params_t *params,
*trace_is_on = 0;
return init_mi_result_ok();
} else {
return init_mi_error(JSONRPC_INVAL_PARAMS_CODE,
return init_mi_error_extra(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR("trace_mode should be 'on' or 'off'"));
}
Expand Down
6 changes: 3 additions & 3 deletions statistics.c
Expand Up @@ -815,7 +815,7 @@ static mi_response_t *mi_get_stats(const mi_params_t *params,

if (!found) {
free_mi_response(resp);
return init_mi_error(404, MI_SSTR("Statistics Not Found"), 0, 0);
return init_mi_error(404, MI_SSTR("Statistics Not Found"));
}

return resp;
Expand Down Expand Up @@ -897,7 +897,7 @@ static mi_response_t *w_mi_list_stats_1(const mi_params_t *params,

if (!found) {
free_mi_response(resp);
return init_mi_error(404, MI_SSTR("Statistics Not Found"), 0, 0);
return init_mi_error(404, MI_SSTR("Statistics Not Found"));
}

return resp;
Expand Down Expand Up @@ -943,7 +943,7 @@ static mi_response_t *mi_reset_stats(const mi_params_t *params,

if (!found) {
free_mi_response(resp);
return init_mi_error(404, MI_SSTR("Statistics Not Found"), 0, 0);
return init_mi_error(404, MI_SSTR("Statistics Not Found"));
}

return resp_obj;
Expand Down

0 comments on commit fe8fcb0

Please sign in to comment.