Skip to content

Commit

Permalink
Merge pull request JabRef#10213 from DohaRamadan/main
Browse files Browse the repository at this point in the history
added error specific messages
  • Loading branch information
Siedlerchr authored Sep 2, 2023
2 parents 2bc65f9 + 341e041 commit bfe22ee
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Added

- We added an error-specific message for when a download from a URL fails[#9826](https://github.com/JabRef/jabref/issues/9826).

### Changed

### Fixed
Expand Down
37 changes: 28 additions & 9 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.jabref.gui.util.ControlHelper;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.externalfiles.LinkedFileHandler;
import org.jabref.logic.importer.FetcherClientException;
import org.jabref.logic.importer.FetcherServerException;
import org.jabref.logic.importer.Importer;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.fileformat.PdfContentImporter;
Expand Down Expand Up @@ -472,17 +474,34 @@ public void download() {
Localization.lang("Fulltext for") + ": " + entry.getCitationKey().orElse(Localization.lang("New entry")));
downloadTask.showToUser(true);
downloadTask.onFailure(ex -> {
LOGGER.error("Error downloading", ex);
dialogService.showErrorDialogAndWait(Localization.lang("Error downloading"), ex);
});
taskExecutor.execute(downloadTask);
} catch (MalformedURLException exception) {
dialogService.showErrorDialogAndWait(Localization.lang("Invalid URL"), exception);
LOGGER.error("Error downloading from URL: " + urlDownload, ex);
String fetcherExceptionMessage = ex.getMessage();
int statusCode;
if (ex instanceof FetcherClientException clientException) {
statusCode = clientException.getStatusCode();
if (statusCode == 401) {
dialogService.showInformationDialogAndWait(Localization.lang("Failed to download from URL"), Localization.lang("401 Unauthorized: Access Denied. You are not authorized to access this resource. Please check your credentials and try again. If you believe you should have access, please contact the administrator for assistance.\nURL: %0 \n %1", urlDownload.getSource(), fetcherExceptionMessage));
} else if (statusCode == 403) {
dialogService.showInformationDialogAndWait(Localization.lang("Failed to download from URL"), Localization.lang("403 Forbidden: Access Denied. You do not have permission to access this resource. Please contact the administrator for assistance or try a different action.\nURL: %0 \n %1", urlDownload.getSource(), fetcherExceptionMessage));
} else if (statusCode == 404) {
dialogService.showInformationDialogAndWait(Localization.lang("Failed to download from URL"), Localization.lang("404 Not Found Error: The requested resource could not be found. It seems that the file you are trying to download is not available or has been moved. Please verify the URL and try again. If you believe this is an error, please contact the administrator for further assistance.\nURL: %0 \n %1", urlDownload.getSource(), fetcherExceptionMessage));
}
} else if (ex instanceof FetcherServerException serverException) {
statusCode = serverException.getStatusCode();
dialogService.showInformationDialogAndWait(Localization.lang("Failed to download from URL"),
Localization.lang("Error downloading form URL. Cause is likely the server side. HTTP Error %0 \n %1 \nURL: %2 \nPlease try again later or contact the server administrator.", statusCode, fetcherExceptionMessage, urlDownload.getSource()));
} else {
dialogService.showErrorDialogAndWait(Localization.lang("Failed to download from URL"), Localization.lang("Error message: %0 \nURL: %1 \nPlease check the URL and try again.", fetcherExceptionMessage, urlDownload.getSource()));
}
});
taskExecutor.execute(downloadTask);
} catch (MalformedURLException exception) {
dialogService.showErrorDialogAndWait(Localization.lang("Invalid URL"), exception);
}
}
}

