1/* 2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/** 17 * @file 18 * @kit AbilityKit 19 */ 20 21import { AsyncCallback } from './@ohos.base'; 22import * as _ErrorObserver from './application/ErrorObserver'; 23import { LoopObserver as _LoopObserver } from './application/LoopObserver'; 24 25/** 26 * This module provides the function of error manager. 27 * 28 * @namespace errorManager 29 * @syscap SystemCapability.Ability.AbilityRuntime.Core 30 * @since 9 31 */ 32/** 33 * This module provides the function of error manager. 34 * 35 * @namespace errorManager 36 * @syscap SystemCapability.Ability.AbilityRuntime.Core 37 * @atomicservice 38 * @since 11 39 */ 40/** 41 * This module provides the function of error manager. 42 * 43 * @namespace errorManager 44 * @syscap SystemCapability.Ability.AbilityRuntime.Core 45 * @crossplatform 46 * @atomicservice 47 * @since 19 48 */ 49declare namespace errorManager { 50 /** 51 * Register error observer. 52 * 53 * @param { 'error' } type - error. 54 * @param { ErrorObserver } observer - The error observer. 55 * @returns { number } Returns the number code of the observer. 56 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 57 * 2. Incorrect parameter types; 3. Parameter verification failed. 58 * @throws { BusinessError } 16000003 - The specified ID does not exist. 59 * @syscap SystemCapability.Ability.AbilityRuntime.Core 60 * @since 9 61 */ 62 /** 63 * Register error observer. 64 * 65 * @param { 'error' } type - error. 66 * @param { ErrorObserver } observer - The error observer. 67 * @returns { number } Returns the number code of the observer. 68 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 69 * 2. Incorrect parameter types; 3. Parameter verification failed. 70 * @throws { BusinessError } 16000003 - The specified ID does not exist. 71 * @syscap SystemCapability.Ability.AbilityRuntime.Core 72 * @atomicservice 73 * @since 11 74 */ 75 /** 76 * Register error observer. 77 * 78 * @param { 'error' } type - error. 79 * @param { ErrorObserver } observer - The error observer. 80 * @returns { number } Returns the number code of the observer. 81 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 82 * 2. Incorrect parameter types; 3. Parameter verification failed. 83 * @throws { BusinessError } 16000003 - The specified ID does not exist. 84 * @syscap SystemCapability.Ability.AbilityRuntime.Core 85 * @crossplatform 86 * @atomicservice 87 * @since 19 88 */ 89 function on(type: 'error', observer: ErrorObserver): number; 90 91 /** 92 * Unregister error observer. 93 * 94 * @param { 'error' } type - error. 95 * @param { number } observerId - Indicates the number code of the observer. 96 * @param { AsyncCallback<void> } callback - The callback of off. 97 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 98 * 2. Incorrect parameter types; 3. Parameter verification failed. 99 * @throws { BusinessError } 16000003 - The specified ID does not exist. 100 * @syscap SystemCapability.Ability.AbilityRuntime.Core 101 * @since 9 102 */ 103 /** 104 * Unregister error observer. 105 * 106 * @param { 'error' } type - error. 107 * @param { number } observerId - Indicates the number code of the observer. 108 * @param { AsyncCallback<void> } callback - The callback of off. 109 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 110 * 2. Incorrect parameter types; 3. Parameter verification failed. 111 * @throws { BusinessError } 16000003 - The specified ID does not exist. 112 * @syscap SystemCapability.Ability.AbilityRuntime.Core 113 * @atomicservice 114 * @since 11 115 */ 116 /** 117 * Unregister error observer. 118 * 119 * @param { 'error' } type - error. 120 * @param { number } observerId - Indicates the number code of the observer. 121 * @param { AsyncCallback<void> } callback - The callback of off. 122 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 123 * 2. Incorrect parameter types; 3. Parameter verification failed. 124 * @throws { BusinessError } 16000003 - The specified ID does not exist. 125 * @syscap SystemCapability.Ability.AbilityRuntime.Core 126 * @crossplatform 127 * @atomicservice 128 * @since 19 129 */ 130 function off(type: 'error', observerId: number, callback: AsyncCallback<void>): void; 131 132 /** 133 * Unregister error observer. 134 * 135 * @param { 'error' } type - error. 136 * @param { number } observerId - Indicates the number code of the observer. 137 * @returns { Promise<void> } The promise returned by the function. 138 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 139 * 2. Incorrect parameter types; 3. Parameter verification failed. 140 * @throws { BusinessError } 16000003 - The specified ID does not exist. 141 * @syscap SystemCapability.Ability.AbilityRuntime.Core 142 * @since 9 143 */ 144 /** 145 * Unregister error observer. 146 * 147 * @param { 'error' } type - error. 148 * @param { number } observerId - Indicates the number code of the observer. 149 * @returns { Promise<void> } The promise returned by the function. 150 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 151 * 2. Incorrect parameter types; 3. Parameter verification failed. 152 * @throws { BusinessError } 16000003 - The specified ID does not exist. 153 * @syscap SystemCapability.Ability.AbilityRuntime.Core 154 * @atomicservice 155 * @since 11 156 */ 157 /** 158 * Unregister error observer. 159 * 160 * @param { 'error' } type - error. 161 * @param { number } observerId - Indicates the number code of the observer. 162 * @returns { Promise<void> } The promise returned by the function. 163 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 164 * 2. Incorrect parameter types; 3. Parameter verification failed. 165 * @throws { BusinessError } 16000003 - The specified ID does not exist. 166 * @syscap SystemCapability.Ability.AbilityRuntime.Core 167 * @crossplatform 168 * @atomicservice 169 * @since 19 170 */ 171 function off(type: 'error', observerId: number): Promise<void>; 172 173 /** 174 * Register an error observer for all VM instances include worker and taskpool. 175 * @param { 'globalErrorOccurred'} type - globalErrorOccurred 176 * @param { GlobalObserver } observer - the global error observer. 177 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 178 * 2. Incorrect parameter types; 3. Parameter verification failed. 179 * @throws { BusinessError } 16200001 - The caller has been released. 180 * @syscap SystemCapability.Ability.AbilityRuntime.Core 181 * @atomicservice 182 * @since 18 183 */ 184 function on(type: 'globalErrorOccurred', observer: GlobalObserver): void; 185 186 /** 187 * Register a rejection observer for all VM instances include worker and taskpool. 188 * @param { 'globalUnhandledRejectionDetected'} type - globalUnhandledRejectionDetected. 189 * @param { GlobalObserver } observer - the global error observer. 190 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 191 * 2. Incorrect parameter types; 3. Parameter verification failed. 192 * @throws { BusinessError } 16200001 - The caller has been released. 193 * @syscap SystemCapability.Ability.AbilityRuntime.Core 194 * @atomicservice 195 * @since 18 196 */ 197 function on(type: 'globalUnhandledRejectionDetected', observer: GlobalObserver): void; 198 199 /** 200 * Unregister the error observer for all VM instance include worker and taskpool. 201 * @param { 'globalErrorOccurred'} type - globalErrorOccurred. 202 * @param { GlobalObserver } observer - the global error observer. 203 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 204 * 2. Incorrect parameter types; 3. Parameter verification failed. 205 * @throws { BusinessError } 16200001 - The caller has been released. 206 * @throws { BusinessError } 16300004 - The observer does not exist. 207 * @syscap SystemCapability.Ability.AbilityRuntime.Core 208 * @atomicservice 209 * @since 18 210 */ 211 function off(type: 'globalErrorOccurred', observer?: GlobalObserver): void; 212 213 /** 214 * Unregister the rejection observer for all VM instance include worker and taskpool. 215 * @param { 'globalUnhandledRejectionDetected'} type - globalUnhandledRejectionDetected. 216 * @param { GlobalObserver } observer - the global error observer. 217 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 218 * 2. Incorrect parameter types; 3. Parameter verification failed. 219 * @throws { BusinessError } 16200001 - The caller has been released. 220 * @throws { BusinessError } 16300004 - The observer does not exist. 221 * @syscap SystemCapability.Ability.AbilityRuntime.Core 222 * @atomicservice 223 * @since 18 224 */ 225 function off(type: 'globalUnhandledRejectionDetected', observer?: GlobalObserver): void; 226 227 /** 228 * The observer will be called by system when an error or unhandled rejection occurs 229 * from all VM instances include worker and taskpool. 230 * @typedef { function } 231 * { GlobalError } reason - the reason of the error or rejection. 232 * @syscap SystemCapability.Ability.AbilityRuntime.Core 233 * @atomicservice 234 * @since 18 235 */ 236 export type GlobalObserver = (reason: GlobalError) => void; 237 238 /** 239 * Defines GlobalError. 240 * 241 * @extends Error 242 * @typedef GlobalError 243 * @syscap SystemCapability.Ability.AbilityRuntime.Core 244 * @atomicservice 245 * @since 18 246 */ 247 export interface GlobalError extends Error { 248 /** 249 * Define the instance name of VM. 250 * @type { string } instanceName 251 * @syscap SystemCapability.Ability.AbilityRuntime.Core 252 * @atomicservice 253 * @since 18 254 */ 255 instanceName: string; 256 257 /** 258 * Define the instance type of VM. 259 * @type { InstanceType } instanceType 260 * @syscap SystemCapability.Ability.AbilityRuntime.Core 261 * @atomicservice 262 * @since 18 263 */ 264 instanceType: InstanceType; 265 } 266 267 /** 268 * Define the instance type of VM. 269 * @enum { number } 270 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 271 * @atomicservice 272 * @since 18 273 */ 274 export enum InstanceType { 275 /** 276 * Indicates it is the main VM instance. 277 * 278 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 279 * @atomicservice 280 * @since 18 281 */ 282 MAIN = 0, 283 284 /** 285 * Indicates it is the worker VM instance. 286 * 287 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 288 * @atomicservice 289 * @since 18 290 */ 291 WORKER = 1, 292 293 /** 294 * Indicates it is the taskpool VM instance. 295 * 296 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 297 * @atomicservice 298 * @since 18 299 */ 300 TASKPOOL = 2, 301 302 /** 303 * Indicates it is a VM instance created by napi_create_ark_runtime from native code by user. 304 * 305 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 306 * @atomicservice 307 * @since 18 308 */ 309 CUSTOM = 3, 310 } 311 312 /** 313 * Register loop observer. This function can only by called from main thread, 314 * and if call this function multiple times, the last 315 * modification will overwrite the previous one. 316 * 317 * @param { 'loopObserver' } type - loopObserver. 318 * @param { number } timeout - Indicates timeout(ms) value of loop observer. 319 * @param { LoopObserver } observer - The loop observer. 320 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 321 * 2. Incorrect parameter types; 3. Parameter verification failed. 322 * @syscap SystemCapability.Ability.AbilityRuntime.Core 323 * @atomicservice 324 * @since 12 325 */ 326 /** 327 * Register loop observer. This function can only by called from main thread, 328 * and if call this function multiple times, the last 329 * modification will overwrite the previous one. 330 * 331 * @param { 'loopObserver' } type - loopObserver. 332 * @param { number } timeout - Indicates timeout(ms) value of loop observer. 333 * @param { LoopObserver } observer - The loop observer. 334 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 335 * 2. Incorrect parameter types; 3. Parameter verification failed. 336 * @syscap SystemCapability.Ability.AbilityRuntime.Core 337 * @crossplatform 338 * @atomicservice 339 * @since 19 340 */ 341 function on(type: 'loopObserver', timeout: number, observer: LoopObserver): void; 342 343 /** 344 * Unregister loop observer. This function can only by called from main thread. 345 * 346 * @param { 'loopObserver' } type - loopObserver. 347 * @param { LoopObserver } observer - The loop observer. 348 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 349 * 2. Incorrect parameter types; 3. Parameter verification failed. 350 * @syscap SystemCapability.Ability.AbilityRuntime.Core 351 * @atomicservice 352 * @since 12 353 */ 354 /** 355 * Unregister loop observer. This function can only by called from main thread. 356 * 357 * @param { 'loopObserver' } type - loopObserver. 358 * @param { LoopObserver } observer - The loop observer. 359 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 360 * 2. Incorrect parameter types; 3. Parameter verification failed. 361 * @syscap SystemCapability.Ability.AbilityRuntime.Core 362 * @crossplatform 363 * @atomicservice 364 * @since 19 365 */ 366 function off(type: 'loopObserver', observer?: LoopObserver): void; 367 368 /** 369 * Register unhandled rejection observer. 370 * 371 * @param { 'unhandledRejection' } type - 'unhandledRejection'. 372 * @param { UnhandledRejectionObserver } observer - The unhandled rejection observer. 373 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 374 * 2. Incorrect parameter types; 3. Parameter verification failed. 375 * @throws { BusinessError } 16200001 - The caller has been released. 376 * @syscap SystemCapability.Ability.AbilityRuntime.Core 377 * @atomicservice 378 * @since 12 379 */ 380 /** 381 * Register unhandled rejection observer. 382 * 383 * @param { 'unhandledRejection' } type - 'unhandledRejection'. 384 * @param { UnhandledRejectionObserver } observer - The unhandled rejection observer. 385 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 386 * 2. Incorrect parameter types; 3. Parameter verification failed. 387 * @throws { BusinessError } 16200001 - The caller has been released. 388 * @syscap SystemCapability.Ability.AbilityRuntime.Core 389 * @crossplatform 390 * @atomicservice 391 * @since 19 392 */ 393 function on(type: 'unhandledRejection', observer: UnhandledRejectionObserver): void; 394 395 /** 396 * Unregister unhandled rejection observer. 397 * 398 * @param { 'unhandledRejection' } type - error. 399 * @param { UnhandledRejectionObserver } [observer] - the registered observer 400 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 401 * 2. Incorrect parameter types; 3. Parameter verification failed. 402 * @throws { BusinessError } 16200001 - The caller has been released. 403 * @throws { BusinessError } 16300004 - The observer does not exist. 404 * @syscap SystemCapability.Ability.AbilityRuntime.Core 405 * @atomicservice 406 * @since 12 407 */ 408 /** 409 * Unregister unhandled rejection observer. 410 * 411 * @param { 'unhandledRejection' } type - error. 412 * @param { UnhandledRejectionObserver } [observer] - the registered observer 413 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 414 * 2. Incorrect parameter types; 3. Parameter verification failed. 415 * @throws { BusinessError } 16200001 - The caller has been released. 416 * @throws { BusinessError } 16300004 - The observer does not exist. 417 * @syscap SystemCapability.Ability.AbilityRuntime.Core 418 * @crossplatform 419 * @atomicservice 420 * @since 19 421 */ 422 function off(type: 'unhandledRejection', observer?: UnhandledRejectionObserver): void; 423 424 /** 425 * Register an observer for freeze event. 426 * This function can only be called from main thread. 427 * Please note that each process only supports registering one observer. 428 * If you register multiple times, the later one will overwrite the previous one. 429 * 430 * @param { 'freeze' } type - 'freeze'. 431 * @param { FreezeObserver } observer - The freeze event observer. 432 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 433 * 2. Incorrect parameter types; 3. Parameter verification failed. 434 * @syscap SystemCapability.Ability.AbilityRuntime.Core 435 * @atomicservice 436 * @since 18 437 */ 438 function on(type: 'freeze', observer: FreezeObserver): void; 439 440 /** 441 * Unregister the observer for freeze event. 442 * This function can only be called from main thread. 443 * 444 * @param { 'freeze' } type - 'freeze'. 445 * @param { FreezeObserver } observer - The freeze event observer. 446 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 447 * 2. Incorrect parameter types; 3. Parameter verification failed. 448 * @throws { BusinessError } 16300004 - The observer does not exist. 449 * @syscap SystemCapability.Ability.AbilityRuntime.Core 450 * @atomicservice 451 * @since 18 452 */ 453 function off(type: 'freeze', observer?: FreezeObserver): void; 454 455 /** 456 * The observer will be called by system when an error occurs. 457 * 458 * @syscap SystemCapability.Ability.AbilityRuntime.Core 459 * @since 9 460 */ 461 /** 462 * The observer will be called by system when an error occurs. 463 * 464 * @typedef { _ErrorObserver.default } 465 * @syscap SystemCapability.Ability.AbilityRuntime.Core 466 * @atomicservice 467 * @since 11 468 */ 469 /** 470 * The observer will be called by system when an error occurs. 471 * 472 * @typedef { _ErrorObserver.default } 473 * @syscap SystemCapability.Ability.AbilityRuntime.Core 474 * @crossplatform 475 * @atomicservice 476 * @since 19 477 */ 478 export type ErrorObserver = _ErrorObserver.default; 479 /** 480 * The observer will be called when application main thread execute timeout. 481 * 482 * @typedef { _LoopObserver } 483 * @syscap SystemCapability.Ability.AbilityRuntime.Core 484 * @atomicservice 485 * @since 12 486 */ 487 /** 488 * The observer will be called when application main thread execute timeout. 489 * 490 * @typedef { _LoopObserver } 491 * @syscap SystemCapability.Ability.AbilityRuntime.Core 492 * @crossplatform 493 * @atomicservice 494 * @since 19 495 */ 496 export type LoopObserver = _LoopObserver; 497 /** 498 * The observer will be called by system when an unhandled rejection occurs. 499 * 500 * @typedef { function } 501 * { Error | any } reason - the reason of the rejection, typically of Error type 502 * { Promise<any> } promise - the promise that is rejected 503 * @syscap SystemCapability.Ability.AbilityRuntime.Core 504 * @atomicservice 505 * @since 12 506 */ 507 /** 508 * The observer will be called by system when an unhandled rejection occurs. 509 * 510 * @typedef { function } 511 * { Error | any } reason - the reason of the rejection, typically of Error type 512 * { Promise<any> } promise - the promise that is rejected 513 * @syscap SystemCapability.Ability.AbilityRuntime.Core 514 * @crossplatform 515 * @atomicservice 516 * @since 19 517 */ 518 export type UnhandledRejectionObserver = (reason: Error | any, promise: Promise<any>) => void; 519 /** 520 * The observer will be called by system when freeze happens. 521 * 522 * @typedef { function } 523 * @syscap SystemCapability.Ability.AbilityRuntime.Core 524 * @atomicservice 525 * @since 18 526 */ 527 export type FreezeObserver = () => void; 528} 529 530export default errorManager; 531