Skip to content

Commit

Permalink
show error when fetching templates from Github
Browse files Browse the repository at this point in the history
This shows "some" error in the UI when there are problems fetching from Github.
The error message also includes a hint to https://bnd.bndtools.org/instructions/connection-settings.html because sometimes you have a ~/.m2/settings.xml with some credentials which can lead to "Bad Credentials" http errors from the Github API, but before this fix you had no idea what was going on, because the UI was blank and the existance of ~/.m2/settings.xml is easily forgotton

Signed-off-by: Christoph Rueger <[email protected]>
  • Loading branch information
chrisrueger committed Jan 23, 2025
1 parent c986676 commit e76acae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
import org.osgi.service.component.ComponentConstants;
import org.osgi.util.promise.Promise;

import aQute.bnd.osgi.Processor;
import aQute.bnd.exceptions.Exceptions;
import aQute.bnd.osgi.Processor;
import aQute.lib.io.IO;
import aQute.libg.tuple.Pair;
import bndtools.Plugin;
Expand Down Expand Up @@ -294,11 +294,20 @@ public void run(IProgressMonitor progress) throws InvocationTargetException {
try {
Throwable failure = namedPromise.getSecond()
.getFailure();
if (failure != null)
if (failure != null) {

shell.getDisplay()
.asyncExec(() -> {
txtDescription.setText("Failed to load from template loader (" + name
+ ") See Error Log for stack trace: "
+ failure.getMessage());
});

Plugin.getDefault()
.getLog()
.log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0,
"Failed to load from template loader: " + name, failure));
}
else {
Collection<Template> loadedTemplates = namedPromise.getSecond()
.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public byte[] download(URI uri) throws Exception {
TaggedData td = client.connectTagged(uri.toURL());
if (td == null || td.isNotFound())
throw new FileNotFoundException("Not found");

if (td.getInputStream() == null) {
throwNoResponseError(td);
}

data = IO.read(td.getInputStream());
if (td.getTag() != null)
cache.put(uri, new Pair<>(td.getTag(), data));
Expand All @@ -41,6 +46,11 @@ public byte[] download(URI uri) throws Exception {
data = cachedTag.getSecond();
} else {
// changed

if (td.getInputStream() == null) {
throwNoResponseError(td);
}

data = IO.read(td.getInputStream());
if (td.getTag() == null) {
// server now not giving an etag -> remove from cache
Expand All @@ -59,4 +69,9 @@ public byte[] download(URI uri) throws Exception {
}
}

private void throwNoResponseError(TaggedData td) throws IOException {
throw new IOException("Error (HTTP " + td.getResponseCode() + ") - no response: " + td
+ " (Check https://bnd.bndtools.org/instructions/connection-settings.html in case of connection or authentication errors.)");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bndtools.templating.jgit;

import java.io.IOException;
import java.net.URI;
import java.util.Objects;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -33,6 +34,9 @@ public Promise<GithubRepoDetailsDTO> loadRepoDetails(String repository) {
return new JSONCodec().dec()
.from(detailsDtoData)
.get(GithubRepoDetailsDTO.class);
})
.onFailure(t -> {
throw new IOException("Error fetching Github repo details: " + URL_PREFIX + repository, t);
});
}
}

0 comments on commit e76acae

Please sign in to comment.