public boolean checkSSLHandshake(URLDownload urlDownload) {
try {
public boolean checkSSLHandshake(URLDownload urlDownload) {
try {
urlDownload.canBeReached();
} catch (kong.unirest.UnirestException ex) {
if (ex.getCause() instanceof javax.net.ssl.SSLHandshakeException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
* Should be thrown when you encounter an HTTP status code error >= 400 and < 500.
*/
public class FetcherClientException extends FetcherException {
private int statusCode;

public FetcherClientException(String errorMessage, Throwable cause) {
super(errorMessage, cause);
}

public FetcherClientException(String errorMessage, Throwable cause, int statusCode) {
super(errorMessage, cause);
this.statusCode = statusCode;
}

public FetcherClientException(String errorMessage) {
super(errorMessage);
}

public FetcherClientException(String errorMessage, String localizedMessage, Throwable cause) {
super(errorMessage, localizedMessage, cause);
}

public int getStatusCode() {
return statusCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
* Should be thrown when you encounter a http status code error >= 500
*/
public class FetcherServerException extends FetcherException {
private int statusCode;

public FetcherServerException(String errorMessage, Throwable cause) {
super(errorMessage, cause);
}

public FetcherServerException(String errorMessage, Throwable cause, int statusCode) {
super(errorMessage, cause);
this.statusCode = statusCode;
}

public FetcherServerException(String errorMessage) {
super(errorMessage);
}

public FetcherServerException(String errorMessage, String localizedMessage, Throwable cause) {
super(errorMessage, localizedMessage, cause);
}

public int getStatusCode() {
return statusCode;
}
}
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2575,3 +2575,10 @@ Automatically\ search\ and\ show\ unlinked\ files\ in\ the\ entry\ editor=Automa
File\ "%0"\ cannot\ be\ added\!=File "%0" cannot be added!
Illegal\ characters\ in\ the\ file\ name\ detected.\nFile\ will\ be\ renamed\ to\ "%0"\ and\ added.=Illegal characters in the file name detected.\nFile will be renamed to "%0" and added.
Rename\ and\ add=Rename and add
401\ Unauthorized\:\ Access\ Denied.\ You\ are\ not\ authorized\ to\ access\ this\ resource.\ Please\ check\ your\ credentials\ and\ try\ again.\ If\ you\ believe\ you\ should\ have\ access,\ please\ contact\ the\ administrator\ for\ assistance.\nURL\:\ %0\ \n\ %1=401 Unauthorized: Access Denied. You are not authorized to access this resource. Please check your credentials and try again. If you believe you should have access, please contact the administrator for assistance.\nURL: %0 \n %1
403\ Forbidden\:\ Access\ Denied.\ You\ do\ not\ have\ permission\ to\ access\ this\ resource.\ Please\ contact\ the\ administrator\ for\ assistance\ or\ try\ a\ different\ action.\nURL\:\ %0\ \n\ %1=403 Forbidden: Access Denied. You do not have permission to access this resource. Please contact the administrator for assistance or try a different action.\nURL: %0 \n %1
404\ Not\ Found\ Error\:\ The\ requested\ resource\ could\ not\ be\ found.\ It\ seems\ that\ the\ file\ you\ are\ trying\ to\ download\ is\ not\ available\ or\ has\ been\ moved.\ Please\ verify\ the\ URL\ and\ try\ again.\ If\ you\ believe\ this\ is\ an\ error,\ please\ contact\ the\ administrator\ for\ further\ assistance.\nURL\:\ %0\ \n\ %1=404 Not Found Error: The requested resource could not be found. It seems that the file you are trying to download is not available or has been moved. Please verify the URL and try again. If you believe this is an error, please contact the administrator for further assistance.\nURL: %0 \n %1
Error\ downloading\ form\ URL.\ Cause\ is\ likely\ the\ server\ side.\ HTTP\ Error\ %0\ \n\ %1\ \nURL\:\ %2\ \nPlease\ try\ again\ later\ or\ contact\ the\ server\ administrator.=Error downloading form URL. Cause is likely the server side. HTTP Error %0 \n %1 \nURL: %2 \nPlease try again later or contact the server administrator.
Error\ message\:\ %0\ \nURL\:\ %1\ \nPlease\ check\ the\ URL\ and\ try\ again.=Error message: %0 \nURL: %1 \nPlease check the URL and try again.
Failed\ to\ download\ from\ URL=Failed to download from URL

0 comments on commit bfe22ee

Please sign in to comment.