Add missing error handling to Avro file handling

Some of the JSON errors weren't handled which could cause problems when a
malformed schema definition is read.

Also added more error messages for situations when opening of the files
fails.
This commit is contained in:
Markus Mäkelä
2017-01-26 10:09:48 +02:00
parent c2b03100f0
commit 84040be182
3 changed files with 68 additions and 43 deletions

View File

@ -126,7 +126,9 @@ MAXAVRO_SCHEMA* maxavro_schema_alloc(const char* json)
if (schema) if (schema)
{ {
json_t *field_arr = NULL; json_t *field_arr = NULL;
json_unpack(schema, "{s:o}", "fields", &field_arr);
if (json_unpack(schema, "{s:o}", "fields", &field_arr) == 0)
{
size_t arr_size = json_array_size(field_arr); size_t arr_size = json_array_size(field_arr);
rval->fields = malloc(sizeof(MAXAVRO_SCHEMA_FIELD) * arr_size); rval->fields = malloc(sizeof(MAXAVRO_SCHEMA_FIELD) * arr_size);
rval->num_fields = arr_size; rval->num_fields = arr_size;
@ -137,10 +139,22 @@ MAXAVRO_SCHEMA* maxavro_schema_alloc(const char* json)
char *key; char *key;
json_t *value_obj; json_t *value_obj;
json_unpack(object, "{s:s s:o}", "name", &key, "type", &value_obj); if (json_unpack(object, "{s:s s:o}", "name", &key, "type", &value_obj) == 0)
{
rval->fields[i].name = strdup(key); rval->fields[i].name = strdup(key);
rval->fields[i].type = unpack_to_type(value_obj, &rval->fields[i]); rval->fields[i].type = unpack_to_type(value_obj, &rval->fields[i]);
} }
else
{
MXS_ERROR("Failed to unpack JSON Object \"name\": %s", json);
}
}
}
else
{
MXS_ERROR("Failed to unpack JSON Object \"fields\": %s", json);
}
json_decref(schema); json_decref(schema);
} }

View File

@ -775,13 +775,18 @@ static bool avro_client_stream_data(AVRO_CLIENT *client)
char filename[PATH_MAX + 1]; char filename[PATH_MAX + 1];
snprintf(filename, PATH_MAX, "%s/%s", router->avrodir, client->avro_binfile); snprintf(filename, PATH_MAX, "%s/%s", router->avrodir, client->avro_binfile);
bool ok = true;
spinlock_acquire(&client->file_lock); spinlock_acquire(&client->file_lock);
if (client->file_handle == NULL) if (client->file_handle == NULL &&
(client->file_handle = maxavro_file_open(filename)) == NULL)
{ {
client->file_handle = maxavro_file_open(filename); ok = false;
} }
spinlock_release(&client->file_lock); spinlock_release(&client->file_lock);
if (ok)
{
switch (client->format) switch (client->format)
{ {
case AVRO_FORMAT_JSON: case AVRO_FORMAT_JSON:
@ -818,6 +823,7 @@ static bool avro_client_stream_data(AVRO_CLIENT *client)
/* may be just use client->avro_file->records_read and remove this var */ /* may be just use client->avro_file->records_read and remove this var */
client->last_sent_pos = client->avro_file.records_read; client->last_sent_pos = client->avro_file.records_read;
} }
}
else else
{ {
fprintf(stderr, "No file specified\n"); fprintf(stderr, "No file specified\n");

View File

@ -72,6 +72,7 @@ int index_query_cb(void *data, int rows, char** values, char** names)
void avro_index_file(AVRO_INSTANCE *router, const char* filename) void avro_index_file(AVRO_INSTANCE *router, const char* filename)
{ {
MAXAVRO_FILE *file = maxavro_file_open(filename); MAXAVRO_FILE *file = maxavro_file_open(filename);
if (file) if (file)
{ {
char *name = strrchr(filename, '/'); char *name = strrchr(filename, '/');
@ -165,6 +166,10 @@ void avro_index_file(AVRO_INSTANCE *router, const char* filename)
maxavro_file_close(file); maxavro_file_close(file);
} }
else
{
MXS_ERROR("Failed to open file '%s' when generating file index.", filename);
}
} }
/** /**