Skip to content

Commit 51f21a0

Browse files
committed
fix throw error code with value datetime string incorect
1 parent 2ce055f commit 51f21a0

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

src/Container.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,21 @@ Napi::Value Container::queryByTimeSeriesRange(const Napi::CallbackInfo &info) {
319319
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", mContainer)
320320
return env.Null();
321321
}
322-
Field field;
322+
323323
Napi::Value startValue = info[0].As<Napi::Value>();
324324
Napi::Value endValue = info[1].As<Napi::Value>();
325325

326326
GSResult ret;
327327
GSQuery* pQuery = NULL;
328-
GSTimestamp startTimestampValue = Util::toGsTimestamp(env, &startValue);
329-
GSTimestamp endTimestampValue = Util::toGsTimestamp(env, &endValue);
328+
GSTimestamp startTimestampValue;
329+
GSTimestamp endTimestampValue;
330+
try {
331+
startTimestampValue = Util::toGsTimestamp(env, &startValue);
332+
endTimestampValue = Util::toGsTimestamp(env, &endValue);
333+
} catch (const Napi::Error &e) {
334+
e.ThrowAsJavaScriptException();
335+
return env.Null();
336+
}
330337

331338
ret = gsQueryByTimeSeriesRange(mContainer, startTimestampValue,
332339
endTimestampValue, &pQuery);
@@ -625,8 +632,13 @@ Napi::Value Container::remove(const Napi::CallbackInfo &info) {
625632
break;
626633
}
627634
case GS_TYPE_TIMESTAMP: {
628-
GSTimestamp tmpTimestampValue =
629-
Util::toGsTimestamp(env, &fieldValue);
635+
GSTimestamp tmpTimestampValue;
636+
try{
637+
tmpTimestampValue = Util::toGsTimestamp(env, &fieldValue);
638+
} catch (const Napi::Error &e) {
639+
PROMISE_REJECT_WITH_ERROR(deferred, e)
640+
break;
641+
}
630642
ret = gsDeleteRow(mContainer, &tmpTimestampValue, &exists);
631643
break;
632644
}

