浏览代码

jsonrpcs: proper propagation of rpc fault code and message

Daniel-Constantin Mierla 8 年之前
父节点
当前提交
9364b16914
共有 2 个文件被更改,包括 22 次插入5 次删除
  1. 21 5
      src/modules/jsonrpcs/jsonrpcs_mod.c
  2. 1 0
      src/modules/jsonrpcs/jsonrpcs_mod.h

+ 21 - 5
src/modules/jsonrpcs/jsonrpcs_mod.c

@@ -299,13 +299,23 @@ static void jsonrpc_fault(jsonrpc_ctx_t* ctx, int code, char* fmt, ...)
 
 	jsonrpc_delayed_reply_ctx_init(ctx);
 
-	ctx->http_code = code;
+	if(code <= 100) {
+		ctx->http_code = 500;
+	} else {
+		ctx->http_code = code;
+	}
 	va_start(ap, fmt);
 	vsnprintf(jsonrpc_error_buf, JSONRPC_ERROR_REASON_BUF_LEN, fmt, ap);
 	va_end(ap);
-	ctx->http_text.len = strlen(jsonrpc_error_buf);
+	ctx->error_text.len = strlen(jsonrpc_error_buf);
+	ctx->error_text.s = jsonrpc_error_buf;
+	ctx->http_text.len = ctx->error_text.len;
 	ctx->http_text.s = jsonrpc_error_buf;
-	if(ctx->error_code == 0) ctx->error_code = -32000;
+	if(code == 0) {
+		ctx->error_code = -32000;
+	} else {
+		ctx->error_code = code;
+	}
 
 	return;
 }
@@ -348,8 +358,14 @@ static int jsonrpc_send(jsonrpc_ctx_t* ctx)
 					_jsonrpc_error_table[i].text.s,
 					_jsonrpc_error_table[i].text.len);
 			} else {
-				srjson_AddStrStrToObject(ctx->jrpl, nj,
-					"message", 7, "Unexpected Error", 16);
+				if(ctx->error_text.len>0) {
+					srjson_AddStrStrToObject(ctx->jrpl, nj,
+							"message", 7,
+							ctx->error_text.s, ctx->error_text.len);
+				} else {
+					srjson_AddStrStrToObject(ctx->jrpl, nj,
+							"message", 7, "Unexpected Error", 16);
+				}
 			}
 			srjson_AddItemToObject(ctx->jrpl, ctx->jrpl->root, "error", nj);
 		}

+ 1 - 0
src/modules/jsonrpcs/jsonrpcs_mod.h

@@ -50,6 +50,7 @@ typedef struct jsonrpc_ctx {
 	srjson_t *rpl_node;    /**< Pointer to crt node in json reply doc */
 	int reply_sent;        /**< Flag set if the json reply was sent */
 	int error_code;        /**< Json error code */
+	str error_text;        /**< Json error text */
 	int http_code;         /**< http reply code */
 	str http_text;         /**< http reply reason text */
 	int transport;         /**< RPC transport */