mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-12 04:15:30 +00:00
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
|
|
import * as Sentry from "@sentry/node";
|
||
|
|
|
||
|
|
type ValidResult<T> = {
|
||
|
|
type: 'valid';
|
||
|
|
data: T[];
|
||
|
|
};
|
||
|
|
|
||
|
|
type NotFoundResult = {
|
||
|
|
type: 'notFound';
|
||
|
|
value: string;
|
||
|
|
};
|
||
|
|
|
||
|
|
type CustomResult<T> = ValidResult<T> | NotFoundResult;
|
||
|
|
|
||
|
|
export function processPromiseResults<T>(
|
||
|
|
results: PromiseSettledResult<CustomResult<T>>[],
|
||
|
|
): {
|
||
|
|
validItems: T[];
|
||
|
|
notFoundItems: string[];
|
||
|
|
} {
|
||
|
|
const validItems: T[] = [];
|
||
|
|
const notFoundItems: string[] = [];
|
||
|
|
|
||
|
|
results.forEach(result => {
|
||
|
|
if (result.status === 'fulfilled') {
|
||
|
|
const value = result.value;
|
||
|
|
if (value.type === 'valid') {
|
||
|
|
validItems.push(...value.data);
|
||
|
|
} else {
|
||
|
|
notFoundItems.push(value.value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
return {
|
||
|
|
validItems,
|
||
|
|
notFoundItems,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export function throwIfAnyFailed<T>(results: PromiseSettledResult<T>[]) {
|
||
|
|
const failedResult = results.find(result => result.status === 'rejected');
|
||
|
|
if (failedResult) {
|
||
|
|
Sentry.captureException(failedResult.reason);
|
||
|
|
throw failedResult.reason;
|
||
|
|
}
|
||
|
|
}
|