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
4 changes: 1 addition & 3 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"typescript/no-non-null-assertion": "off",
"typescript/no-floating-promises": "error",
"typescript/no-redundant-type-constituents": "off",
"typescript/require-array-sort-compare": "off",
"typescript/no-base-to-string": "off",
"typescript/restrict-template-expressions": "off"
"typescript/require-array-sort-compare": "off"
},
"overrides": [
{
Expand Down
17 changes: 17 additions & 0 deletions src/error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {UnexpectedObjectError} from './error';

describe('UnexpectedObjectError', () => {
it('works as expected', () => {
const item = {it: 'works'};
try {
throw new UnexpectedObjectError(item as any);
} catch (e: any) {
expect(e.name).toEqual('UnexpectedObjectError');
expect(e.toString()).toEqual('UnexpectedObjectError: DataTransferItem is not a file');
expect(e).toBeInstanceOf(Error);
expect(e).toBeInstanceOf(UnexpectedObjectError);
expect(e.item).toEqual(item);
expect(e.stack).toBeDefined();
}
});
});
8 changes: 8 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class UnexpectedObjectError extends Error {
item: DataTransferItem;
constructor(item: DataTransferItem) {
super('DataTransferItem is not a file');
this.item = item;
this.name = 'UnexpectedObjectError';
}
}
2 changes: 1 addition & 1 deletion src/file-selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ it('should not use getAsFileSystemHandle when not in a secure context', async ()

it('should reject when getAsFileSystemHandle resolves to null', async () => {
const evt = dragEvtFromItems([dataTransferItemWithFsHandle(null, null)]);
expect(fromEvent(evt)).rejects.toThrow('[object Object] is not a File');
expect(fromEvent(evt)).rejects.toThrow('DataTransferItem is not a file');
});

it('should fallback to getAsFile when getAsFileSystemHandle resolves to undefined', async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/file-selector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {UnexpectedObjectError} from './error';
import {type FileWithPath, toFileWithPath} from './file';

const FILES_TO_IGNORE = [
Expand Down Expand Up @@ -129,7 +130,7 @@ async function fromDataTransferItem(item: DataTransferItem, entry?: FileSystemEn
if (globalThis.isSecureContext && typeof (item as any).getAsFileSystemHandle === 'function') {
const h = await (item as any).getAsFileSystemHandle();
if (h === null) {
throw new Error(`${item} is not a File`);
throw new UnexpectedObjectError(item);
}
// It seems that the handle can be `undefined` (see https://github.com/react-dropzone/file-selector/issues/120),
// so we check if it isn't; if it is, the code path continues to the next API (`getAsFile`).
Expand All @@ -141,7 +142,7 @@ async function fromDataTransferItem(item: DataTransferItem, entry?: FileSystemEn
}
const file = item.getAsFile();
if (!file) {
throw new Error(`${item} is not a File`);
throw new UnexpectedObjectError(item);
}
const fwp = toFileWithPath(file, entry?.fullPath ?? undefined);
return fwp;
Expand Down