src/RowKeyPredicate.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,15 @@ void RowKeyPredicate::setRange(const Napi::CallbackInfo &info) {
149149
case GS_TYPE_TIMESTAMP: {
150150
Napi::Value startKey = info[0].As<Napi::Value>();
151151
Napi::Value finishKey = info[1].As<Napi::Value>();
152-
GSTimestamp tmpTimestampStart = Util::toGsTimestamp(env, &startKey);
153-
GSTimestamp tmpTimestampFinish = Util::toGsTimestamp(env, &finishKey);
152+
GSTimestamp tmpTimestampStart;
153+
GSTimestamp tmpTimestampFinish;
154+
try {
155+
tmpTimestampStart = Util::toGsTimestamp(env, &startKey);
156+
tmpTimestampFinish = Util::toGsTimestamp(env, &finishKey);
157+
} catch (const Napi::Error &e) {
158+
e.ThrowAsJavaScriptException();
159+
return;
160+
}
154161
ret = gsSetPredicateStartKeyByTimestamp(predicate,
155162
&tmpTimestampStart);
156163
if (!GS_SUCCEEDED(ret)) {
@@ -305,7 +312,13 @@ void RowKeyPredicate::setDistinctKeys(const Napi::CallbackInfo &info) {
305312
}
306313
case GS_TYPE_TIMESTAMP: {
307314
Napi::Value value = arrayForSet[i];
308-
GSTimestamp key = Util::toGsTimestamp(env, &value);
315+
GSTimestamp key;
316+
try {
317+
key = Util::toGsTimestamp(env, &value);
318+
} catch (const Napi::Error &e) {
319+
e.ThrowAsJavaScriptException();
320+
break;
321+
}
309322
ret = gsAddPredicateKeyByTimestamp(predicate, key);
310323
if (!GS_SUCCEEDED(ret)) {
311324
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)

src/Util.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ Napi::Value Util::fromField(const Napi::Env& env, GSRow* row, int column,
277277
return fromFieldAsGeometry(env, row, column);
278278
break;
279279
default:
280-
THROW_EXCEPTION_WITH_STR(env, "Type is not support.", NULL)
280+
THROW_CPP_EXCEPTION_WITH_STR(env, "Type is not support.")
281281
}
282282
return env.Null();
283283
}
@@ -314,8 +314,8 @@ static void toFieldAsLong(const Napi::Env &env, Napi::Value *value, GSRow *row,
314314
// When input value is integer,
315315
// it should be between -9007199254740992(-2^53)/9007199254740992(2^53).
316316
if (!(MIN_LONG <= longVal && MAX_LONG >= longVal)) {
317-
THROW_EXCEPTION_WITH_STR(env,
318-
"Input error, should be in range of long", NULL)
317+
THROW_CPP_EXCEPTION_WITH_STR(env,
318+
"Input error, should be in range of long")
319319
return;
320320
}
321321

@@ -397,9 +397,7 @@ static void toFieldAsFloat(const Napi::Env &env, Napi::Value *value,
397397
static void toFieldAsDouble(const Napi::Env &env, Napi::Value *value,
398398
GSRow *row, int column) {
399399
if (!value->IsNumber()) {
400-
Napi::Object gsException = griddb::GSException::New(env,
401-
"Input error, should be double");
402-
THROW_GSEXCEPTION(gsException)
400+
THROW_CPP_EXCEPTION_WITH_STR(env, "Input error, should be double")
403401
return;
404402
}
405403
double doubleVal = value->As<Napi::Number>().DoubleValue();
@@ -423,25 +421,16 @@ GSTimestamp Util::toGsTimestamp(const Napi::Env &env, Napi::Value *value) {
423421
GSBool ret = gsParseTime(
424422
(const GSChar *)datetimestring.c_str(), &timestampVal);
425423
if (ret != GS_TRUE) {
426-
Napi::Object gsException = griddb::GSException::New(
427-
env, "Invalid datetime string");
428-
THROW_GSEXCEPTION(gsException)
429-
return 0;
424+
THROW_CPP_EXCEPTION_WITH_STR(env, "Invalid date time string")
430425
}
431426
} else if (value->IsNumber()) {
432427
timestampVal = value->As<Napi::Number>().Int64Value();
433428
if (timestampVal > (UTC_TIMESTAMP_MAX * 1000)) { // miliseconds
434-
Napi::Object gsException = griddb::GSException::New(
435-
env, "Invalid timestamp");
436-
THROW_GSEXCEPTION(gsException)
437-
return 0;
429+
THROW_CPP_EXCEPTION_WITH_STR(env, "Invalid timestamp")
438430
}
439431
} else {
440432
// Invalid input
441-
Napi::Object gsException = griddb::GSException::New(
442-
env, "Invalid input");
443-
THROW_GSEXCEPTION(gsException)
444-
return 0;
433+
THROW_CPP_EXCEPTION_WITH_STR(env, "Invalid input")
445434
}
446435
return timestampVal;
447436
}
@@ -457,10 +446,7 @@ static void toFieldAsBlob(const Napi::Env &env, Napi::Value *value, GSRow *row,
457446
int column) {
458447
GSBlob blobVal;
459448
if (!value->IsBuffer()) {
460-
Napi::Object gsException = griddb::GSException::New(env,
461-
"Input error, should be buffer");
462-
THROW_GSEXCEPTION(gsException)
463-
return;
449+
THROW_CPP_EXCEPTION_WITH_STR(env, "Input error, should be buffer")
464450
}
465451
Napi::Buffer<char> stringBuffer = value->As<Napi::Buffer<char>>();
466452
char *v = static_cast<char*>(stringBuffer.Data());
@@ -533,7 +519,6 @@ void Util::toField(const Napi::Env &env, Napi::Value *value, GSRow *row,
533519
}
534520
}
535521

536-
537522
void Util::setInstanceData(Napi::Env env, std::string key,
538523
Napi::FunctionReference* function) {
539524
env.GetInstanceData<AddonData>()->insert

0 commit comments

Comments
 (0)