diff --git a/spec/ParseGraphQLServer.spec.js b/spec/ParseGraphQLServer.spec.js index 3e3ac6aaad..85f8289079 100644 --- a/spec/ParseGraphQLServer.spec.js +++ b/spec/ParseGraphQLServer.spec.js @@ -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.'); + }); }); }); diff --git a/src/GraphQL/loaders/filesMutations.js b/src/GraphQL/loaders/filesMutations.js index 8439dfeb4f..f8ea146d4f 100644 --- a/src/GraphQL/loaders/filesMutations.js +++ b/src/GraphQL/loaders/filesMutations.js @@ -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}`; @@ -44,6 +44,11 @@ const handleUpload = async (upload, config) => { }); } ); + req.on('error', reject); + stream.on('error', error => { + req.destroy(); + reject(error); + }); stream.pipe(req); stream.on('end', () => { req.end();