Skip to content

Commit 7f79b53

Browse files
authored
Merge pull request #670 from Merit-Systems/br/better-error-raising-513
better error raising
2 parents bc47942 + f1a1884 commit 7f79b53

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

packages/app/server/src/providers/VertexAIProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ export class VertexAIProvider extends BaseProvider {
473473

474474
private getServiceAccountCredentials(): any {
475475
const serviceAccountKeyEncoded = env.GOOGLE_SERVICE_ACCOUNT_KEY_ENCODED;
476-
476+
477477
if (!serviceAccountKeyEncoded) {
478478
return null;
479479
}

packages/app/server/src/services/facilitator/facilitatorProxy.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,37 @@ const facilitatorTimeout = env.FACILITATOR_REQUEST_TIMEOUT || 20000;
1717

1818
type FacilitatorMethod = 'verify' | 'settle';
1919

20+
async function fetchWithTimeout(
21+
url: string,
22+
options: RequestInit,
23+
timeoutMs: number,
24+
method: FacilitatorMethod
25+
): Promise<Response> {
26+
const abortController = new AbortController();
27+
const timeoutId = setTimeout(() => {
28+
abortController.abort();
29+
logger.warn(
30+
`Proxy facilitator ${method} request timed out after ${timeoutMs}ms`
31+
);
32+
}, Number(timeoutMs));
33+
34+
try {
35+
const res = await fetch(url, {
36+
...options,
37+
signal: abortController.signal,
38+
});
39+
clearTimeout(timeoutId);
40+
return res;
41+
} catch (error) {
42+
clearTimeout(timeoutId);
43+
logMetric('facilitator_proxy_failure', 1, {
44+
method,
45+
error: error instanceof Error ? error.message : 'unknown',
46+
});
47+
throw new FacilitatorProxyError();
48+
}
49+
}
50+
2051
/**
2152
* Executes a facilitator request via the proxy router
2253
*
@@ -53,21 +84,16 @@ export async function facilitatorProxy<
5384
paymentRequirements: toJsonSafe(paymentRequirements),
5485
};
5586

56-
const abortController = new AbortController();
57-
const timeoutId = setTimeout(() => {
58-
abortController.abort();
59-
logger.warn(
60-
`Proxy facilitator ${method} request timed out after ${facilitatorTimeout}ms`
61-
);
62-
}, Number(facilitatorTimeout));
63-
const res = await fetch(`${PROXY_FACILITATOR_URL}/${method}`, {
64-
method: 'POST',
65-
headers,
66-
body: JSON.stringify(requestBody),
67-
signal: abortController.signal,
68-
});
69-
70-
clearTimeout(timeoutId);
87+
const res = await fetchWithTimeout(
88+
`${PROXY_FACILITATOR_URL}/${method}`,
89+
{
90+
method: 'POST',
91+
headers,
92+
body: JSON.stringify(requestBody),
93+
},
94+
facilitatorTimeout,
95+
method
96+
);
7197

7298
if (res.status !== 200) {
7399
logMetric('facilitator_proxy_failure', 1, {

0 commit comments

Comments
 (0)