-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrv_jsonrpc_error.c
127 lines (102 loc) · 3.67 KB
/
rv_jsonrpc_error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @file rv_jsonrpc_error.c
* @note Copyright (C) 2017, ALC NetworX GmbH
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "rv_jsonrpc.h"
#include "print.h"
#include <stdlib.h>
#include <string.h>
static char *rv_jsonrpc_strerror(JsonRpcError error, char *buffer, unsigned buffer_len) {
if(!buffer || (buffer_len == 0)) {
return NULL;
}
switch(error) {
case eRvInvalidObject:
strncpy(buffer, "Invalid JSON object", buffer_len);
break;
case eRvUnsupportedEnc:
strncpy(buffer, "Unsupported JSONRPC encoding", buffer_len);
break;
case eRvInvalidEnc:
strncpy(buffer, "Invalid JSONRPC encoding", buffer_len);
break;
case eRvInvalidMessage:
strncpy(buffer, "Invalid JSONRPC message", buffer_len);
break;
case eRvUnknownCommand:
strncpy(buffer, "Unknown command", buffer_len);
break;
case eRvInvalidValue:
strncpy(buffer, "Invalid value", buffer_len);
break;
case eRvApplicationErr:
strncpy(buffer, "General application error", buffer_len);
break;
case eRvSystemErr:
strncpy(buffer, "General system error", buffer_len);
break;
case eRvTransportErr:
strncpy(buffer, "General transport error", buffer_len);
break;
default:
strncpy(buffer, "Unknown error", buffer_len);
}
return buffer;
}
// steals reference of json objects
RvJsonRpcError* rv_jsonrpc_error_ctor(void *mem, int code, const char *message, json_t *data) {
RvJsonRpcError *self = (RvJsonRpcError*)mem;
if(!self) {
// no memory given, we must allocate it
self = (RvJsonRpcError*)calloc(1, sizeof(RvJsonRpcError));
if(!self) {
pr_emerg("Could not allocate memory for JSON RPC request!");
return NULL;
}
}
memset(self, 0, sizeof(RvJsonRpcError));
self->code = code;
if(!message) {
rv_jsonrpc_strerror(self->code, self->message, JSONRPC_ERRORMSG_MAXLEN);
} else {
strncpy(self->message, message, strnlen(message, JSONRPC_ERRORMSG_MAXLEN));
}
if(data) {
self->data = json_deep_copy(data);
}
return self;
}
RvJsonRpcError* rv_jsonrpc_error_dtor(RvJsonRpcError *self) {
if(!self) {
return NULL;
}
json_decref(self->data);
memset(self, 0, sizeof(RvJsonRpcError));
return self;
}
json_t *rv_jsonrpc_error_encode(RvJsonRpcError *self) {
json_t *result = json_object();
json_object_set_new(result, "code", json_integer(self->code));
json_object_set_new(result, "message", json_string(self->message));
if(self->data) {
json_object_set_new(result, "data", json_incref(self->data));
}
return result;
}
int *rv_jsonrpc_error_decode(RvJsonRpcError *self, json_t *object) {
return 0;
}