this repo has no description
1import { isSome } from "@jet/environment";
2import * as validation from "@jet/environment/json/validation";
3import { tryAwait } from "./promise-util";
4/**
5 * Creates a validation context for async operations and ensures proper cleanup.
6 *
7 * Wraps an async operation in a validation context, automatically handling
8 * context cleanup even when errors occur. Optionally processes errors through
9 * a provided error handler.
10 *
11 * @param name - The name for the validation context
12 * @param producer - Async function that produces the result
13 * @param errorHandler - Optional function to handle errors from the producer
14 * @returns The result of the async operation
15 */
16export async function withAsyncValidationContext(name, producer, errorHandler) {
17 validation.beginContext(name);
18 let result = await tryAwait(producer());
19 if (!result.success && isSome(errorHandler)) {
20 result = await tryAwait(errorHandler(result.error));
21 }
22 validation.endContext();
23 if (!result.success) {
24 throw result.error;
25 }
26 else {
27 return result.value;
28 }
29}
30//# sourceMappingURL=validation-util.js.map