Skip to content

n-api error status discussion #29849

Closed
Closed
@legendecas

Description

  • Version: master
  • Platform: -
  • Subsystem: n-api

We could find out that there are several napi_status existing and been used in various n-api functions.

  • napi_ok:
    For any n-api call that is successful.

  • napi_cancelled
    For canceled async works.

  • napi_escape_called_twice
  • napi_handle_scope_mismatch
  • napi_callback_scope_mismatch
    For handle scope error conditions.

  • napi_queue_full
  • napi_closing
    For ThreadSafeFunction error conditions.

  • napi_invalid_arg
    This is what I'd like to discuss about. If I got it right, mostly, invalid args of calling of n-api should get into this status, except following type exceptions (mostly JavaScript primitive values, but array and date are not, name is a virtual type that is defined in ECMA spec):

  • napi_object_expected

  • napi_string_expected

  • napi_name_expected

  • napi_function_expected

    • Exceptions we have now:
    • napi_instanceof: would throw JavaScript TypeError if constructor is not a function, and return a napi_function_expected status.
  • napi_number_expected

  • napi_boolean_expected

  • napi_array_expected

  • napi_bigint_expected

  • napi_date_expected


Except for all above statuses, following two is additional generic error status:

  • napi_generic_failure
    This could be used in most places IMO, but it has been mixed up with napi_pending_exception in some conditions:

  • napi_pending_exception
    For any n-api call that has pending JavaScript Exception. If I understand n-api document right, every non-napi_ok status should throw a JavaScript exception, and napi_pending_exception should be used if the previous JavaScript exception was not handled while calling sequent n-apis.

The return value will be napi_ok if the request was successful and no uncaught JavaScript exception was thrown. If an error occurred AND an exception was thrown, the napi_status value for the error will be returned. If an exception was thrown, and no error occurred, napi_pending_exception will be returned. Link


So here comes the unresolved questions:

  • How do we define new type expectation status constraints: should they be limited to JavaScript primitive values? Or any non-primitive types that have an equivalent napi_is_<typename> function?
  • In which conditions N-APIs should throw JavaScript exceptions? and for those already throwing, which status should be returned? napi_generic_failure or napi_pending_exception?

Refs: #29768 (comment)
Related: #29847

/cc @gabrielschulhof @mhdawson

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    node-apiIssues and PRs related to the Node-API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions