Skip to content

Commit

Permalink
Handle retries for spec fetching.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Apr 25, 2024
1 parent 0a7d4d9 commit 8c29fdf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This package follows standard semvar, `<major>.<minor>.<build>`. No breaking cha
* Fix default display when the value is falsy.
* Remove deprecated property `nav-item-spacing` in favor of css variable `--nav-path-padding`.
* Prevent making requests when required path parameters are not specified.
* Automatically retry fetching the spec if it doesn't work for any reason.

## 2.1
* Add `x-locale` vendor extension to specify the locale of the spec.
Expand Down
20 changes: 14 additions & 6 deletions src/utils/spec-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ export default async function ProcessSpec(specUrlOrObject, serverUrl = '') {
const inputSpecIsAUrl = typeof specUrlOrObject === 'string' && specUrlOrObject.match(/^http/) || typeof specUrlOrObject === 'object' && typeof specUrlOrObject.href === 'string';

let jsonParsedSpec;
try {
jsonParsedSpec = await OpenApiResolver(specUrlOrObject);
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error parsing specification', error);
throw Error(`Failed to resolve the spec: ${error.message}`);
let errorToDisplay;
for (let iteration = 0; iteration < 7; iteration++) {
try {
jsonParsedSpec = await OpenApiResolver(specUrlOrObject);
break;
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error parsing specification', error);
errorToDisplay = error.message;
await new Promise(resolve => setTimeout(resolve, 100 * 2 ** iteration));
}
}

if (!jsonParsedSpec) {
if (errorToDisplay) {
throw Error(`Failed to resolve the spec: ${errorToDisplay}`);
}
throw Error('SpecificationNotFound');
}

Expand Down

0 comments on commit 8c29fdf

Please sign in to comment.