From 6eafdc8f4924f5907bb53ca210aeb2ca2b233e64 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Mon, 6 Mar 2017 12:56:16 -0800 Subject: [PATCH] Make error messages more descriptive --- src/Podsync/Controllers/DownloadController.cs | 5 +++-- src/Podsync/Services/Resolver/RemoteResolver.cs | 15 +++++++++++++-- src/ytdl/ytdl.py | 11 ++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Podsync/Controllers/DownloadController.cs b/src/Podsync/Controllers/DownloadController.cs index 1b7d2fe..993a20e 100644 --- a/src/Podsync/Controllers/DownloadController.cs +++ b/src/Podsync/Controllers/DownloadController.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Podsync.Helpers; @@ -45,8 +46,8 @@ namespace Podsync.Controllers } catch (Exception ex) { - var response = "Could nou resolve URL"; - if (ex is InvalidOperationException) + var response = "Could not resolve URL"; + if (ex is InvalidOperationException || ex is HttpRequestException) { response = ex.Message; } diff --git a/src/Podsync/Services/Resolver/RemoteResolver.cs b/src/Podsync/Services/Resolver/RemoteResolver.cs index bdddf0d..56280a2 100644 --- a/src/Podsync/Services/Resolver/RemoteResolver.cs +++ b/src/Podsync/Services/Resolver/RemoteResolver.cs @@ -24,8 +24,19 @@ namespace Podsync.Services.Resolver protected override async Task ResolveInternal(Uri videoUrl, ResolveFormat format) { - var response = await _client.GetStringAsync($"/resolve?url={videoUrl}&quality={format}"); - return new Uri(response); + using(var response = await _client.GetAsync($"/resolve?url={videoUrl}&quality={format}")) + { + using(response.Content) + { + var body = await response.Content.ReadAsStringAsync(); + if (!response.IsSuccessStatusCode) + { + throw new HttpRequestException(body); + } + + return new Uri(body); + } + } } } } \ No newline at end of file diff --git a/src/ytdl/ytdl.py b/src/ytdl/ytdl.py index 402129f..f205c5d 100644 --- a/src/ytdl/ytdl.py +++ b/src/ytdl/ytdl.py @@ -1,4 +1,5 @@ import youtube_dl +from youtube_dl.utils import DownloadError from sanic import Sanic from sanic.exceptions import InvalidUsage from sanic.response import text @@ -42,9 +43,13 @@ async def youtube(request): if fmt: opts.update(format=fmt) - with youtube_dl.YoutubeDL(opts) as ytdl: - info = ytdl.extract_info(url, download=False) - return text(info['url']) + try: + with youtube_dl.YoutubeDL(opts) as ytdl: + info = ytdl.extract_info(url, download=False) + return text(info['url']) + except DownloadError as e: + msg = str(e) + return text(msg, status=511) def _choose_format(quality, url):