diff --git a/src/main/java/me/kavin/piped/utils/ErrorResponse.java b/src/main/java/me/kavin/piped/utils/ErrorResponse.java index f6d394f..0a819c1 100644 --- a/src/main/java/me/kavin/piped/utils/ErrorResponse.java +++ b/src/main/java/me/kavin/piped/utils/ErrorResponse.java @@ -25,11 +25,23 @@ public class ErrorResponse extends Exception { this.content = mapper.writeValueAsBytes(statusObj); } + public ErrorResponse(IStatusCode statusObj, Throwable throwable) throws JsonProcessingException { + super(throwable); + this.code = statusObj.getStatusCode(); + this.content = mapper.writeValueAsBytes(statusObj); + } + public ErrorResponse(int code, Object content) throws JsonProcessingException { this.code = code; this.content = mapper.writeValueAsBytes(content); } + public ErrorResponse(int code, Object content, Throwable throwable) throws JsonProcessingException { + super(throwable); + this.code = code; + this.content = mapper.writeValueAsBytes(content); + } + public int getCode() { return code; } diff --git a/src/main/java/me/kavin/piped/utils/ExceptionHandler.java b/src/main/java/me/kavin/piped/utils/ExceptionHandler.java index 86fb1f6..ed7080c 100644 --- a/src/main/java/me/kavin/piped/utils/ExceptionHandler.java +++ b/src/main/java/me/kavin/piped/utils/ExceptionHandler.java @@ -3,8 +3,10 @@ package me.kavin.piped.utils; import com.fasterxml.jackson.core.JsonProcessingException; import io.sentry.Sentry; import me.kavin.piped.consts.Constants; +import me.kavin.piped.utils.resp.InvalidRequestResponse; import org.apache.commons.lang3.exception.ExceptionUtils; import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; @@ -20,13 +22,21 @@ public class ExceptionHandler { if (e.getCause() != null && (e instanceof ExecutionException || e instanceof CompletionException)) e = (Exception) e.getCause(); - if (!(e instanceof ContentNotAvailableException || e instanceof ErrorResponse)) { - Sentry.captureException(e); - if (Constants.SENTRY_DSN.isEmpty()) { - if (path != null) - System.err.println("An error occoured in the path: " + path); - e.printStackTrace(); + if (e instanceof ContentNotAvailableException || e instanceof ErrorResponse) + return e; + + if ((e instanceof ExtractionException extractionException && extractionException.getMessage().contains("No service can handle the url"))) + try { + return new ErrorResponse(new InvalidRequestResponse("Invalid parameter provided, unknown service"), extractionException); + } catch (JsonProcessingException jsonProcessingException) { + throw new RuntimeException(jsonProcessingException); } + + Sentry.captureException(e); + if (Constants.SENTRY_DSN.isEmpty()) { + if (path != null) + System.err.println("An error occoured in the path: " + path); + e.printStackTrace(); } return e; diff --git a/src/main/java/me/kavin/piped/utils/RydHelper.java b/src/main/java/me/kavin/piped/utils/RydHelper.java index dfa4375..0579f0a 100644 --- a/src/main/java/me/kavin/piped/utils/RydHelper.java +++ b/src/main/java/me/kavin/piped/utils/RydHelper.java @@ -6,7 +6,7 @@ import me.kavin.piped.consts.Constants; import java.io.IOException; import static me.kavin.piped.consts.Constants.mapper; -import static me.kavin.piped.utils.RequestUtils.sendGet; +import static me.kavin.piped.utils.RequestUtils.sendGetRaw; public class RydHelper { public static double getDislikeRating(String videoId) throws IOException { @@ -14,9 +14,15 @@ public class RydHelper { if (Constants.DISABLE_RYD) return -1; - var value = mapper.readTree(sendGet(Constants.RYD_PROXY_URL + "/votes/" + videoId)) - .get("rating"); + try (var resp = sendGetRaw(Constants.RYD_PROXY_URL + "/votes/" + videoId)) { - return value == null ? -1 : value.asDouble(-1); + if (!resp.isSuccessful()) + return -1; + + return mapper.readTree(resp.body().byteStream()) + .path("rating") + .asDouble(-1); + + } } }