Skip to content

Commit

Permalink
fix: add retry and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-zippenfenig committed Nov 1, 2023
1 parent 98c7473 commit 7a37a8d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
52 changes: 45 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
import { ByteBuffer } from 'flatbuffers';
import { WeatherApiResponse } from '@openmeteo/sdk/weather-api-response';

const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));

async function fetchRetried(
url: string,
retries = 3,
backoffFactor = 0.5,
backoffMax = 2
): Promise<Response> {
const statusToRetry = [500, 502, 504];
const statusWithJsonError = [400, 429];
let currentTry = 0;
let response = await fetch(url);

while (statusToRetry.includes(response.status)) {
currentTry++;
if (currentTry >= retries) {
throw new Error(response.statusText);
}
const sleepMs =
Math.min(backoffFactor * 2 ** currentTry, backoffMax) * 1000;
await sleep(sleepMs);
response = await fetch(url);
}

if (statusWithJsonError.includes(response.status)) {
const json = await response.json();
if ('reason' in json) {
throw new Error((json as { reason: string }).reason);
}
throw new Error(response.statusText);
}
return response;
}

async function fetchWeatherApi(
url: string,
params: any
params: any,
retries = 3,
backoffFactor = 0.2,
backoffMax = 2
): Promise<WeatherApiResponse[]> {
const urlParams = new URLSearchParams(params);
urlParams.set('format', 'flatbuffers');
//console.log(`${url}?${urlParams.toString()}`);
const response = await fetch(`${url}?${urlParams.toString()}`);
const bb = await response.arrayBuffer();
const fb = new ByteBuffer(new Uint8Array(bb));

// TODO: retry, error handling
const response = await fetchRetried(
`${url}?${urlParams.toString()}`,
retries,
backoffFactor,
backoffMax
);
const fb = new ByteBuffer(new Uint8Array(await response.arrayBuffer()));

const results: WeatherApiResponse[] = [];
let pos = 0;
Expand Down
47 changes: 47 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('openmeteo', () => {
// 2 location data, with hourly temp and precip
global.fetch = jest.fn(() =>
Promise.resolve({
status: 200,
arrayBuffer: () =>
Promise.resolve(
new Uint8Array([
Expand Down Expand Up @@ -92,3 +93,49 @@ describe('openmeteo', () => {
});
});
});

describe('openmeteo', () => {
describe('client', () => {
test('test_error', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
status: 400,
json: () => Promise.resolve({ reason: 'Some error' }),
})
) as jest.Mock;
await expect(fetchWeatherApi('', {})).rejects.toThrow('Some error');
});
});
});

describe('openmeteo', () => {
describe('client', () => {
test('test_unknown_error', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
status: 400,
statusText: 'Other error',
json: () => Promise.resolve({}),
})
) as jest.Mock;
await expect(fetchWeatherApi('', {})).rejects.toThrow('Other error');
});
});
});

describe('openmeteo', () => {
describe('client', () => {
test('test_retry', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
status: 500,
statusText: 'Internal server error',
})
) as jest.Mock;
await expect(fetchWeatherApi('', {})).rejects.toThrow(
'Internal server error'
);
expect(fetch).toHaveBeenCalledTimes(3);
});
});
});

0 comments on commit 7a37a8d

Please sign in to comment.