import { ChildKey, NodeDefinition, StaticGraphNode, StaticNodeDefinition, StaticNodeType } from '../../types/graph'; export declare class MusterError extends Error { static is(value: any): value is MusterError; error: Error | { message: string; stack?: string; }; message: string; stack: string | undefined; code: string | undefined; data: any | undefined; path: Array | undefined; remotePath: Array | undefined; constructor(error: Error | { message: string; stack?: string; }, options: { code: string | undefined; data: any | undefined; path: Array | undefined; remotePath: Array | undefined; }); } /** * An instance of the [[error]] node. * See the [[error]] documentation to find out more. */ export interface ErrorNode extends StaticGraphNode<'error', ErrorNodeProperties, SerializedErrorNodeProperties> { } /** * A definition of the [[error]] node. * See the [[error]] documentation to find out more. */ export interface ErrorNodeDefinition extends StaticNodeDefinition<'error', ErrorNodeProperties, SerializedErrorNodeProperties> { } export interface ErrorNodeProperties { error: Error | { message: string; stack?: string; }; code: string | undefined; data: any | undefined; path: Array | undefined; remotePath: Array | undefined; } export interface SerializedErrorNodeProperties { error: { message: string; stack?: string; }; code: string | undefined; data: any | undefined; path: Array | undefined; remotePath: Array | undefined; } /** * The implementation of the [[error]] node. * See the [[error]] documentation to learn more. */ export declare const ErrorNodeType: StaticNodeType<'error', ErrorNodeProperties, SerializedErrorNodeProperties>; export interface ErrorNodeOptions { data?: any; code?: string; } /** * Creates a new instance of an [[error]] node, which is a type of [[NodeDefinition]] used by Muster when an error has occurred. * This node contains following information: * - error - the instance of the caught exception * - data - additional data provided at the time of error's creation * - path - path in the graph that caused this error * - remotePath - path in the remote graph that caused this error * * Every exception raised in Muster is caught and converted to this [[NodeDefinition]]. It allows for * more fine-grained control of what to do when an error has occurred. Like the * [[pending]], the [[error]] is usually short-circuited and returned to the subscriber. * * Muster also comes with a helper node that allows catching errors and replacing them with a * fallback value. See the [[ifError]] documentation to learn more. * * * @example **Error short-circuiting** * ```js * import muster, { computed, ref } from '@dws/muster'; * * const app = muster({ * throwError: computed([], () => { * console.log('Throwing an error'); * throw new Error('Boom!'); * }), * computeSomething: computed([ref('throwError')], (val) => { * console.log('Computing something'); * return val + 1; * }), * }); * * const result = await app.resolve(ref('computeSomething')); * // result === new Error('Boom!') * // result.path === ['throwError'] * console.log('End'); * * // Console output: * // Throwing an error * // End * ``` * This example shows how the short-circuiting mechanism works. Note that `computeSomething` * does not log anything, as the content of the node is never run. This prevents the application * from getting into an inconsistent state. * * * @example **Returning a custom error** * ```js * import muster, { computed, error, ref, set, variable } from '@dws/muster'; * * const app = muster({ * age: variable(25), * spirits: computed([ref('age')], (age) => { * if (age < 18) { * return error('Alcohol cannot be sold to people under 18!'); * } * return ['Beer', 'Gin', 'Whisky', 'Wine']; * }), * }); * * console.log('Subscribing to spirits'); * app.resolve(ref('spirits')).subscribe((res) => { * console.log(res); * }); * * console.log('Changing age to 17') * await app.resolve(set('age', 17)); * * // Console output: * // Subscribing to spirits * // ['Beer', 'Gin', 'Whisky', 'Wine'] * // Changing age to 17 * // new Error('Alcohol cannot be sold to people under 18!') * ``` * This example shows how to report custom errors from muster. They obey the same rules as * internally reported errors. The console output misses one fact that the error in an actual * Error object, which contains a stack trace. */ export declare function error(err: string | Error | { message: string; stack?: string; } | ErrorNodeDefinition, options?: ErrorNodeOptions): ErrorNodeDefinition; export interface ErrorPathOptions { path?: Array; remotePath?: Array; } export declare function withErrorPath(error: ErrorNodeDefinition, options: ErrorPathOptions): ErrorNodeDefinition; export declare function withErrorPath(error: ErrorNode, options: ErrorPathOptions): ErrorNode; export declare function withErrorPath(error: ErrorNodeDefinition | ErrorNode, options: ErrorPathOptions): ErrorNodeDefinition | ErrorNode; export declare function isErrorNodeDefinition(value: NodeDefinition): value is ErrorNodeDefinition;