Skip to content

feat: use a custom error when items are not files #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 @@ -337,7 +337,7 @@ 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
6 changes: 3 additions & 3 deletions src/file-selector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {UnexpectedObjectError} from './error';
import {FileWithPath, toFileWithPath} from './file';


const FILES_TO_IGNORE = [
// Thumbnail cache files for macOS and Windows
'.DS_Store', // macOs
Expand Down Expand Up @@ -130,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 @@ -142,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
Loading