From d53459171f9c8c869c559c1ebecd0f9a4deed5af Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 7 Jun 2021 00:15:13 +0530 Subject: [PATCH] Give better responses on errors. --- src/main/java/me/kavin/piped/ServerLauncher.java | 13 ++++++++++--- .../me/kavin/piped/utils/resp/ErrorResponse.java | 5 +++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 5b3147c..3497533 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -7,11 +7,14 @@ import static io.activej.http.HttpHeaders.CONTENT_TYPE; import java.io.ByteArrayInputStream; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; +import org.schabi.newpipe.extractor.exceptions.AgeRestrictedContentException; +import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; import org.xml.sax.InputSource; import com.fasterxml.jackson.core.JsonProcessingException; @@ -191,11 +194,15 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { private @NotNull HttpResponse getErrorResponse(Exception e) { - e.printStackTrace(); + if (e instanceof ExecutionException) + e = (Exception) e.getCause(); + + if (!(e instanceof AgeRestrictedContentException || e instanceof ContentNotAvailableException)) + e.printStackTrace(); try { - return getJsonResponse(500, - Constants.mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e))), "private"); + return getJsonResponse(500, Constants.mapper + .writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage())), "private"); } catch (JsonProcessingException ex) { return HttpResponse.ofCode(500); } diff --git a/src/main/java/me/kavin/piped/utils/resp/ErrorResponse.java b/src/main/java/me/kavin/piped/utils/resp/ErrorResponse.java index ef7b2e6..24dad5a 100644 --- a/src/main/java/me/kavin/piped/utils/resp/ErrorResponse.java +++ b/src/main/java/me/kavin/piped/utils/resp/ErrorResponse.java @@ -2,9 +2,10 @@ package me.kavin.piped.utils.resp; public class ErrorResponse { - public final String error; + public final String error, message; - public ErrorResponse(String error) { + public ErrorResponse(String error, String message) { this.error = error; + this.message = message; } }