Skip to content
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
39 changes: 39 additions & 0 deletions spec/ParseGraphQLServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8590,6 +8590,45 @@ describe('ParseGraphQLServer', () => {
expect(res.status).toEqual(200);
expect(await res.text()).toEqual('My File Content');
});

it('should return an error instead of crashing if the upload request to the server fails', async () => {
parseServer = await global.reconfigureServer({
serverURL: 'http://localhost:1/parse',
publicServerURL: 'http://localhost:13377/parse',
});
await createGQLFromParseServer(parseServer);
const body = new FormData();
body.append(
'operations',
JSON.stringify({
query: `
mutation CreateFile($input: CreateFileInput!) {
createFile(input: $input) {
fileInfo {
name
}
}
}
`,
variables: { input: { upload: null } },
})
);
body.append('map', JSON.stringify({ 1: ['variables.input.upload'] }));
body.append('1', 'My File Content', {
filename: 'myFileName.txt',
contentType: 'text/plain',
});

const res = await fetch('http://localhost:13377/graphql', {
method: 'POST',
headers,
body,
});
const result = JSON.parse(await res.text());

expect(result.errors.length).toEqual(1);
expect(result.errors[0].message).toEqual('Could not store file: myFileName.txt.');
});
});
});

Expand Down
7 changes: 6 additions & 1 deletion src/GraphQL/loaders/filesMutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const handleUpload = async (upload, config) => {
delete headers['connection'];
delete headers['host'];
delete headers['content-length'];
const stream = createReadStream();
const mime = (await import('mime')).default;
const stream = createReadStream();
try {
const ext = mime.getExtension(mimetype);
const fullFileName = filename.endsWith(`.${ext}`) ? filename : `${filename}.${ext}`;
Expand Down Expand Up @@ -44,6 +44,11 @@ const handleUpload = async (upload, config) => {
});
}
);
req.on('error', reject);
stream.on('error', error => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
req.destroy();
reject(error);
});
stream.pipe(req);
stream.on('end', () => {
req.end();
Expand Down