Skip to content

Commit

Permalink
Throw error if N-API call fails
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicdoe committed Dec 26, 2020
1 parent 11dbbb5 commit a891bcf
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions ced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ napi_value Method(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
if (status != napi_ok) return nullptr;
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}

void* buf;
size_t buf_length;
status = napi_get_buffer_info(env, args[0], &buf, &buf_length);
if (status != napi_ok) return nullptr;
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}

const char* text = static_cast<const char*>(buf);
size_t text_length = buf_length;
Expand All @@ -32,7 +38,10 @@ napi_value Method(napi_env env, napi_callback_info info) {
napi_value encodingName;
status = napi_create_string_utf8(env, encoding_name, NAPI_AUTO_LENGTH,
&encodingName);
if (status != napi_ok) return nullptr;
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}

return encodingName;
}
Expand All @@ -42,10 +51,16 @@ napi_value init(napi_env env, napi_value exports) {
napi_value fn;

status = napi_create_function(env, nullptr, 0, Method, nullptr, &fn);
if (status != napi_ok) return nullptr;
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}

status = napi_set_named_property(env, exports, "detectEncoding", fn);
if (status != napi_ok) return nullptr;
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}

return exports;
}
Expand Down

0 comments on commit a891bcf

Please sign in to comment.