aboutsummaryrefslogtreecommitdiff
path: root/src/jsonapi/jsonapi_error.c
blob: 8ce71d26f90a56f48ca0db6b85e999aad03cd5d2 (plain) (blame)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "platform.h"
#include "gnunet_jsonapi_lib.h"
#include "jsonapi_objects.h"

/**
 * Parse json to error object
 *
 * @param err_json JSON object
 * @param[out] err error object
 * @return GNUNET_OK on success
 */
int
GNUNET_JSONAPI_json_to_error (json_t *err_json,
                              struct GNUNET_JSONAPI_Error **err)
{
  struct GNUNET_JSON_Specification jsonapispecerror[] = {
    GNUNET_JSON_spec_jsonapi_error (err),
    GNUNET_JSON_spec_end()
  };
  return GNUNET_JSON_parse (err_json, jsonapispecerror,
                            NULL, NULL);
}

/**
 * Serialze jsonapi errors
 *
 * @param data the JSON API errors
 * @param result where to store the result
 * @return GNUNET_SYSERR on error else GNUNET_OK
 */
int
GNUNET_JSONAPI_error_to_json (const struct GNUNET_JSONAPI_Error *err,
                              json_t **result)
{
  *result = json_object ();

  if ((NULL != err->id) &&
      (0 != json_object_set_new (*result,
                                 GNUNET_JSONAPI_KEY_ID,
                                 json_string (err->id))))
    return GNUNET_SYSERR;
  if ((NULL != err->status) &&
      (0 != json_object_set_new (*result,
                                 GNUNET_JSONAPI_KEY_STATUS,
                                 json_string (err->status))))
    return GNUNET_SYSERR;
  if ((NULL != err->code) &&
      (0 != json_object_set_new (*result,
                                 GNUNET_JSONAPI_KEY_CODE,
                                 json_string (err->code))))
    return GNUNET_SYSERR;

  if ((NULL != err->title) &&
      (0 != json_object_set_new (*result,
                                 GNUNET_JSONAPI_KEY_TITLE,
                                 json_string (err->title))))
    return GNUNET_SYSERR;
  if ((NULL != err->detail) &&
      (0 != json_object_set_new (*result,
                                 GNUNET_JSONAPI_KEY_DETAIL,
                                 json_string (err->detail))))
    return GNUNET_SYSERR;
  if ((NULL != err->source) &&
      (0 != json_object_set_new (*result,
                                 GNUNET_JSONAPI_KEY_SOURCE,
                                 err->source)))
    return GNUNET_SYSERR;
  if ((NULL != err->links) &&
      (0 != json_object_set_new (*result,
                                 GNUNET_JSONAPI_KEY_LINKS,
                                 err->links)))
    return GNUNET_SYSERR;
  if ((NULL != err->meta) &&
      (0 != json_object_set_new (*result,
                                 GNUNET_JSONAPI_KEY_META,
                                 err->meta)))
    return GNUNET_SYSERR;
  return GNUNET_OK;
}


/**
 * Parse given JSON object to jsonapi document.
 *
 * @param cls closure, NULL
 * @param root the json object representing data
 * @param[out] spec where to write the data
 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
 */
