1
0
mirror of https://github.com/demodude4u/Factorio-FBSR.git synced 2024-11-24 08:12:21 +02:00

Emergency fix to add reporting for when reporting fails...

This commit is contained in:
Weston Rye (Demod) 2017-06-24 12:53:19 -04:00
parent 8387bb9b0f
commit e2f3b78355
2 changed files with 90 additions and 73 deletions

View File

@ -15,7 +15,20 @@ import com.demod.factorio.Utils;
import com.demod.fbsr.app.BlueprintBotDiscordService;
import com.demod.fbsr.app.ServiceFinder;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.entities.MessageEmbed;
public final class WebUtils {
public static void addPossiblyLargeEmbedField(EmbedBuilder builder, String name, String value, boolean inline)
throws IOException {
if (value.length() <= MessageEmbed.VALUE_MAX_LENGTH) {
builder.addField(name, value, inline);
} else {
builder.addField(name + " Link", uploadToHostingService(name + ".txt", value.getBytes()).toString(),
inline);
}
}
public static InputStream limitMaxBytes(InputStream delegate, int maxBytes) {
return new InputStream() {
int byteCount = 0;

View File

@ -325,98 +325,102 @@ public class BlueprintBotDiscordService extends AbstractIdleService {
}
public void sendReport(String author, String authorURL, TaskReporting reporting) {
Optional<String> context = reporting.getContext();
List<Exception> exceptions = reporting.getExceptions();
List<String> warnings = reporting.getWarnings();
List<String> images = reporting.getImages();
List<String> links = reporting.getLinks();
List<String> downloads = reporting.getDownloads();
Set<String> info = reporting.getInfo();
try {
Optional<String> context = reporting.getContext();
List<Exception> exceptions = reporting.getExceptions();
List<String> warnings = reporting.getWarnings();
List<String> images = reporting.getImages();
List<String> links = reporting.getLinks();
List<String> downloads = reporting.getDownloads();
Set<String> info = reporting.getInfo();
EmbedBuilder builder = new EmbedBuilder();
builder.setAuthor(author, null, authorURL);
builder.setTimestamp(Instant.now());
EmbedBuilder builder = new EmbedBuilder();
builder.setAuthor(author, null, authorURL);
builder.setTimestamp(Instant.now());
Level level = reporting.getLevel();
if (level != Level.INFO) {
builder.setColor(level.getColor());
}
Level level = reporting.getLevel();
if (level != Level.INFO) {
builder.setColor(level.getColor());
}
if (context.isPresent() && context.get().length() <= MADEUP_NUMBER_FROM_AROUND_5_IN_THE_MORNING) {
builder.addField("Context", context.get(), false);
} else if (context.isPresent()) {
try {
if (context.isPresent() && context.get().length() <= MADEUP_NUMBER_FROM_AROUND_5_IN_THE_MORNING) {
builder.addField("Context", context.get(), false);
} else if (context.isPresent()) {
builder.addField("Context Link",
WebUtils.uploadToHostingService("context.txt", context.get().getBytes()).toString(), false);
} catch (IOException e) {
e.printStackTrace();
}
}
if (!links.isEmpty()) {
builder.addField("Link(s)", links.stream().collect(Collectors.joining("\n")), false);
}
if (!links.isEmpty()) {
builder.addField("Link(s)", links.stream().collect(Collectors.joining("\n")), false);
}
if (!images.isEmpty()) {
builder.setImage(images.get(0));
}
if (images.size() > 1) {
builder.addField("Additional Image(s)", images.stream().skip(1).collect(Collectors.joining("\n")), false);
}
if (!images.isEmpty()) {
builder.setImage(images.get(0));
}
if (images.size() > 1) {
WebUtils.addPossiblyLargeEmbedField(builder, "Additional Image(s)",
images.stream().skip(1).collect(Collectors.joining("\n")), false);
}
if (!downloads.isEmpty()) {
builder.addField("Download(s)", downloads.stream().collect(Collectors.joining("\n")), false);
}
if (!downloads.isEmpty()) {
builder.addField("Download(s)", downloads.stream().collect(Collectors.joining("\n")), false);
}
if (!info.isEmpty()) {
builder.addField("Info", info.stream().collect(Collectors.joining("\n")), false);
}
if (!info.isEmpty()) {
builder.addField("Info", info.stream().collect(Collectors.joining("\n")), false);
}
Multiset<String> uniqueWarnings = LinkedHashMultiset.create(warnings);
if (!uniqueWarnings.isEmpty()) {
builder.addField("Warnings",
uniqueWarnings.entrySet().stream()
.map(e -> e.getElement() + (e.getCount() > 1 ? " *(**" + e.getCount() + "** times)*" : ""))
.collect(Collectors.joining("\n")),
false);
}
Multiset<String> uniqueWarnings = LinkedHashMultiset.create(warnings);
if (!uniqueWarnings.isEmpty()) {
builder.addField("Warnings",
uniqueWarnings.entrySet().stream().map(
e -> e.getElement() + (e.getCount() > 1 ? " *(**" + e.getCount() + "** times)*" : ""))
.collect(Collectors.joining("\n")),
false);
}
Multiset<String> uniqueExceptions = LinkedHashMultiset.create();
Optional<String> exceptionFile = Optional.empty();
if (!exceptions.isEmpty()) {
try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
for (Exception e : exceptions) {
if (uniqueExceptions.add(e.getClass().getSimpleName() + ": " + e.getMessage())) {
e.printStackTrace();
e.printStackTrace(pw);
Multiset<String> uniqueExceptions = LinkedHashMultiset.create();
Optional<String> exceptionFile = Optional.empty();
if (!exceptions.isEmpty()) {
try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
for (Exception e : exceptions) {
if (uniqueExceptions.add(e.getClass().getSimpleName() + ": " + e.getMessage())) {
e.printStackTrace();
e.printStackTrace(pw);
}
}
pw.flush();
exceptionFile = Optional.of(sw.toString());
}
}
if (!uniqueExceptions.isEmpty()) {
builder.addField("Exceptions",
uniqueExceptions.entrySet().stream().map(
e -> e.getElement() + (e.getCount() > 1 ? " *(**" + e.getCount() + "** times)*" : ""))
.collect(Collectors.joining("\n")),
false);
}
if (exceptionFile.isPresent()) {
builder.addField("Stack Trace(s)",
WebUtils.uploadToHostingService("exceptions.txt", exceptionFile.get().getBytes()).toString(),
false);
}
PrivateChannel privateChannel = bot.getJDA().getUserById(reportingUserID).openPrivateChannel().complete();
privateChannel.sendMessage(builder.build()).complete();
} catch (Exception e) {
PrivateChannel privateChannel = bot.getJDA().getUserById(reportingUserID).openPrivateChannel().complete();
privateChannel.sendMessage("Failed to create report!").complete();
try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
e.printStackTrace();
e.printStackTrace(pw);
pw.flush();
exceptionFile = Optional.of(sw.toString());
privateChannel.sendFile(sw.toString().getBytes(), "Exception.txt", null).complete();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (!uniqueExceptions.isEmpty()) {
builder.addField("Exceptions",
uniqueExceptions.entrySet().stream()
.map(e -> e.getElement() + (e.getCount() > 1 ? " *(**" + e.getCount() + "** times)*" : ""))
.collect(Collectors.joining("\n")),
false);
}
if (exceptionFile.isPresent()) {
try {
builder.addField("Stack Trace(s)",
WebUtils.uploadToHostingService("exceptions.txt", exceptionFile.get().getBytes()).toString(),
false);
} catch (IOException e) {
e.printStackTrace();
}
}
PrivateChannel privateChannel = bot.getJDA().getUserById(reportingUserID).openPrivateChannel().complete();
privateChannel.sendMessage(builder.build()).complete();
}
@Override