File: error.md

package info (click to toggle)
node-addon-api 8.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,248 kB
  • sloc: cpp: 15,431; javascript: 5,631; ansic: 157; makefile: 7
file content (120 lines) | stat: -rw-r--r-- 3,265 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Error

Class `Napi::Error` inherits from class [`Napi::ObjectReference`][] and class [`std::exception`][].

The `Napi::Error` class is a representation of the JavaScript `Error` object that is thrown
when runtime errors occur. The Error object can also be used as a base object for
user-defined exceptions.

The `Napi::Error` class is a persistent reference to a JavaScript error object thus
inherits its behavior from the `Napi::ObjectReference` class (for more info see: [`Napi::ObjectReference`](object_reference.md)).

If C++ exceptions are enabled (for more info see: [Setup](setup.md)), then the
`Napi::Error` class extends `std::exception` and enables integrated
error-handling for C++ exceptions and JavaScript exceptions.

For more details about error handling refer to the section titled [Error handling](error_handling.md).

## Methods

### New

Creates empty instance of an `Napi::Error` object for the specified environment.

```cpp
Napi::Error::New(Napi::Env env);
```

- `[in] env`: The environment in which to construct the `Napi::Error` object.

Returns an instance of `Napi::Error` object.

### New

Creates instance of an `Napi::Error` object.

```cpp
Napi::Error::New(Napi::Env env, const char* message);
```

- `[in] env`: The environment in which to construct the `Napi::Error` object.
- `[in] message`: Null-terminated string to be used as the message for the `Napi::Error`.

Returns instance of an `Napi::Error` object.

### New

Creates instance of an `Napi::Error` object

```cpp
Napi::Error::New(Napi::Env env, const std::string& message);
```

- `[in] env`: The environment in which to construct the `Napi::Error` object.
- `[in] message`: Reference string to be used as the message for the `Napi::Error`.

Returns instance of an `Napi::Error` object.

### Fatal

In case of an unrecoverable error in a native module, a fatal error can be thrown
to immediately terminate the process.

```cpp
static NAPI_NO_RETURN void Napi::Error::Fatal(const char* location, const char* message);
```

The function call does not return, the process will be terminated.

### Constructor

Creates empty instance of an `Napi::Error`.

```cpp
Napi::Error::Error();
```

Returns an instance of `Napi::Error` object.

### Constructor

Initializes an `Napi::Error` instance from an existing JavaScript error object.

```cpp
Napi::Error::Error(napi_env env, napi_value value);
```

- `[in] env`: The environment in which to construct the error object.
- `[in] value`: The `Napi::Error` reference to wrap.

Returns instance of an `Napi::Error` object.

### Message

```cpp
std::string& Napi::Error::Message() const NAPI_NOEXCEPT;
```

Returns the reference to the string that represent the message of the error.

### ThrowAsJavaScriptException

Throw the error as JavaScript exception.

```cpp
void Napi::Error::ThrowAsJavaScriptException() const;
```

Throws the error as a JavaScript exception.

### what

```cpp
const char* Napi::Error::what() const NAPI_NOEXCEPT override;
```

Returns a pointer to a null-terminated string that is used to identify the
exception. This method can be used only if the exception mechanism is enabled.

[`Napi::ObjectReference`]: ./object_reference.md
[`std::exception`]: https://cplusplus.com/reference/exception/exception/