@picovoice/eagle-web 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,252 +1,252 @@
1
- //
2
- // Copyright 2023 Picovoice Inc.
3
- //
4
- // You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
5
- // file accompanying this source.
6
- //
7
- // Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8
- // an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
- // specific language governing permissions and limitations under the License.
10
- //
11
-
12
- import { PvError } from '@picovoice/web-utils';
13
- import { PvStatus } from './types';
14
-
15
- class EagleError extends Error {
16
- private readonly _status: PvStatus;
17
- private readonly _shortMessage: string;
18
- private readonly _messageStack: string[];
19
-
20
- constructor(
21
- status: PvStatus,
22
- message: string,
23
- messageStack: string[] = [],
24
- pvError: PvError | null = null
25
- ) {
26
- super(EagleError.errorToString(message, messageStack, pvError));
27
- this._status = status;
28
- this.name = 'EagleError';
29
- this._shortMessage = message;
30
- this._messageStack = messageStack;
31
- }
32
-
33
- get status(): PvStatus {
34
- return this._status;
35
- }
36
-
37
- get shortMessage(): string {
38
- return this._shortMessage;
39
- }
40
-
41
- get messageStack(): string[] {
42
- return this._messageStack;
43
- }
44
-
45
- private static errorToString(
46
- initial: string,
47
- messageStack: string[],
48
- pvError: PvError | null = null
49
- ): string {
50
- let msg = initial;
51
-
52
- if (pvError) {
53
- const pvErrorMessage = pvError.getErrorString();
54
- if (pvErrorMessage.length > 0) {
55
- msg += `\nDetails: ${pvErrorMessage}`;
56
- }
57
- }
58
-
59
- if (messageStack.length > 0) {
60
- msg += `: ${messageStack.reduce(
61
- (acc, value, index) => acc + '\n [' + index + '] ' + value,
62
- ''
63
- )}`;
64
- }
65
-
66
- return msg;
67
- }
68
- }
69
-
70
- class EagleOutOfMemoryError extends EagleError {
71
- constructor(
72
- message: string,
73
- messageStack?: string[],
74
- pvError: PvError | null = null
75
- ) {
76
- super(PvStatus.OUT_OF_MEMORY, message, messageStack, pvError);
77
- this.name = 'EagleOutOfMemoryError';
78
- }
79
- }
80
-
81
- class EagleIOError extends EagleError {
82
- constructor(
83
- message: string,
84
- messageStack: string[] = [],
85
- pvError: PvError | null = null
86
- ) {
87
- super(PvStatus.IO_ERROR, message, messageStack, pvError);
88
- this.name = 'EagleIOError';
89
- }
90
- }
91
-
92
- class EagleInvalidArgumentError extends EagleError {
93
- constructor(
94
- message: string,
95
- messageStack: string[] = [],
96
- pvError: PvError | null = null
97
- ) {
98
- super(PvStatus.INVALID_ARGUMENT, message, messageStack, pvError);
99
- this.name = 'EagleInvalidArgumentError';
100
- }
101
- }
102
-
103
- class EagleStopIterationError extends EagleError {
104
- constructor(
105
- message: string,
106
- messageStack: string[] = [],
107
- pvError: PvError | null = null
108
- ) {
109
- super(PvStatus.STOP_ITERATION, message, messageStack, pvError);
110
- this.name = 'EagleStopIterationError';
111
- }
112
- }
113
-
114
- class EagleKeyError extends EagleError {
115
- constructor(
116
- message: string,
117
- messageStack: string[] = [],
118
- pvError: PvError | null = null
119
- ) {
120
- super(PvStatus.KEY_ERROR, message, messageStack, pvError);
121
- this.name = 'EagleKeyError';
122
- }
123
- }
124
-
125
- class EagleInvalidStateError extends EagleError {
126
- constructor(
127
- message: string,
128
- messageStack: string[] = [],
129
- pvError: PvError | null = null
130
- ) {
131
- super(PvStatus.INVALID_STATE, message, messageStack, pvError);
132
- this.name = 'EagleInvalidStateError';
133
- }
134
- }
135
-
136
- class EagleRuntimeError extends EagleError {
137
- constructor(
138
- message: string,
139
- messageStack: string[] = [],
140
- pvError: PvError | null = null
141
- ) {
142
- super(PvStatus.RUNTIME_ERROR, message, messageStack, pvError);
143
- this.name = 'EagleRuntimeError';
144
- }
145
- }
146
-
147
- class EagleActivationError extends EagleError {
148
- constructor(
149
- message: string,
150
- messageStack: string[] = [],
151
- pvError: PvError | null = null
152
- ) {
153
- super(PvStatus.ACTIVATION_ERROR, message, messageStack, pvError);
154
- this.name = 'EagleActivationError';
155
- }
156
- }
157
-
158
- class EagleActivationLimitReachedError extends EagleError {
159
- constructor(
160
- message: string,
161
- messageStack: string[] = [],
162
- pvError: PvError | null = null
163
- ) {
164
- super(PvStatus.ACTIVATION_LIMIT_REACHED, message, messageStack, pvError);
165
- this.name = 'EagleActivationLimitReachedError';
166
- }
167
- }
168
-
169
- class EagleActivationThrottledError extends EagleError {
170
- constructor(
171
- message: string,
172
- messageStack: string[] = [],
173
- pvError: PvError | null = null
174
- ) {
175
- super(PvStatus.ACTIVATION_THROTTLED, message, messageStack, pvError);
176
- this.name = 'EagleActivationThrottledError';
177
- }
178
- }
179
-
180
- class EagleActivationRefusedError extends EagleError {
181
- constructor(
182
- message: string,
183
- messageStack: string[] = [],
184
- pvError: PvError | null = null
185
- ) {
186
- super(PvStatus.ACTIVATION_REFUSED, message, messageStack, pvError);
187
- this.name = 'EagleActivationRefusedError';
188
- }
189
- }
190
-
191
- export {
192
- EagleError,
193
- EagleOutOfMemoryError,
194
- EagleIOError,
195
- EagleInvalidArgumentError,
196
- EagleStopIterationError,
197
- EagleKeyError,
198
- EagleInvalidStateError,
199
- EagleRuntimeError,
200
- EagleActivationError,
201
- EagleActivationLimitReachedError,
202
- EagleActivationThrottledError,
203
- EagleActivationRefusedError,
204
- };
205
-
206
- export function pvStatusToException(
207
- pvStatus: PvStatus,
208
- errorMessage: string,
209
- messageStack: string[] = [],
210
- pvError: PvError | null = null
211
- ): EagleError {
212
- switch (pvStatus) {
213
- case PvStatus.OUT_OF_MEMORY:
214
- return new EagleOutOfMemoryError(errorMessage, messageStack, pvError);
215
- case PvStatus.IO_ERROR:
216
- return new EagleIOError(errorMessage, messageStack, pvError);
217
- case PvStatus.INVALID_ARGUMENT:
218
- return new EagleInvalidArgumentError(errorMessage, messageStack, pvError);
219
- case PvStatus.STOP_ITERATION:
220
- return new EagleStopIterationError(errorMessage, messageStack, pvError);
221
- case PvStatus.KEY_ERROR:
222
- return new EagleKeyError(errorMessage, messageStack, pvError);
223
- case PvStatus.INVALID_STATE:
224
- return new EagleInvalidStateError(errorMessage, messageStack, pvError);
225
- case PvStatus.RUNTIME_ERROR:
226
- return new EagleRuntimeError(errorMessage, messageStack, pvError);
227
- case PvStatus.ACTIVATION_ERROR:
228
- return new EagleActivationError(errorMessage, messageStack, pvError);
229
- case PvStatus.ACTIVATION_LIMIT_REACHED:
230
- return new EagleActivationLimitReachedError(
231
- errorMessage,
232
- messageStack,
233
- pvError
234
- );
235
- case PvStatus.ACTIVATION_THROTTLED:
236
- return new EagleActivationThrottledError(
237
- errorMessage,
238
- messageStack,
239
- pvError
240
- );
241
- case PvStatus.ACTIVATION_REFUSED:
242
- return new EagleActivationRefusedError(
243
- errorMessage,
244
- messageStack,
245
- pvError
246
- );
247
- default:
248
- // eslint-disable-next-line no-console
249
- console.warn(`Unmapped error code: ${pvStatus}`);
250
- return new EagleError(pvStatus, errorMessage);
251
- }
252
- }
1
+ //
2
+ // Copyright 2023 Picovoice Inc.
3
+ //
4
+ // You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
5
+ // file accompanying this source.
6
+ //
7
+ // Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8
+ // an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+
12
+ import { PvError } from '@picovoice/web-utils';
13
+ import { PvStatus } from './types';
14
+
15
+ class EagleError extends Error {
16
+ private readonly _status: PvStatus;
17
+ private readonly _shortMessage: string;
18
+ private readonly _messageStack: string[];
19
+
20
+ constructor(
21
+ status: PvStatus,
22
+ message: string,
23
+ messageStack: string[] = [],
24
+ pvError: PvError | null = null
25
+ ) {
26
+ super(EagleError.errorToString(message, messageStack, pvError));
27
+ this._status = status;
28
+ this.name = 'EagleError';
29
+ this._shortMessage = message;
30
+ this._messageStack = messageStack;
31
+ }
32
+
33
+ get status(): PvStatus {
34
+ return this._status;
35
+ }
36
+
37
+ get shortMessage(): string {
38
+ return this._shortMessage;
39
+ }
40
+
41
+ get messageStack(): string[] {
42
+ return this._messageStack;
43
+ }
44
+
45
+ private static errorToString(
46
+ initial: string,
47
+ messageStack: string[],
48
+ pvError: PvError | null = null
49
+ ): string {
50
+ let msg = initial;
51
+
52
+ if (pvError) {
53
+ const pvErrorMessage = pvError.getErrorString();
54
+ if (pvErrorMessage.length > 0) {
55
+ msg += `\nDetails: ${pvErrorMessage}`;
56
+ }
57
+ }
58
+
59
+ if (messageStack.length > 0) {
60
+ msg += `: ${messageStack.reduce(
61
+ (acc, value, index) => acc + '\n [' + index + '] ' + value,
62
+ ''
63
+ )}`;
64
+ }
65
+
66
+ return msg;
67
+ }
68
+ }
69
+
70
+ class EagleOutOfMemoryError extends EagleError {
71
+ constructor(
72
+ message: string,
73
+ messageStack?: string[],
74
+ pvError: PvError | null = null
75
+ ) {
76
+ super(PvStatus.OUT_OF_MEMORY, message, messageStack, pvError);
77
+ this.name = 'EagleOutOfMemoryError';
78
+ }
79
+ }
80
+
81
+ class EagleIOError extends EagleError {
82
+ constructor(
83
+ message: string,
84
+ messageStack: string[] = [],
85
+ pvError: PvError | null = null
86
+ ) {
87
+ super(PvStatus.IO_ERROR, message, messageStack, pvError);
88
+ this.name = 'EagleIOError';
89
+ }
90
+ }
91
+
92
+ class EagleInvalidArgumentError extends EagleError {
93
+ constructor(
94
+ message: string,
95
+ messageStack: string[] = [],
96
+ pvError: PvError | null = null
97
+ ) {
98
+ super(PvStatus.INVALID_ARGUMENT, message, messageStack, pvError);
99
+ this.name = 'EagleInvalidArgumentError';
100
+ }
101
+ }
102
+
103
+ class EagleStopIterationError extends EagleError {
104
+ constructor(
105
+ message: string,
106
+ messageStack: string[] = [],
107
+ pvError: PvError | null = null
108
+ ) {
109
+ super(PvStatus.STOP_ITERATION, message, messageStack, pvError);
110
+ this.name = 'EagleStopIterationError';
111
+ }
112
+ }
113
+
114
+ class EagleKeyError extends EagleError {
115
+ constructor(
116
+ message: string,
117
+ messageStack: string[] = [],
118
+ pvError: PvError | null = null
119
+ ) {
120
+ super(PvStatus.KEY_ERROR, message, messageStack, pvError);
121
+ this.name = 'EagleKeyError';
122
+ }
123
+ }
124
+
125
+ class EagleInvalidStateError extends EagleError {
126
+ constructor(
127
+ message: string,
128
+ messageStack: string[] = [],
129
+ pvError: PvError | null = null
130
+ ) {
131
+ super(PvStatus.INVALID_STATE, message, messageStack, pvError);
132
+ this.name = 'EagleInvalidStateError';
133
+ }
134
+ }
135
+
136
+ class EagleRuntimeError extends EagleError {
137
+ constructor(
138
+ message: string,
139
+ messageStack: string[] = [],
140
+ pvError: PvError | null = null
141
+ ) {
142
+ super(PvStatus.RUNTIME_ERROR, message, messageStack, pvError);
143
+ this.name = 'EagleRuntimeError';
144
+ }
145
+ }
146
+
147
+ class EagleActivationError extends EagleError {
148
+ constructor(
149
+ message: string,
150
+ messageStack: string[] = [],
151
+ pvError: PvError | null = null
152
+ ) {
153
+ super(PvStatus.ACTIVATION_ERROR, message, messageStack, pvError);
154
+ this.name = 'EagleActivationError';
155
+ }
156
+ }
157
+
158
+ class EagleActivationLimitReachedError extends EagleError {
159
+ constructor(
160
+ message: string,
161
+ messageStack: string[] = [],
162
+ pvError: PvError | null = null
163
+ ) {
164
+ super(PvStatus.ACTIVATION_LIMIT_REACHED, message, messageStack, pvError);
165
+ this.name = 'EagleActivationLimitReachedError';
166
+ }
167
+ }
168
+
169
+ class EagleActivationThrottledError extends EagleError {
170
+ constructor(
171
+ message: string,
172
+ messageStack: string[] = [],
173
+ pvError: PvError | null = null
174
+ ) {
175
+ super(PvStatus.ACTIVATION_THROTTLED, message, messageStack, pvError);
176
+ this.name = 'EagleActivationThrottledError';
177
+ }
178
+ }
179
+
180
+ class EagleActivationRefusedError extends EagleError {
181
+ constructor(
182
+ message: string,
183
+ messageStack: string[] = [],
184
+ pvError: PvError | null = null
185
+ ) {
186
+ super(PvStatus.ACTIVATION_REFUSED, message, messageStack, pvError);
187
+ this.name = 'EagleActivationRefusedError';
188
+ }
189
+ }
190
+
191
+ export {
192
+ EagleError,
193
+ EagleOutOfMemoryError,
194
+ EagleIOError,
195
+ EagleInvalidArgumentError,
196
+ EagleStopIterationError,
197
+ EagleKeyError,
198
+ EagleInvalidStateError,
199
+ EagleRuntimeError,
200
+ EagleActivationError,
201
+ EagleActivationLimitReachedError,
202
+ EagleActivationThrottledError,
203
+ EagleActivationRefusedError,
204
+ };
205
+
206
+ export function pvStatusToException(
207
+ pvStatus: PvStatus,
208
+ errorMessage: string,
209
+ messageStack: string[] = [],
210
+ pvError: PvError | null = null
211
+ ): EagleError {
212
+ switch (pvStatus) {
213
+ case PvStatus.OUT_OF_MEMORY:
214
+ return new EagleOutOfMemoryError(errorMessage, messageStack, pvError);
215
+ case PvStatus.IO_ERROR:
216
+ return new EagleIOError(errorMessage, messageStack, pvError);
217
+ case PvStatus.INVALID_ARGUMENT:
218
+ return new EagleInvalidArgumentError(errorMessage, messageStack, pvError);
219
+ case PvStatus.STOP_ITERATION:
220
+ return new EagleStopIterationError(errorMessage, messageStack, pvError);
221
+ case PvStatus.KEY_ERROR:
222
+ return new EagleKeyError(errorMessage, messageStack, pvError);
223
+ case PvStatus.INVALID_STATE:
224
+ return new EagleInvalidStateError(errorMessage, messageStack, pvError);
225
+ case PvStatus.RUNTIME_ERROR:
226
+ return new EagleRuntimeError(errorMessage, messageStack, pvError);
227
+ case PvStatus.ACTIVATION_ERROR:
228
+ return new EagleActivationError(errorMessage, messageStack, pvError);
229
+ case PvStatus.ACTIVATION_LIMIT_REACHED:
230
+ return new EagleActivationLimitReachedError(
231
+ errorMessage,
232
+ messageStack,
233
+ pvError
234
+ );
235
+ case PvStatus.ACTIVATION_THROTTLED:
236
+ return new EagleActivationThrottledError(
237
+ errorMessage,
238
+ messageStack,
239
+ pvError
240
+ );
241
+ case PvStatus.ACTIVATION_REFUSED:
242
+ return new EagleActivationRefusedError(
243
+ errorMessage,
244
+ messageStack,
245
+ pvError
246
+ );
247
+ default:
248
+ // eslint-disable-next-line no-console
249
+ console.warn(`Unmapped error code: ${pvStatus}`);
250
+ return new EagleError(pvStatus, errorMessage);
251
+ }
252
+ }