add error handling and improve robustness
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/0f8c3e23 Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/0f8c3e23 Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/0f8c3e23 Branch: refs/heads/master Commit: 0f8c3e23f23f1728c0ec6675dc414b2e4bae3bdc Parents: e1959ad Author: SuJinpei <[email protected]> Authored: Tue Sep 26 14:01:30 2017 +0800 Committer: SuJinpei <[email protected]> Committed: Tue Sep 26 14:01:30 2017 +0800 ---------------------------------------------------------------------- core/conn/odb/src/JsonReader.c | 39 +++++++++++++++++++++++++++++++++++-- core/conn/odb/src/JsonReader.h | 23 ++++++++-------------- core/conn/odb/src/odb.c | 5 +++++ 3 files changed, 50 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/0f8c3e23/core/conn/odb/src/JsonReader.c ---------------------------------------------------------------------- diff --git a/core/conn/odb/src/JsonReader.c b/core/conn/odb/src/JsonReader.c index df65170..c621651 100755 --- a/core/conn/odb/src/JsonReader.c +++ b/core/conn/odb/src/JsonReader.c @@ -38,7 +38,12 @@ JsonReader *jsonReaderNew(const char *path) return NULL; } - strncpy(pJsonReader->jsonFileName, path, JSON_PARSER_MAX_FILE_NAME_LEN); + pJsonReader->jsonFileName = (char *)malloc(strlen(path) + 1); + if (!pJsonReader->jsonFileName) { + return NULL; + } + + strcpy(pJsonReader->jsonFileName, path); pJsonReader->nestDepth = 0; pJsonReader->state = JSON_STATE_START; pJsonReader->errorCode = JSON_SUCCESS; @@ -47,6 +52,33 @@ JsonReader *jsonReaderNew(const char *path) return pJsonReader; } +const char *jsonReaderErrorMessage(JsonReader *pJsonReader) { + switch (pJsonReader->errorCode) + { + case JSON_SUCCESS: + strcpy(pJsonReader->errorMessage, "success"); + break; + case JSON_ERROR_DEPTH: + sprintf(pJsonReader->errorMessage, "nested depth exceeding limit of %d in file:%s, line:%lu, pos:%lu", + JSON_PARSER_MAX_NESTED_NUM, pJsonReader->jsonFileName, pJsonReader->lineNum, pJsonReader->linePos); + break; + case JSON_ERROR_PARSE_EOF: + strcpy(pJsonReader->errorMessage, "parse to end of file"); + break; + case JSON_ERROR_STATE: + strcpy(pJsonReader->errorMessage, "error state"); + break; + case JSON_ERROR_BAD_FORMAT: + sprintf(pJsonReader->errorMessage, "unexpected character in file:%s, line:%lu, pos:%lu", + pJsonReader->jsonFileName, pJsonReader->lineNum, pJsonReader->linePos); + break; + default: + strcpy(pJsonReader->errorMessage, "unexpected error code"); + break; + } + return pJsonReader->errorMessage; +} + JsonReaderError jsonMoveCurrentCharPtr(JsonReader *pJsonReader) { if (pJsonReader->isBufReady) { @@ -59,7 +91,7 @@ JsonReaderError jsonMoveCurrentCharPtr(JsonReader *pJsonReader) pJsonReader->isBufReady = true; } else { - pJsonReader->errorCode = JSON_ERROR_PARSE_EOF; + return (pJsonReader->errorCode = JSON_ERROR_PARSE_EOF); } } @@ -559,5 +591,8 @@ JsonReaderError jsonParse(JsonReader *pJsonReader) void jsonReaderFree(JsonReader *pJsonReader) { fclose(pJsonReader->jsonFile); + if (pJsonReader->jsonFileName) { + free(pJsonReader->jsonFileName); + } free(pJsonReader); } http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/0f8c3e23/core/conn/odb/src/JsonReader.h ---------------------------------------------------------------------- diff --git a/core/conn/odb/src/JsonReader.h b/core/conn/odb/src/JsonReader.h index e1420f1..1ed7d03 100755 --- a/core/conn/odb/src/JsonReader.h +++ b/core/conn/odb/src/JsonReader.h @@ -29,7 +29,6 @@ #define JSON_PARSER_BUF_LEN 1024 #define JSON_PARSER_ERROR_MESSAGE_LEN 512 -#define JSON_PARSER_MAX_FILE_NAME_LEN 256 #define JSON_PARSER_MAX_NESTED_NUM 100 #ifndef bool @@ -65,20 +64,8 @@ typedef enum JsonReaderState_ JsonReaderState; enum JsonReaderError_ { JSON_SUCCESS, - JSON_CONTINUE, JSON_ERROR_DEPTH, JSON_ERROR_PARSE_EOF, - JSON_ERROR_PARSE_UNEXPECTED, - JSON_ERROR_PARSE_NULL, - JSON_ERROR_PARSE_BOOLEAN, - JSON_ERROR_PARSE_NUMBER, - JSON_ERROR_PARSE_ARRAY, - JSON_ERROR_PARSE_OBJECT_KEY_NAME, - JSON_ERROR_PARSE_OBJECT_KEY_SEP, - JSON_ERROR_PARSE_OBJECT_VALUE_SEP, - JSON_ERROR_PARSE_STRING, - JSON_ERROR_PARSE_COMMENT, - JSON_ERROR_SIZE, JSON_ERROR_STATE, JSON_ERROR_BAD_FORMAT }; @@ -98,10 +85,10 @@ struct JsonReader_ size_t nestDepth; JsonReaderState state; JsonReaderError errorCode; - char errorMsg[JSON_PARSER_ERROR_MESSAGE_LEN]; char buf[JSON_PARSER_BUF_LEN]; + char errorMessage[JSON_PARSER_ERROR_MESSAGE_LEN]; char *currentCharPtr; - char jsonFileName[JSON_PARSER_MAX_FILE_NAME_LEN]; + char *jsonFileName; size_t numberReadBuf; size_t lineNum; size_t linePos; @@ -117,6 +104,12 @@ typedef struct JsonReader_ JsonReader; */ JsonReader *jsonReaderNew(const char *path); +/* jsonReaderErrorMessage: get json reader error message + * pJsonReader: pointer to json reader + * return: pointer to json reader + */ +const char *jsonReaderErrorMessage(JsonReader *pJsonReader); + /* jsonMoveCurrentCharPtr: move current char pointer forward. * * pJsonReader: pointer of json reader http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/0f8c3e23/core/conn/odb/src/odb.c ---------------------------------------------------------------------- diff --git a/core/conn/odb/src/odb.c b/core/conn/odb/src/odb.c index 23f7cad..5d26c03 100755 --- a/core/conn/odb/src/odb.c +++ b/core/conn/odb/src/odb.c @@ -9736,6 +9736,11 @@ static void OloadJson(int eid) k = 0; } } + + if (pJsonReader->errorCode != JSON_ERROR_PARSE_EOF) { + fprintf(stderr, "odb [OloadJson(%d)] - Error parse json file encountered error:%s\n", __LINE__, jsonReaderErrorMessage(pJsonReader)); + } + jsonReaderFree(pJsonReader); /* load trailing rows */ if (m) { /* Insert rowset */
