From d5585738da7f8f29fe0c34e21dce6e90c5d7346c Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 31 Oct 2022 11:50:29 +0000 Subject: [PATCH] Throw 400 response for invalid parameters messages from extractor. --- .../me/kavin/piped/utils/ErrorResponse.java | 12 ++++++++++ .../kavin/piped/utils/ExceptionHandler.java | 22 ++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) 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;