fix: make property-assessor safer for whatsapp runs
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { discoverHarListing } from "../../web-automation/scripts/har-discover.js";
|
||||
import { discoverZillowListing } from "../../web-automation/scripts/zillow-discover.js";
|
||||
import { TimeoutError, withTimeout } from "./async-timeout.js";
|
||||
|
||||
export interface ListingDiscoveryResult {
|
||||
attempts: string[];
|
||||
@@ -7,29 +8,65 @@ export interface ListingDiscoveryResult {
|
||||
harUrl: string | null;
|
||||
}
|
||||
|
||||
export async function discoverListingSources(address: string): Promise<ListingDiscoveryResult> {
|
||||
interface ListingDiscoveryDeps {
|
||||
timeoutMs?: number;
|
||||
discoverZillowListingFn?: typeof discoverZillowListing;
|
||||
discoverHarListingFn?: typeof discoverHarListing;
|
||||
}
|
||||
|
||||
const DEFAULT_DISCOVERY_TIMEOUT_MS = Number(
|
||||
process.env.PROPERTY_ASSESSOR_DISCOVERY_TIMEOUT_MS || 20_000
|
||||
);
|
||||
|
||||
export async function discoverListingSources(
|
||||
address: string,
|
||||
deps: ListingDiscoveryDeps = {}
|
||||
): Promise<ListingDiscoveryResult> {
|
||||
const attempts: string[] = [];
|
||||
let zillowUrl: string | null = null;
|
||||
let harUrl: string | null = null;
|
||||
const timeoutMs = deps.timeoutMs ?? DEFAULT_DISCOVERY_TIMEOUT_MS;
|
||||
const discoverZillowListingFn = deps.discoverZillowListingFn || discoverZillowListing;
|
||||
const discoverHarListingFn = deps.discoverHarListingFn || discoverHarListing;
|
||||
|
||||
try {
|
||||
const result = await discoverZillowListing(address);
|
||||
const result = await withTimeout(
|
||||
() => discoverZillowListingFn(address),
|
||||
{
|
||||
operationName: "Zillow discovery",
|
||||
timeoutMs
|
||||
}
|
||||
);
|
||||
zillowUrl = result.listingUrl;
|
||||
attempts.push(...result.attempts);
|
||||
} catch (error) {
|
||||
attempts.push(
|
||||
`Zillow discovery failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
if (error instanceof TimeoutError) {
|
||||
attempts.push(`Zillow discovery timed out after ${timeoutMs}ms.`);
|
||||
} else {
|
||||
attempts.push(
|
||||
`Zillow discovery failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await discoverHarListing(address);
|
||||
const result = await withTimeout(
|
||||
() => discoverHarListingFn(address),
|
||||
{
|
||||
operationName: "HAR discovery",
|
||||
timeoutMs
|
||||
}
|
||||
);
|
||||
harUrl = result.listingUrl;
|
||||
attempts.push(...result.attempts);
|
||||
} catch (error) {
|
||||
attempts.push(
|
||||
`HAR discovery failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
if (error instanceof TimeoutError) {
|
||||
attempts.push(`HAR discovery timed out after ${timeoutMs}ms.`);
|
||||
} else {
|
||||
attempts.push(
|
||||
`HAR discovery failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user