UNPKG

7.64 kBTypeScriptView Raw
1import { StringValueMap } from '../data/StringValueMap';
2/**
3 * Defines a base class to defive various application exceptions.
4 *
5 * Most languages have own definition of base exception (error) types.
6 * However, this class is implemented symmetrically in all languages
7 * supported by PipServices toolkit. It allows to create portable implementations
8 * and support proper error propagation in microservices calls.
9 *
10 * Error propagation means that when microservice implemented in one language
11 * calls microservice(s) implemented in a different language(s), errors are returned
12 * throught the entire call chain and restored in their original (or close) type.
13 *
14 * Since number of potential exception types is endless, PipServices toolkit
15 * supports only 12 standard categories of exceptions defined in [[ErrorCategory]].
16 * This [[ApplicationException]] class acts as a basis for
17 * all other 12 standard exception types.
18 *
19 * Most exceptions have just free-form message that describes occured error.
20 * That may not be sufficient to create meaninful error descriptions.
21 * The [[ApplicationException]] class proposes an extended error definition
22 * that has more standard fields:
23 *
24 * - message: is a human-readable error description
25 * - category: one of 12 standard error categories of errors
26 * - status: numeric HTTP status code for REST invocations
27 * - code: a unique error code, usually defined as "MY_ERROR_CODE"
28 * - correlation_id: a unique transaction id to trace execution through a call chain
29 * - details: map with error parameters that can help to recreate meaningful error description in other languages
30 * - stack_trace: a stack trace
31 * - cause: original error that is wrapped by this exception
32 *
33 * ApplicationException class is not serializable. To pass errors through the wire
34 * it is converted into [[ErrorDescription]] object and restored on receiving end into
35 * identical exception type.
36 *
37 * @see [[ErrorCategory]]
38 * @see [[ErrorDescription]]
39 */
40export declare class ApplicationException extends Error {
41 /** A human-readable error description (usually written in English) */
42 message: string;
43 /** Standard error category */
44 category: string;
45 /** HTTP status code associated with this error type */
46 status: number;
47 /** A unique error code */
48 code: string;
49 /** A map with additional details that can be used to restore error description in other languages */
50 details: StringValueMap;
51 /** A unique transaction id to trace execution throug call chain */
52 correlation_id: string;
53 /** Stack trace of the exception */
54 stack_trace: string;
55 /** Original error wrapped by this exception */
56 cause: string;
57 /**
58 * Creates a new instance of application exception and assigns its values.
59 *
60 * @param category (optional) a standard error category. Default: Unknown
61 * @param correlation_id (optional) a unique transaction id to trace execution through call chain.
62 * @param code (optional) a unique error code. Default: "UNKNOWN"
63 * @param message (optional) a human-readable description of the error.
64 */
65 constructor(category?: string, correlation_id?: string, code?: string, message?: string);
66 /**
67 * Gets original error wrapped by this exception as a string message.
68 *
69 * @returns an original error message.
70 */
71 getCauseString(): string;
72 /**
73 * Sets original error wrapped by this exception as a string message.
74 *
75 * @param value an original error message.
76 */
77 setCauseString(value: string): void;
78 /**
79 * Gets a stack trace where this exception occured.
80 *
81 * @returns a stack trace as a string.
82 */
83 getStackTraceString(): string;
84 /**
85 * Sets a stack trace where this exception occured.
86 *
87 * @param value a stack trace as a string
88 */
89 setStackTraceString(value: string): void;
90 /**
91 * Sets a unique error code.
92 *
93 * This method returns reference to this exception to implement Builder pattern
94 * to chain additional calls.
95 *
96 * @param code a unique error code
97 * @returns this exception object
98 */
99 withCode(code: string): ApplicationException;
100 /**
101 * Sets a original error wrapped by this exception
102 *
103 * This method returns reference to this exception to implement Builder pattern
104 * to chain additional calls.
105 *
106 * @param cause original error object
107 * @returns this exception object
108 */
109 withCause(cause: Error): ApplicationException;
110 /**
111 * Sets a HTTP status code which shall be returned by REST calls.
112 *
113 * This method returns reference to this exception to implement Builder pattern
114 * to chain additional calls.
115 *
116 * @param status an HTTP error code.
117 * @returns this exception object
118 */
119 withStatus(status: number): ApplicationException;
120 /**
121 * Sets a parameter for additional error details.
122 * This details can be used to restore error description in other languages.
123 *
124 * This method returns reference to this exception to implement Builder pattern
125 * to chain additional calls.
126 *
127 * @param key a details parameter name
128 * @param value a details parameter name
129 * @returns this exception object
130 */
131 withDetails(key: string, value: any): ApplicationException;
132 /**
133 * Sets a correlation id which can be used to trace this error through a call chain.
134 *
135 * This method returns reference to this exception to implement Builder pattern
136 * to chain additional calls.
137 *
138 * @param correlationId a unique transaction id to trace error through call chain
139 * @returns this exception object
140 */
141 withCorrelationId(correlationId: string): ApplicationException;
142 /**
143 * Sets a stack trace for this error.
144 *
145 * This method returns reference to this exception to implement Builder pattern
146 * to chain additional calls.
147 *
148 * @param stackTrace a stack trace where this error occured
149 * @returns this exception object
150 */
151 withStackTrace(stackTrace: string): ApplicationException;
152 /**
153 * Wraps another exception into an application exception object.
154 *
155 * If original exception is of ApplicationException type it is returned without changes.
156 * Otherwise a new ApplicationException is created and original error is set as its cause.
157 *
158 * @param cause an original error object
159 * @returns an original or newly created ApplicationException
160 */
161 wrap(cause: any): ApplicationException;
162 /**
163 * Wraps another exception into specified application exception object.
164 *
165 * If original exception is of ApplicationException type it is returned without changes.
166 * Otherwise the original error is set as a cause to specified ApplicationException object.
167 *
168 * @param error an ApplicationException object to wrap the cause
169 * @param cause an original error object
170 * @returns an original or newly created ApplicationException
171 *
172 * @see [[wrap]]
173 */
174 static wrapError(error: ApplicationException, cause: any): ApplicationException;
175 /**
176 * Unwraps original exception through wrapped exception objects.
177 *
178 * Many frameworks like Seneca or restify wrap original exception.
179 * That may result in propagating less specific errors and can hide
180 * causes of the errors.
181 *
182 * @param error an error object
183 * @returns an original error object
184 */
185 static unwrapError(error: any): any;
186}