/** * Copyright 2020 Angus.Fenying * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ export declare type DefaultMetadataType = Record; /** * Describe the basic information of an error. */ export interface IErrorData { /** * The numeric-code of an error, used to identify the type or the reason * of the error. */ code: number; /** * The string-code of an error, as the name, used to identify the type or * the reason of the error. */ name: string; /** * The detail description of an error, used to describe the details of the * error. */ message: string; /** * The metadata of error. */ metadata: M; /** * The name of module thats emit this error. */ module: string; } /** * Describe the full information of an error. */ export interface IErrorFullData extends IErrorData { /** * The calling-stack of the position where the error occurred. */ stack: string[]; } /** * The LiteRT standard error objects. */ export interface IError { /** * The numeric-code of an error, used to identify the type or the reason * of the error. */ readonly code: number; /** * The string-code of an error, as the name, used to identify the type or * the reason of the error. */ readonly name: string; /** * The detail description of an error, used to describe the details of the * error. */ readonly message: string; /** * The calling-stack of the position where the error occurred. */ readonly stack: string; /** * The metadata of error. */ readonly metadata: M; /** * The name of module thats emit this error. */ readonly module: string; /** * Get the calling-stack as a string array. */ getStackAsArray(): string[]; /** * Stringify the error to be printable. */ toString(): string; /** * Convert to a raw data object that could be stringified as JSON. */ toJSON(withStack?: false): IErrorData; toJSON(withStack: true): IErrorFullData; /** * Emit as a warning. */ warn(): this; } /** * The constructor of error objects. */ export interface IErrorConstructor { /** * The constructor of the objects of the error. */ new (opts?: { message?: string; metadata?: Partial; }): IError; /** * The name of the error. */ readonly name: string; /** * The default description of the error. */ readonly message: string; /** * The code of the error. */ readonly code: number; /** * The name of module thats emit this error. */ readonly module: string; /** * The default value of metadata. */ readonly defaultMetadata: M; } /** * A hub of errors, is a collection and factory of error types. * * Every hub has a standalone namespace of error types. */ export interface IErrorHub { /** * The name of module thats emit this error. */ readonly module: string; codeIndex: number; /** * Define a new error type. * * @param code The unique numeric-identity for the new error type. * The code is generated automatically if set to null. * @param name The unique string-identity for the new error type. * @param message The description for the new error type. * @param metadata The metadata of this new error. */ define(code: number | null, name: string, message: string, metadata: M): IErrorConstructor; /** * Get the error constructor by its name or code. * * @param identity The string-identity or code-identity for the error type. */ get(identity: string | number): IErrorConstructor; /** * Check if an error belongs to an error type defined in this hub. * * @param e The error to be checked. * @param id The name or code of error type to be checked. */ is(e: any, id?: string | number): e is IError; /** * Emit a warning. * * @param e The details of warning. */ warn(e: IError): this; /** * Force throwing warnings as errors. * * @param enabled Set to true to force throwing warnings as exceptions. [Default: true] */ forceWarningAsError(enabled?: boolean): this; /** * Add a new listener callback for warnings. * * @param key The key of listener. * @param listener The callback of listener. */ addWarningListener(key: string, listener: (e: IError) => void): this; /** * Remove an existing listener by key. * * @param key The key of listener. */ removeWarningListener(key: string): this; } /** * The default name for module of errors, if omitted. */ export declare const DEFAULT_ERROR_HUB_MODULE = "unknown"; export declare const DEFAULT_WARNING_LISTENER_KEY = "litert:errors:hub:default-warning-listener"; export declare const DEFAULT_WARNING_LISTENER: (e: IError) => void; /** * Create a new error hub that has a standalone namespace of error types. */ export declare function createErrorHub(moduleName?: string): IErrorHub; /** * Get the default hub of errors. */ export declare function getDefaultErrorHub(): IErrorHub; /** * Check if an object is a LiteRT error object. * * @param e The error object to be identified. */ export declare function isError(e: unknown): e is IError; //# sourceMappingURL=Error.d.ts.map