Skip to content

Commit

Permalink
Fix torus-cli build
Browse files Browse the repository at this point in the history
  • Loading branch information
hieu-w committed Jan 20, 2025
1 parent 5a9493c commit 2d5b410
Showing 1 changed file with 43 additions and 39 deletions.
82 changes: 43 additions & 39 deletions src/some.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export const Some = <K, T>(promises: Promise<K>[], predicate: (resultArr: K[], {
let predicateError: Error | string;

promises.forEach((x, index) => {
x.then((resp: K) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
x.then((resp: K): any => {
resultArr[index] = resp;
return undefined;
})
Expand All @@ -56,47 +57,50 @@ export const Some = <K, T>(promises: Promise<K>[], predicate: (resultArr: K[], {
// eslint-disable-next-line promise/no-return-in-finally
.finally(() => {
if (sharedState.resolved) return;
return predicate(resultArr.slice(0), sharedState)
.then((data) => {
sharedState.resolved = true;
resolve(data);
return undefined;
})
.catch((error) => {
// log only the last predicate error
predicateError = error;
})
.finally(() => {
finishedCount += 1;
if (finishedCount === promises.length) {
const errors = Object.values(
resultArr.reduce((acc: Record<string, string>, z) => {
if (z) {
const { id, error } = z as { id?: string; error?: { data?: string } };
if (error?.data?.length > 0) {
if (error.data.startsWith("Error occurred while verifying params")) acc[id] = capitalizeFirstLetter(error.data);
else acc[id] = error.data;
return (
predicate(resultArr.slice(0), sharedState)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.then((data): any => {
sharedState.resolved = true;
resolve(data);
return undefined;
})
.catch((error) => {
// log only the last predicate error
predicateError = error;
})
.finally(() => {
finishedCount += 1;
if (finishedCount === promises.length) {
const errors = Object.values(
resultArr.reduce((acc: Record<string, string>, z) => {
if (z) {
const { id, error } = z as { id?: string; error?: { data?: string } };
if (error?.data?.length > 0) {
if (error.data.startsWith("Error occurred while verifying params")) acc[id] = capitalizeFirstLetter(error.data);
else acc[id] = error.data;
}
}
}
return acc;
}, {})
);

if (errors.length > 0) {
// Format-able errors
const msg = errors.length > 1 ? `\n${errors.map((it) => `• ${it}`).join("\n")}` : errors[0];
reject(new Error(msg));
} else {
reject(
new SomeError({
errors: errorArr,
responses: resultArr,
predicate: (predicateError as Error)?.message || (predicateError as string),
})
return acc;
}, {})
);

if (errors.length > 0) {
// Format-able errors
const msg = errors.length > 1 ? `\n${errors.map((it) => `• ${it}`).join("\n")}` : errors[0];
reject(new Error(msg));
} else {
reject(
new SomeError({
errors: errorArr,
responses: resultArr,
predicate: (predicateError as Error)?.message || (predicateError as string),
})
);
}
}
}
});
})
);
});
});
});

0 comments on commit 2d5b410

Please sign in to comment.