From baa84e94031ee49ea77df49ee52bb6c8e15a9d7a Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 10 Sep 2021 00:19:46 +0530 Subject: [PATCH] Refractor and improve log handling. --- .../java/me/kavin/piped/ServerLauncher.java | 64 +++++++++---------- .../kavin/piped/utils/ExceptionHandler.java | 23 +++++++ .../me/kavin/piped/utils/ResponseHelper.java | 13 ++-- 3 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 src/main/java/me/kavin/piped/utils/ExceptionHandler.java diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 87a104a..6d66de3 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -10,14 +10,11 @@ import static io.activej.http.HttpMethod.POST; import java.io.ByteArrayInputStream; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; -import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import org.apache.commons.lang3.exception.ExceptionUtils; import org.hibernate.Session; 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; @@ -36,6 +33,7 @@ import io.activej.launchers.http.MultithreadedHttpServerLauncher; import me.kavin.piped.consts.Constants; import me.kavin.piped.utils.CustomServletDecorator; import me.kavin.piped.utils.DatabaseSessionFactory; +import me.kavin.piped.utils.ExceptionHandler; import me.kavin.piped.utils.Multithreading; import me.kavin.piped.utils.ResponseHelper; import me.kavin.piped.utils.SponsorBlockUtils; @@ -66,7 +64,6 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { Multithreading.runAsync(() -> { Session s = DatabaseSessionFactory.createSession(); feed.getEntries().forEach(entry -> { - System.out.println(entry.getLinks().get(0).getHref()); ResponseHelper.handleNewVideo(entry.getLinks().get(0).getHref(), entry.getPublishedDate().getTime(), null, s); }); @@ -76,7 +73,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { return HttpResponse.ofCode(204); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/sponsors/:videoId", AsyncServlet.ofBlocking(executor, request -> { try { @@ -85,14 +82,14 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { request.getQueryParameter("category")).getBytes(StandardCharsets.UTF_8), "public, max-age=3600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/streams/:videoId", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.streamsResponse(request.getPathParameter("videoId")), "public, s-maxage=21540"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/channel/:channelId", AsyncServlet.ofBlocking(executor, request -> { try { @@ -100,14 +97,14 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { ResponseHelper.channelResponse("channel/" + request.getPathParameter("channelId")), "public, max-age=600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/c/:name", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.channelResponse("c/" + request.getPathParameter("name")), "public, max-age=600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/user/:name", AsyncServlet.ofBlocking(executor, request -> { try { @@ -115,21 +112,21 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { ResponseHelper.channelResponse("user/" + request.getPathParameter("name")), "public, max-age=600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/nextpage/channel/:channelId", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.channelPageResponse(request.getPathParameter("channelId"), request.getQueryParameter("nextpage")), "public, max-age=3600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/playlists/:playlistId", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.playlistResponse(request.getPathParameter("playlistId")), "public, max-age=600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/nextpage/playlists/:playlistId", AsyncServlet.ofBlocking(executor, request -> { try { @@ -138,7 +135,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { request.getQueryParameter("nextpage")), "public, max-age=3600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/rss/playlists/:playlistId", AsyncServlet.ofBlocking(executor, request -> { try { @@ -146,21 +143,21 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { ResponseHelper.playlistRSSResponse(request.getPathParameter("playlistId")), "public, s-maxage=600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/suggestions", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.suggestionsResponse(request.getQueryParameter("query")), "public, max-age=600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/search", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.searchResponse(request.getQueryParameter("q"), request.getQueryParameter("filter")), "public, max-age=600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/nextpage/search", AsyncServlet.ofBlocking(executor, request -> { try { @@ -169,28 +166,28 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { request.getQueryParameter("filter"), request.getQueryParameter("nextpage")), "public, max-age=3600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/trending", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.trendingResponse(request.getQueryParameter("region")), "public, max-age=3600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/comments/:videoId", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.commentsResponse(request.getPathParameter("videoId")), "public, max-age=1200"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/nextpage/comments/:videoId", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.commentsPageResponse(request.getPathParameter("videoId"), request.getQueryParameter("nextpage")), "public, max-age=3600"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(POST, "/register", AsyncServlet.ofBlocking(executor, request -> { try { @@ -199,7 +196,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { return getJsonResponse(ResponseHelper.registerResponse(body.username, body.password), "private"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(POST, "/login", AsyncServlet.ofBlocking(executor, request -> { try { @@ -207,7 +204,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { LoginRequest.class); return getJsonResponse(ResponseHelper.loginResponse(body.username, body.password), "private"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(POST, "/subscribe", AsyncServlet.ofBlocking(executor, request -> { try { @@ -217,7 +214,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { ResponseHelper.subscribeResponse(request.getHeader(AUTHORIZATION), body.channelId), "private"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(POST, "/unsubscribe", AsyncServlet.ofBlocking(executor, request -> { try { @@ -227,28 +224,28 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { ResponseHelper.unsubscribeResponse(request.getHeader(AUTHORIZATION), body.channelId), "private"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/subscribed", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.isSubscribedResponse(request.getHeader(AUTHORIZATION), request.getQueryParameter("channelId")), "private"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/feed", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.feedResponse(request.getQueryParameter("authToken")), "private"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/feed/rss", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.feedResponseRSS(request.getQueryParameter("authToken")), "public, s-maxage=120"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(POST, "/import", AsyncServlet.ofBlocking(executor, request -> { try { @@ -257,14 +254,14 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { return getJsonResponse(ResponseHelper.importResponse(request.getHeader(AUTHORIZATION), subscriptions, Boolean.parseBoolean(request.getQueryParameter("override"))), "private"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })).map(GET, "/subscriptions", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.subscriptionsResponse(request.getHeader(AUTHORIZATION)), "private"); } catch (Exception e) { - return getErrorResponse(e); + return getErrorResponse(e, request.getPath()); } })); @@ -293,13 +290,10 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { .withHeader(CACHE_CONTROL, cache); } - private @NotNull HttpResponse getErrorResponse(Exception e) { + private @NotNull HttpResponse getErrorResponse(Exception e, String path) { - if (e.getCause() != null && e instanceof ExecutionException) - e = (Exception) e.getCause(); - - if (!(e instanceof AgeRestrictedContentException || e instanceof ContentNotAvailableException)) - e.printStackTrace(); + System.err.println("An error occoured in the path: " + path); + e = ExceptionHandler.handle(e); try { return getJsonResponse(500, Constants.mapper diff --git a/src/main/java/me/kavin/piped/utils/ExceptionHandler.java b/src/main/java/me/kavin/piped/utils/ExceptionHandler.java new file mode 100644 index 0000000..6b430de --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/ExceptionHandler.java @@ -0,0 +1,23 @@ +package me.kavin.piped.utils; + +import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutionException; + +import org.schabi.newpipe.extractor.exceptions.AgeRestrictedContentException; +import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; +import org.schabi.newpipe.extractor.exceptions.GeographicRestrictionException; + +public class ExceptionHandler { + + public static Exception handle(Exception e) { + + if (e.getCause() != null && (e instanceof ExecutionException || e instanceof CompletionException)) + e = (Exception) e.getCause(); + + if (!(e instanceof AgeRestrictedContentException || e instanceof ContentNotAvailableException + || e instanceof GeographicRestrictionException)) + e.printStackTrace(); + + return e; + } +} diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 085b1c8..dafb301 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -665,7 +665,7 @@ public class ResponseHelper { subscribePubSub(channelId, sessSub); sessSub.close(); } catch (Exception e) { - e.printStackTrace(); + ExceptionHandler.handle(e); } }); @@ -878,7 +878,7 @@ public class ResponseHelper { subscribePubSub(channelId, sessSub); sessSub.close(); } catch (Exception e) { - e.printStackTrace(); + ExceptionHandler.handle(e); } }); @@ -898,7 +898,7 @@ public class ResponseHelper { sess.close(); } catch (Exception e) { - e.printStackTrace(); + ExceptionHandler.handle(e); } }); @@ -977,7 +977,7 @@ public class ResponseHelper { try { handleNewVideo(StreamInfo.getInfo(url), time, channel, s); } catch (Exception e) { - e.printStackTrace(); + ExceptionHandler.handle(e); } } @@ -1033,7 +1033,7 @@ public class ResponseHelper { s.close(); } catch (Exception e) { - e.printStackTrace(); + ExceptionHandler.handle(e); } }); } @@ -1043,7 +1043,6 @@ public class ResponseHelper { PubSub pubsub = DatabaseHelper.getPubSubFromId(s, channelId); if (pubsub == null || System.currentTimeMillis() - pubsub.getSubbedAt() > TimeUnit.DAYS.toMillis(4)) { - System.out.println(String.format("PubSub: Subscribing to %s", channelId)); String callback = Constants.PUBLIC_URL + "/webhooks/pubsub"; String topic = "https://www.youtube.com/xml/feeds/videos.xml?channel_id=" + channelId; @@ -1105,7 +1104,7 @@ public class ResponseHelper { try { url = new URL(old); } catch (MalformedURLException e) { - e.printStackTrace(); + ExceptionHandler.handle(e); } final String host = url.getHost();