static int
parse_jsonapierror (void *cls,
                     json_t *root,
                     struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_JSONAPI_Error *result;
  json_t *pos;
  
  GNUNET_assert (NULL != root);
  result = GNUNET_new (struct GNUNET_JSONAPI_Error);
  pos = json_object_get (root, GNUNET_JSONAPI_KEY_ID);
  if (json_is_string (pos))
    result->id = GNUNET_strdup (json_string_value (pos));
  
  pos = json_object_get (root, GNUNET_JSONAPI_KEY_LINKS);
  if (json_is_object (pos))
    result->links = json_deep_copy (pos);
  
  pos = json_object_get (root, GNUNET_JSONAPI_KEY_STATUS);
  if (json_is_string (pos))
    result->status = GNUNET_strdup (json_string_value (pos));

  pos = json_object_get (root, GNUNET_JSONAPI_KEY_CODE);
  if (json_is_string (pos))
    result->code = GNUNET_strdup (json_string_value (pos));

  pos = json_object_get (root, GNUNET_JSONAPI_KEY_TITLE);
  if (json_is_string (pos))
    result->title = GNUNET_strdup (json_string_value (pos));

  pos = json_object_get (root, GNUNET_JSONAPI_KEY_DETAIL);
  if (json_is_string (pos))
    result->detail = GNUNET_strdup (json_string_value (pos));

  pos = json_object_get (root, GNUNET_JSONAPI_KEY_SOURCE);
  if (json_is_object (pos))
    result->source = json_deep_copy (pos);
  pos = json_object_get (root, GNUNET_JSONAPI_KEY_META);
  if (json_is_object (pos))
    result->meta = json_deep_copy (pos);
  *(struct GNUNET_JSONAPI_Error **) spec->ptr = result;
  return GNUNET_OK;
}

/**
 * Create a JSON API error
 *
 * @param res the JSON error
 */
struct GNUNET_JSONAPI_Error*
GNUNET_JSONAPI_error_new (const char *id,
                          const char *status,
                          const char *code,
                          const char *title,
                          const char *detail,
                          json_t *links,
                          json_t *source,
                          json_t *meta)
{
  struct GNUNET_JSONAPI_Error *error;
  error = GNUNET_new (struct GNUNET_JSONAPI_Error);

  if (NULL != id)
    error->id = GNUNET_strdup (id);
  if (NULL != status)
    error->status = GNUNET_strdup (status);
  if (NULL != code)
    error->code = GNUNET_strdup (code);
  if (NULL != title)
    error->title = GNUNET_strdup (title);
  if (NULL != detail)
    error->detail = GNUNET_strdup (detail);
  if (NULL != links)
    error->links = json_deep_copy (links);
  if (NULL != source)
    error->source = json_deep_copy (source);
  if (NULL != meta)
    error->meta = json_deep_copy (meta);
  return error;
}
/**
 * Delete a JSON API error
 *
 * @param res the JSON error
 */
void
GNUNET_JSONAPI_error_delete (struct GNUNET_JSONAPI_Error *error)
{
  GNUNET_assert (NULL != error);

  if (NULL != error->id)
    GNUNET_free (error->id);
  if (NULL != error->status)
    GNUNET_free (error->status);
  if (NULL != error->code)
    GNUNET_free (error->code);
  if (NULL != error->title)
    GNUNET_free (error->title);
  if (NULL != error->detail)
    GNUNET_free (error->detail);
  if (NULL != error->links)
    json_decref (error->links);
  if (NULL != error->source)
    json_decref (error->source);
  if (NULL != error->meta)
    json_decref (error->meta);
  GNUNET_free (error);
}



/**
 * Cleanup data left from parsing RSA public key.
 *
 * @param cls closure, NULL
 * @param[out] spec where to free the data
 */
static void
clean_jsonapierror (void *cls,
                     struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_JSONAPI_Error **jsonapi_obj;
  jsonapi_obj = (struct GNUNET_JSONAPI_Error **) spec->ptr;
  if (NULL != *jsonapi_obj)
  {
    GNUNET_JSONAPI_error_delete (*jsonapi_obj);
    *jsonapi_obj = NULL;
  }
}
/**
 * JSON object.
 *
 * @param name name of the JSON field
 * @param[out] jsonp where to store the JSON found under @a name
 */
struct GNUNET_JSON_Specification
GNUNET_JSON_spec_jsonapi_error (struct GNUNET_JSONAPI_Error **jsonapi_object)
{
  struct GNUNET_JSON_Specification ret = {
    .parser = &parse_jsonapierror,
    .cleaner = &clean_jsonapierror,
    .cls = NULL,
    .field = NULL,
    .ptr = jsonapi_object,
    .ptr_size = 0,
    .size_ptr = NULL
  };
  *jsonapi_object = NULL;
  return ret;
}