Skip to content

Commit 7bb19d2

Browse files
committed
Wrapping parsing of 426 HTTP responses in a try-catch
1 parent 1274c6f commit 7bb19d2

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

packages/compass/src/main/auto-update-manager.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -673,31 +673,41 @@ class CompassAutoUpdateManager {
673673
const response = await this.fetch((await this.getUpdateCheckURL()).href);
674674

675675
if (response.status === 426) {
676-
const json = await response.json();
677-
assert(
678-
typeof json === 'object' && json !== null,
679-
'Expected response to be an object'
680-
);
681-
if ('reason' in json && json.reason === 'outdated-operating-system') {
676+
try {
677+
const json = await response.json();
682678
assert(
683-
'expectedVersion' in json,
684-
"Expected 'expectedVersion' in response"
679+
typeof json === 'object' && json !== null,
680+
'Expected response to be an object'
685681
);
686-
const { expectedVersion } = json;
687-
assert(
688-
typeof expectedVersion === 'string',
689-
"Expected 'expectedVersion' in response"
682+
if ('reason' in json && json.reason === 'outdated-operating-system') {
683+
assert(
684+
'expectedVersion' in json,
685+
"Expected 'expectedVersion' in response"
686+
);
687+
const { expectedVersion } = json;
688+
assert(
689+
typeof expectedVersion === 'string',
690+
"Expected 'expectedVersion' in response"
691+
);
692+
return {
693+
available: false,
694+
reason: 'outdated-operating-system',
695+
expectedVersion,
696+
};
697+
} else {
698+
// Some future reason that no update is available
699+
return {
700+
available: false,
701+
};
702+
}
703+
} catch (err) {
704+
log.warn(
705+
mongoLogId(1_001_000_347),
706+
'AutoUpdateManager',
707+
'Failed to parse HTTP 426 (Upgrade Required) response',
708+
{ error: err instanceof Error ? err.message : 'Unknown error' }
690709
);
691-
return {
692-
available: false,
693-
reason: 'outdated-operating-system',
694-
expectedVersion,
695-
};
696-
} else {
697-
// Some future reason that no update is available
698-
return {
699-
available: false,
700-
};
710+
return { available: false };
701711
}
702712
} else if (response.status !== 200) {
703713
return { available: false };

0 commit comments

Comments
 (0)