Skip to content

Commit 4f60d5f

Browse files
author
lh01217311
committed
fix: handle action/data async rejection in processFile
- Add try-catch for async action/data functions - Return parsedFile: null when action/data reject (same as beforeUpload strategy) - This prevents unhandled Promise rejection and filters out failed files
1 parent dbe15d8 commit 4f60d5f

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

src/AjaxUploader.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,48 @@ class AjaxUploader extends Component<UploadProps> {
220220
const { action } = this.props;
221221
let mergedAction: string;
222222
if (typeof action === 'function') {
223-
mergedAction = await action(file);
223+
try {
224+
mergedAction = await action(file);
225+
} catch {
226+
transformedFile = false;
227+
}
224228
} else {
225229
mergedAction = action;
226230
}
227231

232+
// Early return if action rejected
233+
if (transformedFile === false) {
234+
return {
235+
origin: file,
236+
parsedFile: null,
237+
action: null,
238+
data: null,
239+
};
240+
}
241+
228242
// Get latest data
229243
const { data } = this.props;
230244
let mergedData: Record<string, unknown>;
231245
if (typeof data === 'function') {
232-
mergedData = await data(file);
246+
try {
247+
mergedData = await data(file);
248+
} catch {
249+
transformedFile = false;
250+
}
233251
} else {
234252
mergedData = data;
235253
}
236254

255+
// Early return if data rejected
256+
if (transformedFile === false) {
257+
return {
258+
origin: file,
259+
parsedFile: null,
260+
action: null,
261+
data: null,
262+
};
263+
}
264+
237265
const parsedData =
238266
// string type is from legacy `transformFile`.
239267
// Not sure if this will work since no related test case works with it

0 commit comments

Comments
 (0)