1/* 2 * Copyright (c) 2022-2023 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 */ 40declare namespace errorManager { 41 /** 42 * Register error observer. 43 * 44 * @param { 'error' } type - error. 45 * @param { ErrorObserver } observer - The error observer. 46 * @returns { number } Returns the number code of the observer. 47 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 48 * 2. Incorrect parameter types; 3. Parameter verification failed. 49 * @throws { BusinessError } 16000003 - Id does not exist. 50 * @syscap SystemCapability.Ability.AbilityRuntime.Core 51 * @since 9 52 */ 53 /** 54 * Register error observer. 55 * 56 * @param { 'error' } type - error. 57 * @param { ErrorObserver } observer - The error observer. 58 * @returns { number } Returns the number code of the observer. 59 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 60 * 2. Incorrect parameter types; 3. Parameter verification failed. 61 * @throws { BusinessError } 16000003 - The specified ID does not exist. 62 * @syscap SystemCapability.Ability.AbilityRuntime.Core 63 * @atomicservice 64 * @since 11 65 */ 66 function on(type: 'error', observer: ErrorObserver): number; 67 68 /** 69 * Unregister error observer. 70 * 71 * @param { 'error' } type - error. 72 * @param { number } observerId - Indicates the number code of the observer. 73 * @param { AsyncCallback<void> } callback - The callback of off. 74 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 75 * 2. Incorrect parameter types; 3. Parameter verification failed. 76 * @throws { BusinessError } 16000003 - Id does not exist. 77 * @syscap SystemCapability.Ability.AbilityRuntime.Core 78 * @since 9 79 */ 80 /** 81 * Unregister error observer. 82 * 83 * @param { 'error' } type - error. 84 * @param { number } observerId - Indicates the number code of the observer. 85 * @param { AsyncCallback<void> } callback - The callback of off. 86 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 87 * 2. Incorrect parameter types; 3. Parameter verification failed. 88 * @throws { BusinessError } 16000003 - The specified ID does not exist. 89 * @syscap SystemCapability.Ability.AbilityRuntime.Core 90 * @atomicservice 91 * @since 11 92 */ 93 function off(type: 'error', observerId: number, callback: AsyncCallback<void>): void; 94 95 /** 96 * Unregister error observer. 97 * 98 * @param { 'error' } type - error. 99 * @param { number } observerId - Indicates the number code of the observer. 100 * @returns { Promise<void> } The promise returned by the function. 101 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 102 * 2. Incorrect parameter types; 3. Parameter verification failed. 103 * @throws { BusinessError } 16000003 - Id does not exist. 104 * @syscap SystemCapability.Ability.AbilityRuntime.Core 105 * @since 9 106 */ 107 /** 108 * Unregister error observer. 109 * 110 * @param { 'error' } type - error. 111 * @param { number } observerId - Indicates the number code of the observer. 112 * @returns { Promise<void> } The promise returned by the function. 113 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 114 * 2. Incorrect parameter types; 3. Parameter verification failed. 115 * @throws { BusinessError } 16000003 - The specified ID does not exist. 116 * @syscap SystemCapability.Ability.AbilityRuntime.Core 117 * @atomicservice 118 * @since 11 119 */ 120 function off(type: 'error', observerId: number): Promise<void>; 121 122 /** 123 * Register an error observer for all VM instances include worker and taskpool. 124 * @param { 'globalErrorOccurred'} type - globalErrorOccurred 125 * @param { GlobalObserver } observer - the global error observer. 126 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 127 * 2. Incorrect parameter types; 3. Parameter verification failed. 128 * @throws { BusinessError } 16200001 - If the caller is invalid. 129 * @syscap SystemCapability.Ability.AbilityRuntime.Core 130 * @atomicservice 131 * @since 18 132 */ 133 function on(type: 'globalErrorOccurred', observer: GlobalObserver): void; 134 135 /** 136 * Register a rejection observer for all VM instances include worker and taskpool. 137 * @param { 'globalUnhandledRejectionDetected'} type - globalUnhandledRejectionDetected. 138 * @param { GlobalObserver } observer - the global error observer. 139 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 140 * 2. Incorrect parameter types; 3. Parameter verification failed. 141 * @throws { BusinessError } 16200001 - If the caller is invalid. 142 * @syscap SystemCapability.Ability.AbilityRuntime.Core 143 * @atomicservice 144 * @since 18 145 */ 146 function on(type: 'globalUnhandledRejectionDetected', observer: GlobalObserver): void; 147 148 /** 149 * Unregister the error observer for all VM instance include worker and taskpool. 150 * @param { 'globalErrorOccurred'} type - globalErrorOccurred. 151 * @param { GlobalObserver } observer - the global error observer. 152 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 153 * 2. Incorrect parameter types; 3. Parameter verification failed. 154 * @throws { BusinessError } 16200001 - If the caller is invalid. 155 * @throws { BusinessError } 16300004 - If the observer does not exist 156 * @syscap SystemCapability.Ability.AbilityRuntime.Core 157 * @atomicservice 158 * @since 18 159 */ 160 function off(type: 'globalErrorOccurred', observer?: GlobalObserver): void; 161 162 /** 163 * Unregister the rejection observer for all VM instance include worker and taskpool. 164 * @param { 'globalUnhandledRejectionDetected'} type - globalUnhandledRejectionDetected. 165 * @param { GlobalObserver } observer - the global error observer. 166 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 167 * 2. Incorrect parameter types; 3. Parameter verification failed. 168 * @throws { BusinessError } 16200001 - If the caller is invalid. 169 * @throws { BusinessError } 16300004 - If the observer does not exist 170 * @syscap SystemCapability.Ability.AbilityRuntime.Core 171 * @atomicservice 172 * @since 18 173 */ 174 function off(type: 'globalUnhandledRejectionDetected', observer?: GlobalObserver): void; 175 176 /** 177 * The observer will be called by system when an error or unhandled rejection occurs 178 * from all VM instances include worker and taskpool. 179 * @typedef { function } 180 * { GlobalError } reason - the reason of the error or rejection. 181 * @syscap SystemCapability.Ability.AbilityRuntime.Core 182 * @atomicservice 183 * @since 18 184 */ 185 export type GlobalObserver = (reason: GlobalError) => void; 186 187 /** 188 * Defines GlobalError. 189 * 190 * @extends Error 191 * @typedef GlobalError 192 * @syscap SystemCapability.Ability.AbilityRuntime.Core 193 * @atomicservice 194 * @since 18 195 */ 196 export interface GlobalError extends Error { 197 /** 198 * Define the instance name of VM. 199 * @type { string } instanceName 200 * @syscap SystemCapability.Ability.AbilityRuntime.Core 201 * @atomicservice 202 * @since 18 203 */ 204 instanceName: string; 205 206 /** 207 * Define the instance type of VM. 208 * @type { InstanceType } instanceType 209 * @syscap SystemCapability.Ability.AbilityRuntime.Core 210 * @atomicservice 211 * @since 18 212 */ 213 instanceType: InstanceType; 214 } 215 216 /** 217 * Define the instance type of VM. 218 * @enum { number } 219 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 220 * @atomicservice 221 * @since 18 222 */ 223 export enum InstanceType { 224 /** 225 * Indicates it is the main VM instance. 226 * 227 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 228 * @atomicservice 229 * @since 18 230 */ 231 MAIN = 0, 232 233 /** 234 * Indicates it is the worker VM instance. 235 * 236 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 237 * @atomicservice 238 * @since 18 239 */ 240 WORKER = 1, 241 242 /** 243 * Indicates it is the taskpool VM instance. 244 * 245 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 246 * @atomicservice 247 * @since 18 248 */ 249 TASKPOOL = 2, 250 251 /** 252 * Indicates it is a VM instance created by napi_create_ark_runtime from native code by user. 253 * 254 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 255 * @atomicservice 256 * @since 18 257 */ 258 CUSTOM = 3, 259 } 260 261 /** 262 * Register loop observer. This function can only by called from main thread, 263 * and if call this function multiple times, the last 264 * modification will overwrite the previous one. 265 * 266 * @param { 'loopObserver' } type - loopObserver. 267 * @param { number } timeout - Indicates timeout(ms) value of loop observer. 268 * @param { LoopObserver } observer - The loop observer. 269 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 270 * 2. Incorrect parameter types; 3. Parameter verification failed. 271 * @syscap SystemCapability.Ability.AbilityRuntime.Core 272 * @atomicservice 273 * @since 12 274 */ 275 function on(type: 'loopObserver', timeout: number, observer: LoopObserver): void; 276 277 /** 278 * Unregister loop observer. This function can only by called from main thread. 279 * 280 * @param { 'loopObserver' } type - loopObserver. 281 * @param { LoopObserver } observer - The loop observer. 282 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 283 * 2. Incorrect parameter types; 3. Parameter verification failed. 284 * @syscap SystemCapability.Ability.AbilityRuntime.Core 285 * @atomicservice 286 * @since 12 287 */ 288 function off(type: 'loopObserver', observer?: LoopObserver): void; 289 290 /** 291 * Register unhandled rejection observer. 292 * 293 * @param { 'unhandledRejection' } type - 'unhandledRejection'. 294 * @param { UnhandledRejectionObserver } observer - The unhandled rejection observer. 295 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 296 * 2. Incorrect parameter types; 3. Parameter verification failed. 297 * @throws { BusinessError } 16200001 - If the caller is invalid. 298 * @syscap SystemCapability.Ability.AbilityRuntime.Core 299 * @atomicservice 300 * @since 12 301 */ 302 function on(type: 'unhandledRejection', observer: UnhandledRejectionObserver): void; 303 304 /** 305 * Unregister unhandled rejection observer. 306 * 307 * @param { 'unhandledRejection' } type - error. 308 * @param { UnhandledRejectionObserver } [observer] - the registered observer 309 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 310 * 2. Incorrect parameter types; 3. Parameter verification failed. 311 * @throws { BusinessError } 16200001 - If the caller is invalid. 312 * @throws { BusinessError } 16300004 - If the observer does not exist 313 * @syscap SystemCapability.Ability.AbilityRuntime.Core 314 * @atomicservice 315 * @since 12 316 */ 317 function off(type: 'unhandledRejection', observer?: UnhandledRejectionObserver): void; 318 319 /** 320 * Register an observer for freeze event. 321 * This function can only be called from main thread. 322 * Please note that each process only supports registering one observer. 323 * If you register multiple times, the later one will overwrite the previous one. 324 * 325 * @param { 'freeze' } type - 'freeze'. 326 * @param { FreezeObserver } observer - The freeze event observer. 327 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 328 * 2. Incorrect parameter types; 3. Parameter verification failed. 329 * @syscap SystemCapability.Ability.AbilityRuntime.Core 330 * @atomicservice 331 * @since 18 332 */ 333 function on(type: 'freeze', observer: FreezeObserver): void; 334 335 /** 336 * Unregister the observer for freeze event. 337 * This function can only be called from main thread. 338 * 339 * @param { 'freeze' } type - 'freeze'. 340 * @param { FreezeObserver } observer - The freeze event observer. 341 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 342 * 2. Incorrect parameter types; 3. Parameter verification failed. 343 * @throws { BusinessError } 16300004 - If the observer does not exist 344 * @syscap SystemCapability.Ability.AbilityRuntime.Core 345 * @atomicservice 346 * @since 18 347 */ 348 function off(type: 'freeze', observer?: FreezeObserver): void; 349 350 /** 351 * The observer will be called by system when an error occurs. 352 * 353 * @syscap SystemCapability.Ability.AbilityRuntime.Core 354 * @since 9 355 */ 356 /** 357 * The observer will be called by system when an error occurs. 358 * 359 * @typedef { _ErrorObserver.default } 360 * @syscap SystemCapability.Ability.AbilityRuntime.Core 361 * @atomicservice 362 * @since 11 363 */ 364 export type ErrorObserver = _ErrorObserver.default; 365 /** 366 * The observer will be called when application main thread execute timeout. 367 * 368 * @typedef { _LoopObserver } 369 * @syscap SystemCapability.Ability.AbilityRuntime.Core 370 * @atomicservice 371 * @since 12 372 */ 373 export type LoopObserver = _LoopObserver; 374 /** 375 * The observer will be called by system when an unhandled rejection occurs. 376 * 377 * @typedef { function } 378 * { Error | any } reason - the reason of the rejection, typically of Error type 379 * { Promise<any> } promise - the promise that is rejected 380 * @syscap SystemCapability.Ability.AbilityRuntime.Core 381 * @atomicservice 382 * @since 12 383 */ 384 export type UnhandledRejectionObserver = (reason: Error | any, promise: Promise<any>) => void; 385 /** 386 * The observer will be called by system when freeze happens. 387 * 388 * @typedef { function } 389 * @syscap SystemCapability.Ability.AbilityRuntime.Core 390 * @atomicservice 391 * @since 18 392 */ 393 export type FreezeObserver = () => void; 394} 395 396export default errorManager; 397