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;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.NoSuchElementException;
......@@ -82,8 +83,11 @@ public class BuildOutputServlet extends OdeServlet {
}
downloadableFile = fileExporter.exportProjectOutputFile(nonce.getUserId(), nonce.getProjectId(), null);
} catch (IllegalArgumentException e) {
throw CrashReport.createAndLogError(LOG, req, "nonceValue=" + nonceValue, e);
} catch (FileNotFoundException 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();
......
......@@ -15,6 +15,7 @@ import com.google.appinventor.shared.rpc.project.RawFile;
import com.google.appinventor.shared.storage.StorageUtil;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
......@@ -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
......
......@@ -141,11 +141,11 @@ public class FileExporterImplTest extends LocalDatastoreTestCase {
assertTrue(Arrays.equals(TARGET1_CONTENT, file.getContent()));
}
public void testExportProjectOutputFileWithNonExistingTraget() throws IOException {
public void testExportProjectOutputFileWithNonExistingTarget() throws IOException {
try {
exporter.exportProjectOutputFile(USER_ID, projectId, "target3");
fail();
} catch (IllegalArgumentException e) {
} catch (FileNotFoundException e) {
// 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