Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/__tests__/utils/pt9-import-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ describe('isPt9TooLargeError', () => {
expect(isPt9TooLargeError(new Error('boom'))).toBe(false);
expect(isPt9TooLargeError(MARKER_MESSAGE)).toBe(false);
});

it('rejects a platform error carrying no message instead of throwing', () => {
// Narrows as a platform error despite omitting the message the type declares as required.
const noMessage: unknown = { platformErrorVersion: 1 };

expect(() => isPt9TooLargeError(noMessage)).not.toThrow();
expect(isPt9TooLargeError(noMessage)).toBe(false);
});
});
7 changes: 6 additions & 1 deletion src/utils/pt9-import-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const PT9_TOO_LARGE_MARKER = 'PT9 interlinear data is too large';
*/
export function isPt9TooLargeError(error: unknown): boolean {
if (isPlatformError(error))
return error.code === 'RESOURCE_EXHAUSTED' || error.message.includes(PT9_TOO_LARGE_MARKER);
// The narrowing proves less than the type does: a value can satisfy it without carrying the
// declared message, and we want to answer rather than throw.
return (
error.code === 'RESOURCE_EXHAUSTED' ||
(typeof error.message === 'string' && error.message.includes(PT9_TOO_LARGE_MARKER))
);
return error instanceof Error && error.message.includes(PT9_TOO_LARGE_MARKER);
}
Loading