Eliminate IllegalArgumentExceptions

If someone attempts to download a packaged project while a new build is
running, we throw in IllegalArgumentException because the built apk is
no longer present (the first thing a new build does is remove the old
apk). This happens often enough that it is one of the top errors being
logged by the Google Cloud Console.

This change eliminates the IllegalArgumentException and instead just
returns a “File Not Found” error (404 error) to the user attempting the
download.

Change-Id: I6fb92e51477c1ad93059cf57a449396d4fea0d7d
parent ead08b21
...@@ -20,6 +20,7 @@ import com.google.appinventor.shared.storage.StorageUtil; ...@@ -20,6 +20,7 @@ import com.google.appinventor.shared.storage.StorageUtil;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
...@@ -82,8 +83,11 @@ public class BuildOutputServlet extends OdeServlet { ...@@ -82,8 +83,11 @@ public class BuildOutputServlet extends OdeServlet {
} }
downloadableFile = fileExporter.exportProjectOutputFile(nonce.getUserId(), nonce.getProjectId(), null); downloadableFile = fileExporter.exportProjectOutputFile(nonce.getUserId(), nonce.getProjectId(), null);
} catch (IllegalArgumentException e) { } catch (FileNotFoundException e) {
throw CrashReport.createAndLogError(LOG, req, "nonceValue=" + nonceValue, e); // This can happen if a new build is running while an attempt is made to download
// a previous built version of the project
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
} }
String fileName = downloadableFile.getFileName(); String fileName = downloadableFile.getFileName();
......
...@@ -15,6 +15,7 @@ import com.google.appinventor.shared.rpc.project.RawFile; ...@@ -15,6 +15,7 @@ import com.google.appinventor.shared.rpc.project.RawFile;
import com.google.appinventor.shared.storage.StorageUtil; import com.google.appinventor.shared.storage.StorageUtil;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -54,7 +55,7 @@ public final class FileExporterImpl implements FileExporter { ...@@ -54,7 +55,7 @@ public final class FileExporterImpl implements FileExporter {
} }
} }
throw new IllegalArgumentException("No files to download"); throw new FileNotFoundException("No files to download");
} }
@Override @Override
......
...@@ -141,11 +141,11 @@ public class FileExporterImplTest extends LocalDatastoreTestCase { ...@@ -141,11 +141,11 @@ public class FileExporterImplTest extends LocalDatastoreTestCase {
assertTrue(Arrays.equals(TARGET1_CONTENT, file.getContent())); assertTrue(Arrays.equals(TARGET1_CONTENT, file.getContent()));
} }
public void testExportProjectOutputFileWithNonExistingTraget() throws IOException { public void testExportProjectOutputFileWithNonExistingTarget() throws IOException {
try { try {
exporter.exportProjectOutputFile(USER_ID, projectId, "target3"); exporter.exportProjectOutputFile(USER_ID, projectId, "target3");
fail(); fail();
} catch (IllegalArgumentException e) { } catch (FileNotFoundException e) {
// expected // expected
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment